Skip to content

Commit bee9bcb

Browse files
committed
ci: auto-merge dependabot PRs after CI passes with auto-approval
1 parent d6514c3 commit bee9bcb

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
name: Dependabot Auto-Merge
22

33
on:
4-
pull_request:
5-
branches: [ main ]
4+
workflow_run:
5+
workflows: ["CI"]
6+
types: [completed]
67

78
permissions:
89
contents: write
910
pull-requests: write
1011

1112
jobs:
1213
automerge:
13-
name: Enable auto-merge for Dependabot PRs
14+
name: Auto-merge Dependabot PRs after CI passes
1415
runs-on: ubuntu-latest
15-
if: github.actor == 'dependabot[bot]'
16+
if: github.event.workflow_run.conclusion == 'success'
1617
steps:
17-
- name: Enable auto-merge
18-
run: gh pr merge --auto --squash "$PR_URL"
18+
- name: Find associated PR
19+
id: find-pr
1920
env:
20-
PR_URL: ${{ github.event.pull_request.html_url }}
2121
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

Comments
 (0)