|
| 1 | +name: Auto Merge Main into PR (Only if Main was Updated) |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + pull_request: |
| 8 | + types: [opened, synchronize, reopened] |
| 9 | + |
| 10 | +jobs: |
| 11 | + auto-merge: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout PR branch |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + ref: ${{ github.head_ref }} |
| 19 | + |
| 20 | + - name: Fetch the latest main branch |
| 21 | + run: | |
| 22 | + git fetch origin main |
| 23 | +
|
| 24 | + - name: Get the commit hash of main at the time the PR was created |
| 25 | + id: pr_created |
| 26 | + run: | |
| 27 | + PR_CREATED_COMMIT=$(git log --merges --pretty=format:'%H' origin/main..${{ github.event.pull_request.head.sha }} | tail -n 1) |
| 28 | + echo "PR_CREATED_COMMIT=$PR_CREATED_COMMIT" >> $GITHUB_ENV |
| 29 | +
|
| 30 | + - name: Check if main was updated since PR was created |
| 31 | + id: check_main_update |
| 32 | + run: | |
| 33 | + # Get the latest commit hash from the main branch |
| 34 | + LATEST_MAIN_COMMIT=$(git rev-parse origin/main) |
| 35 | + |
| 36 | + # Compare the commit hashes |
| 37 | + if [ "$PR_CREATED_COMMIT" != "$LATEST_MAIN_COMMIT" ]; then |
| 38 | + echo "Main has been updated since PR creation" |
| 39 | + echo "MAIN_UPDATED=true" >> $GITHUB_ENV |
| 40 | + else |
| 41 | + echo "Main has not been updated since PR creation" |
| 42 | + echo "MAIN_UPDATED=false" >> $GITHUB_ENV |
| 43 | + fi |
| 44 | + |
| 45 | + - name: Check if main is already merged into PR |
| 46 | + id: check_merge |
| 47 | + run: | |
| 48 | + # Check if the commit hash of the latest main commit is already in the PR's history |
| 49 | + MERGE_COMMIT=$(git log --oneline --merges | grep -m 1 "Merge branch 'main' into" || true) |
| 50 | + |
| 51 | + if [[ -z "$MERGE_COMMIT" ]]; then |
| 52 | + echo "Main has not been merged into the PR branch yet" |
| 53 | + echo "MERGE_DONE=false" >> $GITHUB_ENV |
| 54 | + else |
| 55 | + echo "Main has already been merged into the PR branch" |
| 56 | + echo "MERGE_DONE=true" >> $GITHUB_ENV |
| 57 | + fi |
| 58 | + |
| 59 | + - name: Attempt to merge main into PR branch (if main was updated and not merged yet) |
| 60 | + if: env.MAIN_UPDATED == 'true' && env.MERGE_DONE == 'false' |
| 61 | + run: | |
| 62 | + # Set user info for the merge commit, ensuring dependabot[bot] or another user is identified |
| 63 | + # git config --global user.name "GitHub Actions" |
| 64 | + # git config --global user.email "[email protected]" |
| 65 | + git config --global user.name "github-actions[bot]" |
| 66 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 67 | +
|
| 68 | +
|
| 69 | + # Attempt to merge the main branch into the PR branch |
| 70 | + git merge --no-ff --no-commit origin/main --allow-unrelated-histories || echo "Merge conflict detected" |
| 71 | + # If merge was successful, commit and push |
| 72 | + if [ $? -eq 0 ]; then |
| 73 | + git commit -m "Auto merge main into PR branch" |
| 74 | + git push |
| 75 | + fi |
| 76 | + env: |
| 77 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + |
| 79 | + - name: Handle merge conflicts and notify the user |
| 80 | + if: failure() && env.MAIN_UPDATED == 'true' && env.MERGE_DONE == 'false' |
| 81 | + run: | |
| 82 | + # Leave a comment on the PR about merge conflicts |
| 83 | + PR_URL="https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" |
| 84 | + curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -d '{"body":"Merge conflicts were detected when attempting to merge `main` into this PR. Please resolve the conflicts manually."}' $PR_URL |
| 85 | + |
| 86 | + # Optionally, print out conflict information for debugging |
| 87 | + echo "Merge conflicts occurred. Please resolve them manually." |
| 88 | +
|
| 89 | + - name: Skip merge if no updates in main or already merged |
| 90 | + if: env.MAIN_UPDATED == 'false' || env.MERGE_DONE == 'true' |
| 91 | + run: | |
| 92 | + if [ "$MAIN_UPDATED" == "false" ]; then |
| 93 | + echo "No new changes in main since the PR was created. Skipping merge." |
| 94 | + fi |
| 95 | + if [ "$MERGE_DONE" == "true" ]; then |
| 96 | + echo "Main has already been merged into the PR branch. Skipping merge." |
| 97 | + fi |
0 commit comments