Dependency updates are the chore nobody wants and everybody needs. In a typical web application, Dependabot opens a steady stream of pull requests across several ecosystems—in our case, Composer, npm, and GitHub Actions. Most are patch or minor updates that pass CI and get merged without a second glance. Clicking “Approve” and “Merge” on each one is pure overhead.
So we automated it. This post walks through the 35-line workflow that approves and auto-merges low-risk Dependabot PRs, and the safeguards that make it reasonable to trust.
The setup
Dependabot is configured to check each ecosystem weekly and target the develop branch:
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
target-branch: "develop"
schedule:
interval: "weekly"
# ...same for "npm" and "composer"
In this setup, merging into develop deploys to staging, and main is promoted to production. That matters: an auto-merged dependency update lands on staging, not directly in production.
The workflow
name: Dependabot Auto-Approve and Merge
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: write
pull-requests: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.actor == 'dependabot[bot]'
steps:
- name: Fetch Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v2
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Approve patch and minor updates
if: steps.metadata.outputs.update-type == 'version-update:semver-patch' || steps.metadata.outputs.update-type == 'version-update:semver-minor'
run: |
gh pr review "$PR_URL" --approve
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Enable auto-merge
if: steps.metadata.outputs.update-type == 'version-update:semver-patch' || steps.metadata.outputs.update-type == 'version-update:semver-minor'
run: |
gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Trigger
The workflow runs on pull_request events for opened, synchronize, and reopened. That covers a new Dependabot PR, a force-push when Dependabot rebases it, and a PR that someone closes and reopens.
Permissions
The default GITHUB_TOKEN is scoped to what the job needs: contents: write (required to merge) and pull-requests: write (required to approve and enable auto-merge). Nothing else is granted.
Only run for Dependabot
if: github.actor == 'dependabot[bot]'
This job-level guard means human PRs never run these steps. The job is skipped entirely, so the approval logic cannot be triggered by someone else's PR.
Classifying the update
uses: dependabot/fetch-metadata@v2
The official dependabot/fetch-metadata action parses the PR and exposes outputs such as dependency-names, package-ecosystem, and, most importantly, update-type. That output is one of:
version-update:semver-patchversion-update:semver-minorversion-update:semver-major
This is the policy lever. We use the semver level to determine whether a human needs to review the update.
Approve and auto-merge patch and minor updates
Both remaining steps share the same condition: the update type must be patch or minor. Major updates skip both steps. The PR stays open and unapproved, waiting for someone to read the changelog.
Step one approves the PR with gh pr review --approve. Step two runs gh pr merge --auto --merge.
The --auto option does not merge immediately. It tells GitHub to merge the PR once all branch protection requirements are met. That means the full CI suite: unit tests, static analysis, linters, formatters, type checks, and the production build. If any check fails, the PR waits while someone investigates. The workflow removes the manual clicking, not the safety net.
Passing the PR URL through env
The PR URL goes through an environment variable rather than being interpolated directly into the shell script:
env:
PR_URL: ${{ github.event.pull_request.html_url }}
This is the recommended pattern for avoiding script injection in Actions. The ${{ }} expression is evaluated before the shell runs, so interpolating untrusted values directly into run: can be exploited. Passing the value through env and quoting "$PR_URL" in the script keeps it as data. The URL here is GitHub-generated, so the risk is low, but it’s a good habit.
Prerequisites
For this to work, the repository needs a few settings:
- Allow auto-merge must be enabled in the repository settings. Without it,
gh pr merge --autofails. - Branch protection on the target branch with required status checks. Auto-merge waits for these. Without them, a PR could merge immediately.
- Required approvals (optional). If you require one review, the bot's approval satisfies it. Be deliberate about this, since it means the bot can approve this category of PRs.
- Allow GitHub Actions to approve pull requests must be enabled under Settings → Actions → General.
Trade-offs and limitations
- Semver is a promise, not a guarantee. Maintainers sometimes break things in minor releases, and 0.x packages treat minor updates as breaking. We rely on our test suite to catch this. Where coverage is thin, the risk is real.
- Staging is the backstop. Because merges land on
developand deploy to staging first, a bad update is caught before it reaches production. - Supply-chain risk remains. Auto-merging reduces human review of new upstream code. A compromised dependency release would pass through automatically if it passes CI. If that concerns you, consider a cooldown period in
dependabot.yml, restricting auto-merge to development dependencies, or grouping updates. - Pinning to
@v2. The workflow references a mutable tag. For stricter supply-chain hygiene, pin actions to a commit SHA. Dependabot can keep the SHA up to date for you. - Majors are still manual. That is by design, but major updates can pile up if nobody reviews them.
Possible improvements
- Filter by
dependency-type(direct:productionvs.direct:development) to auto-merge only development dependencies. - Use
dependabot/fetch-metadataoutputs to exclude specific packages, such as the framework itself. - Add
groupsindependabot.ymlto reduce PR volume. - Merge the two conditional steps into one by using a job-level
ifon the update type.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.