|
| 1 | +name: squash-merge-into-dev |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + pr_number: |
| 7 | + description: "Pull Request Number (e.g. 123)" |
| 8 | + required: true |
| 9 | + |
| 10 | +jobs: |
| 11 | + squash-merge: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + # Checkout repository |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + token: ${{ secrets.PAT }} |
| 21 | + |
| 22 | + # Configure Git |
| 23 | + - name: Configure Git |
| 24 | + run: | |
| 25 | + git config --global user.name "GitHub Actions" |
| 26 | + git config --global user.email "actions@github.com" |
| 27 | +
|
| 28 | + # Fetch PR branch info |
| 29 | + - name: Get PR branch name |
| 30 | + id: pr_info |
| 31 | + run: | |
| 32 | + BRANCH_NAME=$(gh pr view ${{ github.event.inputs.pr_number }} --json headRefName -q ".headRefName") |
| 33 | + echo "branch_name=$BRANCH_NAME" >> $GITHUB_ENV |
| 34 | + env: |
| 35 | + GH_TOKEN: ${{ secrets.PAT }} |
| 36 | + |
| 37 | + # Check for duplicate commit in dev |
| 38 | + - name: Check if commit message already exists in dev |
| 39 | + id: check_commit |
| 40 | + run: | |
| 41 | + git fetch origin dev |
| 42 | + git checkout dev |
| 43 | + COMMIT_MSG="from ${{ env.branch_name }}" |
| 44 | + if git log --pretty=format:"%s" origin/dev | grep -q "^$COMMIT_MSG$"; then |
| 45 | + echo "duplicate=true" >> $GITHUB_OUTPUT |
| 46 | + else |
| 47 | + echo "duplicate=false" >> $GITHUB_OUTPUT |
| 48 | + fi |
| 49 | +
|
| 50 | + # Trigger revert branch workflow if duplicate found |
| 51 | + - name: Trigger revert workflow |
| 52 | + if: steps.check_commit.outputs.duplicate == 'true' |
| 53 | + uses: peter-evans/workflow-dispatch@v2 |
| 54 | + with: |
| 55 | + token: ${{ secrets.PAT }} |
| 56 | + repository: ${{ github.repository }} |
| 57 | + workflow: manual-revert-workflow.yml |
| 58 | + ref: dev |
| 59 | + inputs: |
| 60 | + branch_name: ${{ env.branch_name }} |
| 61 | + |
| 62 | + # Wait for revert workflow to finish |
| 63 | + - name: Wait for revert to complete |
| 64 | + if: steps.check_commit.outputs.duplicate == 'true' |
| 65 | + run: | |
| 66 | + echo "Waiting for revert workflow to finish..." |
| 67 | + sleep 30 |
| 68 | +
|
| 69 | + # Retry squash merge after revert OR first attempt |
| 70 | + - name: Squash merge PR into dev |
| 71 | + id: merge |
| 72 | + run: | |
| 73 | + git fetch origin |
| 74 | + git checkout dev |
| 75 | + git pull origin dev |
| 76 | +
|
| 77 | + # Fetch PR branch |
| 78 | + git fetch origin ${{ env.branch_name }} |
| 79 | +
|
| 80 | + set +e |
| 81 | + git merge origin/${{ env.branch_name }} --squash |
| 82 | + STATUS=$? |
| 83 | + set -e |
| 84 | +
|
| 85 | + if [ $STATUS -ne 0 ]; then |
| 86 | + echo "Merge conflict detected!" |
| 87 | + echo "conflict=true" >> $GITHUB_OUTPUT |
| 88 | + exit 0 |
| 89 | + fi |
| 90 | +
|
| 91 | + git commit -m "from ${{ env.branch_name }}" |
| 92 | + git push origin dev |
| 93 | + echo "conflict=false" >> $GITHUB_OUTPUT |
| 94 | +
|
| 95 | + # Fail workflow if merge conflict detected |
| 96 | + - name: Fail on conflict |
| 97 | + if: steps.merge.outputs.conflict == 'true' |
| 98 | + run: | |
| 99 | + echo "❌ Merge conflict detected while merging ${{ env.branch_name }} into dev." |
| 100 | + echo "Please create a PR and resolve conflicts manually." |
| 101 | + exit 1 |
| 102 | +
|
| 103 | + # Comment on PR after successful merge |
| 104 | + - name: Comment on PR |
| 105 | + if: steps.merge.outputs.conflict == 'false' |
| 106 | + run: | |
| 107 | + gh pr comment ${{ github.event.inputs.pr_number }} --body "✅ Successfully squash merged into dev with commit: from ${{ env.branch_name }}" |
| 108 | + env: |
| 109 | + GH_TOKEN: ${{ secrets.PAT }} |
0 commit comments