最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

github actions - Sync develop with master - Stack Overflow

programmeradmin1浏览0评论

In my anization, we release from the develop branch, and after each release, we sync master with develop. To achieve this, we create two pull requests: one from develop to main and then another from main to develop to ensure there are no commit differences between the two branches.

I am trying to automate this using GitHub Actions.

The workflow works fine when syncing code from develop to main, creating a merge commit in main. However, when syncing from main to develop, it adds an extra commit to develop and seems to result in an infinite loop. Why might this be happening? Also commits should be verified.

name: Auto Sync Develop and Master

on:
  workflow_dispatch: # Manually trigger if needed

jobs:
  sync-to-master:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # Ensures full history for merging

      - name: Check if a PR exists between Develop and Main
        id: pr_check
        env:
          GITHUB_TOKEN: ${{ secrets.DEV_TO_MASTER_SYNC }}
        run: |
          # Check if a PR exists between develop and main
          pr_exists=$(gh pr list --base main --head develop --json number --jq '.[0].number')
          
          if [ -z "$pr_exists" ]; then
            echo "No existing PR found, creating a new PR."
            # Create the PR without the --json flag for PR number
            gh pr create --base main --head develop --title "Auto Merge Develop -> Main" --body "Automated merge after production deployment."
            # Now get the PR number using the list command
            pr_number=$(gh pr list --base main --head develop --json number --jq '.[0].number')
            echo "Pull Request created with number $pr_number"
          else
            echo "PR already exists. Skipping creation."
            pr_number=$pr_exists
          fi

          # Output the PR number for later steps
          echo "pr_number=$pr_number" >> $GITHUB_ENV

      - name: Merge PR into Main
        env:
          GITHUB_TOKEN: ${{ secrets.DEV_TO_MASTER_SYNC }}
        run: |
          echo "PR Number: ${{ env.pr_number }}"
          if [ -n "${{ env.pr_number }}" ] && [ "${{ env.pr_number }}" != "null" ]; then
            gh pr merge ${{ env.pr_number }} --squash
            echo "PR merged into main"
          else
            echo "No PR to merge"
          fi

  sync-back-to-develop:
    needs: sync-to-master
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check if a PR exists between Main and Develop
        id: pr_check_dev
        env:
          GITHUB_TOKEN: ${{ secrets.DEV_TO_MASTER_SYNC }}
        run: |
          # Check if a PR exists between main and develop
          pr_exists=$(gh pr list --base develop --head main --json number --jq '.[0].number')

          if [ -z "$pr_exists" ]; then
            echo "No existing PR found, creating a new PR."
            # Create the PR
            gh pr create --base develop --head main --title "Auto Merge Main -> Develop" --body "Syncing changes from main to develop."
            # Get the new PR number
            pr_number=$(gh pr list --base develop --head main --json number --jq '.[0].number')
            echo "Pull Request created with number $pr_number"
          else
            echo "PR already exists. Skipping creation."
            pr_number=$pr_exists
          fi

          # Output the PR number for later steps
          echo "pr_number=$pr_number" >> $GITHUB_ENV

      - name: Merge PR into Develop
        env:
          GITHUB_TOKEN: ${{ secrets.DEV_TO_MASTER_SYNC }}
        run: |
          echo "PR Number: ${{ env.pr_number }}"
          if [ -n "${{ env.pr_number }}" ] && [ "${{ env.pr_number }}" != "null" ]; then
            gh pr merge ${{ env.pr_number }} --rebase
            echo "PR merged into develop"
          else
            echo "No PR to merge"
          fi
发布评论

评论列表(0)

  1. 暂无评论