From d66da8eb70b2ff9cf671398df468b74f64e32134 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Sat, 25 Oct 2025 19:45:31 -0700 Subject: [PATCH] ci: add cherry-picking to the main branch (#5583) --- .github/workflows/cherry-pick-to-main.yml | 93 +++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 .github/workflows/cherry-pick-to-main.yml diff --git a/.github/workflows/cherry-pick-to-main.yml b/.github/workflows/cherry-pick-to-main.yml new file mode 100644 index 0000000000..92f37a0840 --- /dev/null +++ b/.github/workflows/cherry-pick-to-main.yml @@ -0,0 +1,93 @@ +name: Cherry-pick to Main + +on: + pull_request: + types: [closed] + branches: + - canary + +jobs: + cherry-pick: + if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'merge-to-main') + runs-on: ubuntu-latest + + permissions: + contents: write + pull-requests: write + issues: write + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Configure Git + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + + - name: Cherry-pick to main + id: cherry-pick + run: | + # Fetch all branches + git fetch origin main:main + git fetch origin canary:canary + + # Get the merge commit SHA + MERGE_COMMIT="${{ github.event.pull_request.merge_commit_sha }}" + echo "Merge commit: $MERGE_COMMIT" + + # Checkout main branch + git checkout main + + # Attempt cherry-pick + if git cherry-pick -m 1 "$MERGE_COMMIT"; then + echo "status=success" >> $GITHUB_OUTPUT + echo "Cherry-pick successful!" + + # Push to main + git push origin main + echo "Pushed to main successfully!" + else + echo "status=conflict" >> $GITHUB_OUTPUT + echo "conflict=true" >> $GITHUB_OUTPUT + + # Get conflict details + CONFLICTS=$(git diff --name-only --diff-filter=U | tr '\n' ',' | sed 's/,$//') + echo "conflicts=$CONFLICTS" >> $GITHUB_OUTPUT + + # Abort the cherry-pick + git cherry-pick --abort + + echo "Cherry-pick failed due to conflicts" + exit 1 + fi + + - name: Comment on PR (Success) + if: steps.cherry-pick.outputs.status == 'success' + uses: actions/github-script@v7 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: '✅ Successfully cherry-picked to `main` branch!\n\nCommit: ${{ github.event.pull_request.merge_commit_sha }}' + }) + + - name: Comment on PR (Conflict) + if: failure() && steps.cherry-pick.outputs.conflict == 'true' + uses: actions/github-script@v7 + with: + script: | + const conflicts = '${{ steps.cherry-pick.outputs.conflicts }}'; + const conflictFiles = conflicts ? conflicts.split(',').map(f => `- \`${f}\``).join('\n') : 'Unknown files'; + + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `⚠️ **Cherry-pick to \`main\` failed due to conflicts!**\n\nCommit: ${{ github.event.pull_request.merge_commit_sha }}\n\n**Conflicting files:**\n${conflictFiles}\n\nPlease manually cherry-pick this commit to the \`main\` branch and resolve the conflicts.` + })