|
1 | 1 | name: Dependabot Auto-Merge |
2 | 2 |
|
3 | 3 | on: |
4 | | - pull_request: |
5 | | - branches: [ main ] |
| 4 | + workflow_run: |
| 5 | + workflows: ["CI"] |
| 6 | + types: [completed] |
6 | 7 |
|
7 | 8 | permissions: |
8 | 9 | contents: write |
9 | 10 | pull-requests: write |
10 | 11 |
|
11 | 12 | jobs: |
12 | 13 | automerge: |
13 | | - name: Enable auto-merge for Dependabot PRs |
| 14 | + name: Auto-merge Dependabot PRs after CI passes |
14 | 15 | runs-on: ubuntu-latest |
15 | | - if: github.actor == 'dependabot[bot]' |
| 16 | + if: github.event.workflow_run.conclusion == 'success' |
16 | 17 | steps: |
17 | | - - name: Enable auto-merge |
18 | | - run: gh pr merge --auto --squash "$PR_URL" |
| 18 | + - name: Find associated PR |
| 19 | + id: find-pr |
19 | 20 | env: |
20 | | - PR_URL: ${{ github.event.pull_request.html_url }} |
21 | 21 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + REPO: ${{ github.repository }} |
| 23 | + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
| 24 | + run: | |
| 25 | + PR=$(gh api "repos/$REPO/commits/$HEAD_SHA/pulls" \ |
| 26 | + -H "Accept: application/vnd.github+json" \ |
| 27 | + --jq '.[0] | {number: .number, actor: .user.login}') |
| 28 | + NUMBER=$(echo "$PR" | jq -r '.number // empty') |
| 29 | + ACTOR=$(echo "$PR" | jq -r '.actor // empty') |
| 30 | + echo "pr_number=$NUMBER" >> "$GITHUB_OUTPUT" |
| 31 | + echo "actor=$ACTOR" >> "$GITHUB_OUTPUT" |
| 32 | +
|
| 33 | + - name: Approve and merge if Dependabot PR |
| 34 | + if: steps.find-pr.outputs.actor == 'dependabot[bot]' && steps.find-pr.outputs.pr_number != '' |
| 35 | + env: |
| 36 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + REPO: ${{ github.repository }} |
| 38 | + PR_NUMBER: ${{ steps.find-pr.outputs.pr_number }} |
| 39 | + run: | |
| 40 | + gh pr review "$PR_NUMBER" --approve --repo "$REPO" |
| 41 | + gh pr merge "$PR_NUMBER" --squash --repo "$REPO" |
0 commit comments