|
| 1 | +name: Sync main → develop |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [closed] |
| 6 | + branches: [main] |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + head_branch: |
| 10 | + description: "Branch to merge FROM (default: main)." |
| 11 | + required: false |
| 12 | + default: "main" |
| 13 | + base_branch: |
| 14 | + description: "Branch to merge INTO (default: develop)." |
| 15 | + required: false |
| 16 | + default: "develop" |
| 17 | + source_pr_number: |
| 18 | + description: "If testing, the PR number to comment on (optional)." |
| 19 | + required: false |
| 20 | + test_mode: |
| 21 | + description: "Bypass PR/push guards for manual testing" |
| 22 | + required: false |
| 23 | + default: "true" |
| 24 | + |
| 25 | +permissions: |
| 26 | + contents: write |
| 27 | + pull-requests: write |
| 28 | + issues: write |
| 29 | + |
| 30 | +concurrency: |
| 31 | + group: sync-main-into-develop |
| 32 | + cancel-in-progress: false |
| 33 | + |
| 34 | +jobs: |
| 35 | + open-sync-pr: |
| 36 | + if: | |
| 37 | + github.actor != 'github-actions[bot]' && ( |
| 38 | + ( |
| 39 | + github.event_name == 'pull_request' && github.event.pull_request.merged == true |
| 40 | + ) || ( |
| 41 | + github.event_name == 'workflow_dispatch' && (inputs.test_mode == 'true') |
| 42 | + ) |
| 43 | + ) |
| 44 | + runs-on: ubuntu-latest |
| 45 | + |
| 46 | + env: |
| 47 | + # Use inputs for dispatch (testing), defaults for normal triggers |
| 48 | + HEAD_BRANCH: ${{ (github.event_name == 'workflow_dispatch' && inputs.head_branch) || 'main' }} |
| 49 | + BASE_BRANCH: ${{ (github.event_name == 'workflow_dispatch' && inputs.base_branch) || 'develop' }} |
| 50 | + SOURCE_PR: ${{ (github.event_name == 'pull_request' && github.event.pull_request.number) || inputs.source_pr_number || '' }} |
| 51 | + |
| 52 | + steps: |
| 53 | + - uses: actions/checkout@v4 |
| 54 | + with: |
| 55 | + fetch-depth: 0 |
| 56 | + |
| 57 | + # Generate branch name from PR# when available, otherwise use first 7 commit SHA characters |
| 58 | + - name: Compute branch/name metadata |
| 59 | + id: meta |
| 60 | + run: | |
| 61 | + if [ -n "${SOURCE_PR}" ]; then |
| 62 | + echo "branch=sync/${HEAD_BRANCH}-into-${BASE_BRANCH}-pr-${SOURCE_PR}" >> $GITHUB_OUTPUT |
| 63 | + echo "title=Sync ${HEAD_BRANCH} → ${BASE_BRANCH} (PR #${SOURCE_PR})" >> $GITHUB_OUTPUT |
| 64 | + echo "body=Auto-opened to merge \`${HEAD_BRANCH}\` into \`${BASE_BRANCH}\`. Source PR: #${SOURCE_PR}." >> $GITHUB_OUTPUT |
| 65 | + else |
| 66 | + short_sha=${GITHUB_SHA::7} |
| 67 | + echo "branch=sync/${HEAD_BRANCH}-into-${BASE_BRANCH}-${short_sha}" >> $GITHUB_OUTPUT |
| 68 | + echo "title=Sync ${HEAD_BRANCH} → ${BASE_BRANCH} (${short_sha})" >> $GITHUB_OUTPUT |
| 69 | + echo "body=Auto-opened to merge \`${HEAD_BRANCH}\` into \`${BASE_BRANCH}\` at \`${GITHUB_SHA}\`." >> $GITHUB_OUTPUT |
| 70 | + fi |
| 71 | +
|
| 72 | + # Short-lived sync branch from develop and merge main into it (do NOT rebase) |
| 73 | + # use +e to stop errors from short-circuiting the script |
| 74 | + - name: Prepare sync branch |
| 75 | + id: prep |
| 76 | + run: | |
| 77 | + git fetch origin "${BASE_BRANCH}" "${HEAD_BRANCH}" |
| 78 | + git switch -c "${{ steps.meta.outputs.branch }}" "origin/${BASE_BRANCH}" |
| 79 | + set +e |
| 80 | + git merge --no-ff "origin/${HEAD_BRANCH}" |
| 81 | + rc=$? |
| 82 | + set -e |
| 83 | + git add -A || true |
| 84 | + git commit -m "WIP: merge ${HEAD_BRANCH} into ${BASE_BRANCH} via ${{ steps.meta.outputs.branch }}" || true |
| 85 | + git push origin HEAD |
| 86 | + echo "merge_status=$rc" >> "$GITHUB_OUTPUT" |
| 87 | +
|
| 88 | + # Open the PR targeting develop |
| 89 | + - name: Open PR to develop |
| 90 | + id: syncpr |
| 91 | + uses: peter-evans/create-pull-request@v6 |
| 92 | + with: |
| 93 | + branch: ${{ steps.meta.outputs.branch }} |
| 94 | + base: ${{ env.BASE_BRANCH }} |
| 95 | + title: ${{ steps.meta.outputs.title }} |
| 96 | + body: | |
| 97 | + ${{ steps.meta.outputs.body }} |
| 98 | +
|
| 99 | + Merge status: ${{ steps.prep.outputs.merge_status == '0' && 'clean ✅' || 'conflicts ❗' }} |
| 100 | + labels: ${{ steps.prep.outputs.merge_status == '0' && 'back-merge,automation' || 'back-merge,automation,conflicts' }} |
| 101 | + |
| 102 | + # Comment back on the ORIGINAL merged PR with a link to the sync PR |
| 103 | + - name: Comment on source PR with sync PR link |
| 104 | + if: github.event_name == 'pull_request' && steps.syncpr.outputs.pull-request-number != '' |
| 105 | + uses: actions/github-script@v7 |
| 106 | + with: |
| 107 | + script: | |
| 108 | + const issue_number = Number(process.env.SOURCE_PR); |
| 109 | + const syncUrl = `${{ toJson(steps.syncpr.outputs['pull-request-url']) }}`.replace(/^"|"$/g, ''); |
| 110 | + const body = `Opened sync PR **${process.env.HEAD_BRANCH} → ${process.env.BASE_BRANCH}**: ${syncUrl}`; |
| 111 | + await github.rest.issues.createComment({ |
| 112 | + owner: context.repo.owner, |
| 113 | + repo: context.repo.repo, |
| 114 | + issue_number, |
| 115 | + body, |
| 116 | + }); |
0 commit comments