メモ
この記事では、GitHub Actions を使用して Dependabot 関連のタスクを自動化する方法について説明します。 GitHub Actions を使用して Dependabot updates を実行する方法の詳細については、代わりに「GitHub Actions ランナーの Dependabot について」を参照してください。
GitHub Actions を使用すると、Dependabot が、依存関係を更新する pull request (プル リクエスト) を作成したときに、自動タスクを実行することができます。 これは次のような場合に便利です。
-
Dependabot pull request (バージョン更新プログラムとセキュリティ更新プログラム) が、ラベルや名前など、作業プロセスに適したデータを使って確実に作成されるようにする。
-
Dependabot pull request (バージョン更新プログラムとセキュリティ更新プログラム) をレビュー プロセスに送信するか、自動的にマージするワークフローをトリガーする。
Dependabot及びGitHub Actionsについて
重要
Dependabot は、リポジトリで有効になっている場合は、常に GitHub Actions で実行され、リポジトリまたは Organization レベルでの Actions ポリシー チェックと無効化の両方をバイパスします。 これにより、Dependabot が有効になっているときは、常にセキュリティとバージョンの更新ワークフローが常に実行されるようになります。
依存関係を最新の状態に保つために、Dependabot が pull request (プル リクエスト) を作成します。 GitHub Actions を使用すると、これらの pull request (プル リクエスト) が作成されたときに自動タスクを実行できます。 たとえば、追加の成果物のフェッチ、ラベルの追加、テストの実行、あるいは pull request (プル リクエスト) の変更ができます。
Dependabot は、pull request とコメントで GitHub Actions ワークフローをトリガーできます。ただし、特定のイベントは異なる方法で処理されます。 詳細については、「GitHub Actions の Dependabot のトラブルシューティング」を参照してください。
以下は、GitHub Actions を使用して自動化できる pull request (プル リクエスト) に対応する一般的ないくつかのシナリオです。
pull reqeust (プル リクエスト) に関するメタデータのフェッチ
大半の自動化では、依存関係の名前が何か、それは実働環境の依存関係か、メジャー、マイナー、パッチアップデートのいずれなのかといった、pull request (プル リクエスト) の内容に関する情報を知ることが必要です。 アクションを使用すると、Dependabot によって生成された pull request (プル リクエスト) が更新した依存関係に関する情報を取得できます。
例:
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。
name: Dependabot fetch metadata
on: pull_request
permissions:
pull-requests: write
issues: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
# The following properties are now available:
# - steps.metadata.outputs.dependency-names
# - steps.metadata.outputs.dependency-type
# - steps.metadata.outputs.update-type
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。
name: Dependabot fetch metadata
on: pull_request
permissions:
pull-requests: write
issues: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
# The following properties are now available:
# - steps.metadata.outputs.dependency-names
# - steps.metadata.outputs.dependency-type
# - steps.metadata.outputs.update-type
詳細については、dependabot/fetch-metadata リポジトリを参照してください。
pull request (プル リクエスト) のラベル付け
GitHub ラベルに基づく他の自動化ワークフローやトリアージ ワークフローが存在する場合は、提供されたメタデータに基づいてラベルを割り当てるアクションを構成できます。
すべての運用依存関係の更新にラベルを付ける例:
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。
name: Dependabot auto-label
on: pull_request
permissions:
pull-requests: write
issues: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Add a label for all production dependencies
if: steps.metadata.outputs.dependency-type == 'direct:production'
run: gh pr edit "$PR_URL" --add-label "production"
env:
PR_URL: ${{github.event.pull_request.html_url}}
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。
name: Dependabot auto-label
on: pull_request
permissions:
pull-requests: write
issues: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Add a label for all production dependencies
if: steps.metadata.outputs.dependency-type == 'direct:production'
run: gh pr edit "$PR_URL" --add-label "production"
env:
PR_URL: ${{github.event.pull_request.html_url}}
pull request (プル リクエスト) を自動的に承認
ワークフロー内で GitHub CLI を使用すると、Dependabot pull request (プル リクエスト) を自動的に承認できます。
例:
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。
name: Dependabot auto-approve
on: pull_request
permissions:
pull-requests: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Approve a PR
run: gh pr review --approve "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。
name: Dependabot auto-approve
on: pull_request
permissions:
pull-requests: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Approve a PR
run: gh pr review --approve "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
pull request (プル リクエスト) の自動マージを有効化
メンテナーが自動マージの特定の pull request (プル リクエスト) をマークできるようにする場合は、GitHub の自動マージ機能を使用できます。 これにより、ブランチ保護ルールで必要なすべての必須テストと承認が正常に満たされた場合に、pull request がマージされます。
詳細については、「プルリクエストを自動的にマージする」および「ブランチ保護ルールを管理する」を参照してください。
代わりに、GitHub Actions と GitHub CLI を使用できます。 すべてのパッチ更新プログラムを my-dependency に自動マージする例を次に示します:
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。
name: Dependabot auto-merge
on: pull_request
permissions:
contents: write
pull-requests: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Enable auto-merge for Dependabot PRs
if: contains(steps.metadata.outputs.dependency-names, 'my-dependency') && steps.metadata.outputs.update-type == 'version-update:semver-patch'
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
# このワークフローはGitHubによって認定されていないアクションを使用します。
# それらはサードパーティによって提供され、
# 別個の利用規約、プライバシーポリシー、
# ドキュメントを参照してください。
name: Dependabot auto-merge
on: pull_request
permissions:
contents: write
pull-requests: write
jobs:
dependabot:
runs-on: ubuntu-latest
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Enable auto-merge for Dependabot PRs
if: contains(steps.metadata.outputs.dependency-names, 'my-dependency') && steps.metadata.outputs.update-type == 'version-update:semver-patch'
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
メモ
メモ: pull request (プル リクエスト) のテストにステータス チェックを使用する場合、Dependabot pull request (プル リクエスト) に対応するターゲット ブランチで [Require status checks to pass before merging (マージ前にステータス チェックの合格を必須にする)] をオンにする必要があります。 このブランチ保護ルールを使用すると、必須のステータス チェックすべてに合格しない限り、確実に pull request (プル リクエスト) がマージされなくなります。 詳しくは、「ブランチ保護ルールを管理する」をご覧ください。
Dependabot と、GitHub Actions の各ポリシー
通常、ワークフローがリポジトリ内で実行できるかどうかは、GitHub Actions のポリシー チェックと、GitHub Actions が organization (組織) またはリポジトリのレベルで有効になっているかどうかによって決まります。 これらのコントロールを使用すると、ワークフローの実行を制限できます。特に、外部アクションがブロックされているとき、または GitHub Actions が完全に無効になっている吐合です。
ただし、Dependabot がリポジトリで有効になっている場合は、そのワークフローは常に GitHub Actions 上で実行され、Actions ポリシー チェックと無効化の両方をバイパスします。
- Dependabot ワークフローは、Actions の無効化または Enterprise (エンタープライズ) のポリシー制限によってブロックされることはありません。
- 外部アクションが許可されていない場合であっても、これらのワークフロー内で参照されているアクションも、また実行を許可されます。
詳細については、「GitHub Actions ランナーの Dependabot について」を参照してください。
失敗したワークフロー実行の調査
ワークフローの実行が失敗した場合は、以下をチェックしてください。
- 適切なアクターがトリガーした場合にのみワークフローを実行しているか。
pull_requestに対する正しいrefをチェックアウトしています。- シークレットは、GitHub Actions シークレットとしてではなく、Dependabot シークレットで使用できます。
- 適切なアクセス許可を持つ
GITHUB_TOKENがあります。
GitHub Actions の作成とデバッグに関する情報については、「ワークフローの書き込み」を参照してください。
ワークフローに関する問題を解決するための他のヒントについては、「GitHub Actions の Dependabot のトラブルシューティング」を参照してください。