|
| 1 | +name: Auto-merge forward |
| 2 | + |
| 3 | +# Push to a rel-x.y branch opens a merge PR into the next newer rel-* line, |
| 4 | +# or into dev when this line is the newest. Merge of that PR retriggers the |
| 5 | +# next hop, so a bug-fix on rel-1.0 flows rel-1.0 -> rel-1.1 -> ... -> dev. |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - 'rel-*' |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +concurrency: |
| 13 | + group: auto-merge-forward-${{ github.ref_name }} |
| 14 | + cancel-in-progress: false |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + |
| 19 | +jobs: |
| 20 | + forward: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: write |
| 24 | + pull-requests: write |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + fetch-depth: 0 |
| 29 | + |
| 30 | + - name: Resolve forward target |
| 31 | + id: target |
| 32 | + run: | |
| 33 | + set -euo pipefail |
| 34 | + SOURCE="${GITHUB_REF_NAME}" |
| 35 | + if [[ ! "$SOURCE" =~ ^rel-[0-9]+\.[0-9]+$ ]]; then |
| 36 | + echo "Not a rel-x.y branch ($SOURCE); skipping." |
| 37 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 38 | + exit 0 |
| 39 | + fi |
| 40 | +
|
| 41 | + git fetch origin --prune |
| 42 | +
|
| 43 | + mapfile -t RELS < <( |
| 44 | + git ls-remote --heads origin 'rel-*' \ |
| 45 | + | awk '{print $2}' \ |
| 46 | + | sed 's|refs/heads/||' \ |
| 47 | + | grep -E '^rel-[0-9]+\.[0-9]+$' \ |
| 48 | + | sort -t. -k1.5,1n -k2,2n |
| 49 | + ) |
| 50 | +
|
| 51 | + TARGET="dev" |
| 52 | + found=0 |
| 53 | + for branch in "${RELS[@]}"; do |
| 54 | + if [[ "$found" -eq 1 ]]; then |
| 55 | + TARGET="$branch" |
| 56 | + break |
| 57 | + fi |
| 58 | + if [[ "$branch" == "$SOURCE" ]]; then |
| 59 | + found=1 |
| 60 | + fi |
| 61 | + done |
| 62 | +
|
| 63 | + if [[ "$found" -eq 0 ]]; then |
| 64 | + echo "::error::Source branch $SOURCE was not listed among origin rel-* heads." |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +
|
| 68 | + if ! git rev-parse --verify "origin/$TARGET" >/dev/null 2>&1; then |
| 69 | + echo "::error::Target branch origin/$TARGET does not exist." |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +
|
| 73 | + if git merge-base --is-ancestor "origin/$SOURCE" "origin/$TARGET"; then |
| 74 | + echo "origin/$SOURCE is already an ancestor of origin/$TARGET; nothing to forward." |
| 75 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 76 | + exit 0 |
| 77 | + fi |
| 78 | +
|
| 79 | + echo "skip=false" >> "$GITHUB_OUTPUT" |
| 80 | + echo "source=$SOURCE" >> "$GITHUB_OUTPUT" |
| 81 | + echo "target=$TARGET" >> "$GITHUB_OUTPUT" |
| 82 | + echo "Auto-merge forward: $SOURCE -> $TARGET" |
| 83 | +
|
| 84 | + - name: Merge into forward branch |
| 85 | + if: steps.target.outputs.skip != 'true' |
| 86 | + id: merge |
| 87 | + run: | |
| 88 | + set -euo pipefail |
| 89 | + SOURCE="${{ steps.target.outputs.source }}" |
| 90 | + TARGET="${{ steps.target.outputs.target }}" |
| 91 | + FORWARD_BRANCH="auto-merge-forward/${SOURCE}-to-${TARGET}-${{ github.run_number }}" |
| 92 | +
|
| 93 | + git config user.name "github-actions[bot]" |
| 94 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 95 | +
|
| 96 | + git checkout -B "$FORWARD_BRANCH" "origin/$TARGET" |
| 97 | + if git merge --no-edit "origin/$SOURCE"; then |
| 98 | + echo "conflict=false" >> "$GITHUB_OUTPUT" |
| 99 | + else |
| 100 | + git merge --abort |
| 101 | + git checkout -B "$FORWARD_BRANCH" "origin/$SOURCE" |
| 102 | + echo "conflict=true" >> "$GITHUB_OUTPUT" |
| 103 | + echo "::warning::Merge conflict forwarding ${SOURCE} to ${TARGET}. PR left open for manual resolution." |
| 104 | + fi |
| 105 | +
|
| 106 | + git push origin "$FORWARD_BRANCH" |
| 107 | + echo "branch=$FORWARD_BRANCH" >> "$GITHUB_OUTPUT" |
| 108 | +
|
| 109 | + - name: Create pull request |
| 110 | + if: steps.target.outputs.skip != 'true' |
| 111 | + id: pr |
| 112 | + env: |
| 113 | + GH_TOKEN: ${{ github.token }} |
| 114 | + run: | |
| 115 | + set -euo pipefail |
| 116 | + SOURCE="${{ steps.target.outputs.source }}" |
| 117 | + TARGET="${{ steps.target.outputs.target }}" |
| 118 | + FORWARD_BRANCH="${{ steps.merge.outputs.branch }}" |
| 119 | + CONFLICT="${{ steps.merge.outputs.conflict }}" |
| 120 | +
|
| 121 | + BODY="Automated forward merge of \`${SOURCE}\` into \`${TARGET}\`." |
| 122 | + if [[ "$CONFLICT" == "true" ]]; then |
| 123 | + BODY+=$'\n\n**Merge conflict:** this branch is \`${SOURCE}\` as-is. Resolve against \`${TARGET}\` before merging.' |
| 124 | + fi |
| 125 | +
|
| 126 | + URL="$(gh pr create \ |
| 127 | + --base "$TARGET" \ |
| 128 | + --head "$FORWARD_BRANCH" \ |
| 129 | + --title "Auto-merge forward ${SOURCE} → ${TARGET}" \ |
| 130 | + --body "$BODY")" |
| 131 | + echo "url=$URL" >> "$GITHUB_OUTPUT" |
| 132 | + echo "Created $URL" |
| 133 | +
|
| 134 | + # BOT_SECRET, not github.token: a merge performed with the default token produces a push |
| 135 | + # that triggers no workflow, which would stop the chain at the first hop. |
| 136 | + - name: Approve and auto-merge |
| 137 | + if: steps.target.outputs.skip != 'true' && steps.merge.outputs.conflict != 'true' |
| 138 | + env: |
| 139 | + GH_TOKEN: ${{ secrets.BOT_SECRET }} |
| 140 | + run: | |
| 141 | + set -euo pipefail |
| 142 | + FORWARD_BRANCH="${{ steps.merge.outputs.branch }}" |
| 143 | + gh pr review "$FORWARD_BRANCH" --approve |
| 144 | + gh pr merge "$FORWARD_BRANCH" --merge --auto --delete-branch |
0 commit comments