Skip to content

Commit ac12bf4

Browse files
kimtoreKyrremann
authored andcommitted
document how to set up dependabot with auto-merge (#615)
1 parent 0422806 commit ac12bf4

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Dependabot with auto-merge
2+
3+
[working-with-dependabot]: https://docs.github.com/en/code-security/dependabot/working-with-dependabot
4+
[automating-dependabot]: https://docs.github.com/en/code-security/dependabot/working-with-dependabot/automating-dependabot-with-github-actions
5+
[configure-dependabot-yaml]: https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
6+
[github-cli]: https://cli.github.com/
7+
8+
[Dependabot][working-with-dependabot] is a security tool offered by GitHub.
9+
Dependabot scans your repositories for vulnerabilities and outdated dependencies, and may automatically open pull requests to bump dependency versions.
10+
The sheer volume of pull requests can incur a significant workload on your team, especially if you manage a lot of repositories.
11+
12+
By completing this guide, Dependabot will automatically fix your insecure or outdated dependencies, and the changes will automatically get merged into your main branch.
13+
14+
## Prerequisites
15+
16+
* [GitHub command-line interface][github-cli] installed.
17+
18+
## Enable Dependabot
19+
20+
The contents of this file will depend on your project requirements. Do not use this file as-is.
21+
Please see [dependabot.yaml configuration syntax][configure-dependabot-yaml] for detailed instructions on how to configure Dependabot.
22+
23+
!!! note ".github/dependabot.yaml"
24+
25+
```yaml
26+
version: 2
27+
updates:
28+
- die: &I didn't edit my config file
29+
- package-ecosystem: "github-actions"
30+
directory: "/"
31+
schedule:
32+
interval: "daily"
33+
time: "10:05"
34+
timezone: "Europe/Oslo"
35+
- package-ecosystem: "docker"
36+
directory: "/"
37+
schedule:
38+
interval: "daily"
39+
time: "10:05"
40+
timezone: "Europe/Oslo"
41+
```
42+
43+
## GitHub workflow for auto-merging Dependabot pull requests
44+
45+
This workflow will trigger when dependabot opens a pull request.
46+
All minor and patch-level changes are automatically merged.
47+
Major version bumps needs manual merging.
48+
Additionally, all GitHub Actions workflow version bumps will be merged automatically, even if they are major bumps.
49+
50+
See also [Automating Dependabot with GitHub Actions][automating-dependabot].
51+
52+
!!! note ".github/workflows/dependabot-auto-merge.yaml"
53+
54+
```yaml
55+
name: Dependabot auto-merge
56+
on: pull_request
57+
58+
permissions:
59+
contents: write
60+
pull-requests: write
61+
62+
jobs:
63+
dependabot:
64+
runs-on: ubuntu-latest
65+
if: ${{ github.actor == 'dependabot[bot]' }}
66+
steps:
67+
- name: Dependabot metadata
68+
id: metadata
69+
uses: dependabot/fetch-metadata@v1
70+
with:
71+
github-token: "${{ secrets.GITHUB_TOKEN }}"
72+
- name: Auto-merge changes from Dependabot
73+
if: steps.metadata.outputs.update-type != 'version-update:semver-major' || steps.metadata.outputs.package-ecosystem == 'github_actions'
74+
run: gh pr merge --auto --squash "$PR_URL"
75+
env:
76+
PR_URL: ${{github.event.pull_request.html_url}}
77+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
78+
```
79+
80+
## Enable branch protection and auto-merge on repository
81+
82+
Change working directory to your git repository, then run this script.
83+
Otherwise, the workflow above might not work as expected.
84+
85+
If you prefer, you can instead use GitHub's web frontend to configure auto-merge and branch protection. See GitHub docs for
86+
[enable auto-merge](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/configuring-pull-request-merges/managing-auto-merge-for-pull-requests-in-your-repository)
87+
and
88+
[branch protection rules](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/managing-a-branch-protection-rule).
89+
90+
!!! note "enforce_branch_protection.sh"
91+
92+
```bash
93+
#!/bin/bash
94+
# adapted from https://github.com/navikt/dagpenger/blob/master/bin/enforce_branch_protection.sh
95+
96+
# Get the current repository information
97+
repo_url=$(git remote get-url origin)
98+
repo_name=$(basename -s .git "$repo_url")
99+
owner=$(echo "$repo_url" | awk -F"(/|:)" '{print $2}')
100+
101+
# Determine the name of the main branch
102+
main_branch=$(git symbolic-ref --short HEAD 2>/dev/null || git branch -l --no-color | grep -E '^[*]' | sed 's/^[* ] //')
103+
104+
# Configure branch protection, and require tests to pass before merging.
105+
# Match the list of checks up against repository workflows.
106+
echo '{ "required_status_checks": { "strict": true, "checks": [ { "context": "test" } ] }, "enforce_admins": false, "required_pull_request_reviews": null, "required_conversation_resolution": true, "restrictions": null }' | \
107+
gh api repos/"$owner"/"$repo_name"/branches/"$main_branch"/protection \
108+
--method PUT \
109+
--silent \
110+
--header "Accept: application/vnd.github.v3+json" \
111+
--input -
112+
113+
# Enable auto-merge on repository
114+
echo '{ "allow_auto_merge": true, "delete_branch_on_merge": true }' | gh api repos/"$owner"/"$repo_name" \
115+
--method PATCH \
116+
--silent \
117+
--header "Accept: application/vnd.github.v3+json" \
118+
--input -
119+
120+
if [ $? -eq 0 ]; then
121+
echo "Branch protection configured for $owner/$repo_name on branch $main_branch"
122+
else
123+
echo "Failed to configure branch protection for $owner/$repo_name on branch $main_branch"
124+
fi
125+
```

0 commit comments

Comments
 (0)