はじめに
依存パッケージの更新、面倒ですよね。
Dependabotを導入すれば自動でPRを作ってくれますが、結局マージは手動…という状態になっていませんか?
この記事では、Dependabotの導入から CIが通ったら自動でマージする ところまでを完全自動化する方法を解説します。
全体像
Dependabot → PR作成 → CI実行 → 成功 → 自動マージ
↓
失敗 → 手動対応
設定が完了すると、以下が自動で行われます:
- 毎週月曜日にDependabotが依存関係をチェック
- 更新があればPRを自動作成
- CIが実行される
- CIが成功したら自動でマージ
Step 1: Dependabotを有効化する
.github/dependabot.yml を作成します。
Go プロジェクトの場合
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "weekly"
day: "monday"
time: "19:00"
timezone: "Asia/Tokyo"
commit-message:
prefix: "deps"
labels:
- "dependencies"
- "go"
open-pull-requests-limit: 5
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
day: "monday"
time: "19:00"
timezone: "Asia/Tokyo"
commit-message:
prefix: "ci"
labels:
- "dependencies"
- "github-actions"
Node.js プロジェクトの場合
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
day: "monday"
time: "19:00"
timezone: "Asia/Tokyo"
commit-message:
prefix: "deps"
labels:
- "dependencies"
- "npm"
open-pull-requests-limit: 5
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
day: "monday"
time: "19:00"
timezone: "Asia/Tokyo"
commit-message:
prefix: "ci"
labels:
- "dependencies"
- "github-actions"
設定のポイント
| 設定項目 | 説明 |
|---|---|
interval |
daily, weekly, monthly から選択 |
day / time |
更新チェックの曜日と時刻 |
timezone |
タイムゾーン |
commit-message.prefix |
コミットメッセージの接頭辞 |
labels |
PRに付けるラベル |
open-pull-requests-limit |
同時に開くPRの上限 |
Step 2: CIワークフローを用意する
自動マージの前提として、CIが必要です。
.github/workflows/ci.yml の例:
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Test
run: go test ./...
- name: Build
run: go build ./...
Step 3: 自動マージを設定する
ここがポイントです。CIが成功したらDependabotのPRを自動マージするワークフローを作成します。
.github/workflows/auto-merge-deps.yml:
name: Auto-merge Dependabot PRs
on:
workflow_run:
workflows: ["CI"]
types: [completed]
permissions:
contents: write
pull-requests: write
jobs:
auto-merge:
if: >
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Get PR
id: pr
uses: actions/github-script@v7
with:
script: |
const prs = context.payload.workflow_run.pull_requests;
if (!prs || prs.length === 0) {
core.setFailed('No PR found for workflow_run');
return;
}
const pr = prs[0];
core.setOutput('url', pr.html_url);
core.setOutput('number', pr.number);
- name: Ensure Dependabot PR
uses: actions/github-script@v7
with:
script: |
const prNumber = ${{ steps.pr.outputs.number }};
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
if (pr.user.login !== 'dependabot[bot]') {
core.setFailed(`Not a Dependabot PR: ${pr.user.login}`);
}
- name: Enable auto-merge
run: gh pr merge --auto --squash "$PR_URL"
env:
PR_URL: ${{ steps.pr.outputs.url }}
GH_TOKEN: ${{ secrets.DEPENDABOT_AUTO_MERGE_TOKEN }}
なぜ workflow_run を使うのか?
pull_request イベントで直接マージしようとすると、Dependabotのセキュリティ制限でシークレットにアクセスできません。workflow_run を使うことで、CI完了後に別のコンテキストでマージ処理を実行できます。
Step 4: Personal Access Tokenを設定する
自動マージには専用のトークンが必要です。
トークンの作成
- GitHub → Settings → Developer settings → Personal access tokens → Fine-grained tokens
- 「Generate new token」をクリック
- 以下を設定:
- Token name:
dependabot-auto-mergeなど - Repository access: 対象リポジトリを選択
- Permissions:
- Contents: Read and write
- Pull requests: Read and write
- Token name:
シークレットへの登録
- リポジトリの Settings → Secrets and variables → Actions
- 「New repository secret」をクリック
- Name:
DEPENDABOT_AUTO_MERGE_TOKEN - Value: 作成したトークン
Step 5: ブランチ保護ルールの設定(オプション)
より安全に運用するため、ブランチ保護ルールを設定することをおすすめします。
- リポジトリの Settings → Branches
- 「Add branch protection rule」
- 以下を設定:
- Branch name pattern:
main - Require status checks to pass before merging: ✅
- Require branches to be up to date before merging: ✅
- Status checks that are required:
test(CIのjob名)
- Branch name pattern:
これにより、CIが通らないPRはマージされません。
動作確認
設定が完了したら、以下の流れで動作を確認できます:
- Dependabotが作成したPRを確認
- CIが実行されることを確認
- CIが成功後、自動でマージされることを確認
PRのタイムラインに以下のようなログが表示されれば成功です:
dependabot[bot] enabled auto-merge (squash)
github-actions[bot] merged commit xxx into main
トラブルシューティング
自動マージされない場合
- トークンの権限を確認: Contents と Pull requests の Read/Write が必要
- ブランチ保護ルールを確認: 「Allow auto-merge」が有効になっているか
- CIの名前を確認:
workflow_run.workflowsに正しいワークフロー名を指定しているか
Dependabot PRが作成されない場合
.github/dependabot.ymlのパスとファイル名を確認- リポジトリの Insights → Dependency graph → Dependabot でエラーがないか確認
まとめ
- Dependabot:
.github/dependabot.ymlで依存更新のPRを自動作成 - 自動マージ:
workflow_runイベントでCI成功後にマージ - トークン: Fine-grained tokenでセキュアに設定
一度設定すれば、依存関係の更新はほぼ完全に自動化されます。セキュリティアップデートも見逃さなくなるので、ぜひ導入してみてください。

