メモ
この記事では、GitHub Actionsを使ってDependabot 関連のタスクを自動化する方法についてを説明します。 GitHub Actions を使って Dependabot updates を実行する方法の詳細については、代わりに「GitHub Actions ランナーの Dependabot について」を参照してください。
GitHub Actions を使うと、依存関係を更新する pull request が Dependabot によって作成されたときに自動タスクを実行することができます。 これは次のような場合に便利です。
-
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 ワークフローをトリガーできます。ただし、特定のイベントは異なる方法で処理されます。詳細については、「Troubleshooting Dependabot on GitHub Actions」を参照してください。
ここでは、GitHub Actions を使って自動化できる pull request の一般的なシナリオをいくつか示します。
Pull request に関するメタデータをフェッチする
ほとんどの自動化では、pull request の内容に関する情報 (依存関係の名前、運用依存関係かどうか、更新プログラムがメジャー、マイナー、パッチのいずれであるか) を把握する必要があります。 アクションを使うと、Dependabot で生成された pull request によって更新された依存関係に関する情報を取得できます。
例:
{% data reusables.actions.actions-not-certified-by-github-comment %}
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
{% data reusables.actions.actions-not-certified-by-github-comment %}
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 ラベルに基づく他の自動化またはトリアージ ワークフローがある場合は、提供されたメタデータに基づいてラベルを割り当てるアクションを構成できます。
すべての運用依存関係の更新にラベルを付ける例:
{% data reusables.actions.actions-not-certified-by-github-comment %}
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}}
{% data reusables.actions.actions-not-certified-by-github-comment %}
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 を自動的に承認できます。
例:
{% data reusables.actions.actions-not-certified-by-github-comment %}
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}}
{% data reusables.actions.actions-not-certified-by-github-comment %}
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 に自動マージする例を次に示します。
{% data reusables.actions.actions-not-certified-by-github-comment %}
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}}
{% data reusables.actions.actions-not-certified-by-github-comment %}
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 について」をご覧ください。
失敗したワークフロー実行を調査する
ワークフローの実行が失敗した場合は、以下をチェックしてください。
- 適切なアクターがトリガーした場合にのみワークフローを実行しているか。
-
`ref` に対する正しい `pull_request` をチェックアウトしています。 - シークレットは、GitHub Actions シークレットとしてではなく、Dependabot シークレットで使用できます。
- 適切なアクセス許可を持つ
GITHUB_TOKENがあります。
GitHub Actions の作成とデバッグに関する情報については、「ワークフローの書き込み」を参照してください。
ワークフローに関する issue を解決するためのその他のヒントについては、「Troubleshooting Dependabot on GitHub Actions」を参照してください。