0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dependabotで依存関係管理を次のレベルへ

Posted at

Dependabotで依存関係管理を次のレベルへ

1. はじめに

依存関係の管理は、現代のソフトウェア開発における最も重要な課題の一つです。セキュリティ脆弱性への対応、バージョンアップデートの追跡、そして複数のパッケージマネージャーの管理。これらすべてを手動で行うのは、もはや現実的ではありません。

GitHubのDependabotは、この課題に対する包括的なソリューションを提供します。本記事では、Dependabotの機能を深く掘り下げ、実際の開発フローに統合するための実践的な知識を提供します。

2. Dependabotの3つの柱

Dependabotは、以下の3つの主要機能で構成されています。

2.1. Dependabotアラート

脆弱性のある依存関係を検出し、通知します。GitHub Advisory Databaseをデータソースとして使用し、新しいアドバイザリが追加されるたび、または依存関係グラフに変更があるたびにスキャンを実行します。

重要な特徴:

  • GitHubがレビューしたアドバイザリのみがアラートをトリガー
  • リポジトリレベルでアラートを集約
  • アーカイブされたリポジトリはスキャン対象外

2.2. Dependabotセキュリティアップデート

脆弱な依存関係を自動的に修正するPRを作成します。依存関係グラフを壊すことなく、脆弱性を解決する最小バージョンへのアップデートを試みます。

動作フロー:

  1. Dependabotアラートが発生
  2. 依存関係グラフを分析
  3. 修正可能な場合、PRを自動作成
  4. 修正不可能な場合、アラートにエラーを報告

2.3. Dependabotバージョンアップデート

依存関係を最新バージョンに保ちます。dependabot.ymlファイルで設定したスケジュールに基づいて、定期的にPRを作成します。

3. サポートされているエコシステム

Dependabotは、主要なパッケージマネージャーを幅広くサポートしています。

3.1. 主要なエコシステム一覧

パッケージマネージャー YAML値 サポートバージョン 備考
JavaScript/Node.js
npm npm v7, v8, v9, v10, v11
yarn npm v1, v2, v3, v4 v2以降はvendoring対応
pnpm npm v7, v8, v9, v10
Bun bun >=v1.1.39 テキストベースのbun.lockのみ
Python
pip pip v21.1.2
pipenv pip <= 2021-05-29
pip-compile pip 6.1.0
poetry pip v1
uv uv v0
Ruby
Bundler bundler v2
Java
Maven maven 該当なし
Gradle gradle 該当なし
その他
Docker docker v1
GitHub Actions github-actions 該当なし
Go modules gomod v1
Cargo cargo v1
Terraform terraform >= 0.13, <= 1.13.x

3.2. エコシステム別の特記事項

3.2.1. npm/yarn/pnpm/Bun

.npmrcファイルでプライベートレジストリを設定できます。スコープ付き依存関係(@my-org/my-dep)の場合、プロジェクトの.npmrcにプライベートレジストリを定義する必要があります。

3.2.2. Python

複数のパッケージマネージャーに対応していますが、dependabot.ymlではpipというYAML値を使用します。例えば、poetryを使用している場合でもpackage-ecosystem: "pip"と指定します。

3.2.3. Docker

Semantic Versioningをパースし、プレリリースタグを検出した場合、同じプレリリースラベルを持つ最新バージョンのみを提案します。また、Dockerメタデータ(リリースノート、変更履歴、コミット履歴)をPRに含めることができます。

4. 基本設定:dependabot.ymlファイル

Dependabotの設定は、リポジトリの.github/dependabot.ymlファイルで行います。

4.1. 最小限の設定例

version: 2
updates:
  # npm依存関係の更新を有効化
  - package-ecosystem: "npm"
    # ルートディレクトリのpackage.jsonとlockファイルを確認
    directory: "/"
    # 毎週月曜日から金曜日に確認
    schedule:
      interval: "daily"

  # Docker依存関係の更新を有効化
  - package-ecosystem: "docker"
    # ルートディレクトリのDockerfileを確認
    directory: "/"
    # 週に一度確認
    schedule:
      interval: "weekly"

4.2. 必須キー

キー 場所 用途
version トップレベル 常に2を指定
updates トップレベル 更新対象のパッケージエコシステムを定義
package-ecosystem updates パッケージマネージャーを指定
directory / directories updates マニフェストファイルの場所を指定
schedule.interval updates 更新チェックの頻度

4.3. スケジュール設定の詳細

schedule.intervalには以下の値を指定できます:

  • daily: 平日毎日(月曜日~金曜日)
  • weekly: 週1回(デフォルトは月曜日)
  • monthly: 月1回(月初)
  • quarterly: 四半期に1回(1月、4月、7月、10月の初日)
  • semiannually: 半年に1回(1月と7月の初日)
  • yearly: 年1回(1月初日)
  • cron: cron式で指定

時刻指定の例:

schedule:
  interval: "weekly"
  day: "tuesday"
  time: "02:00"
  timezone: "Asia/Tokyo"

cron式の例:

schedule:
  interval: "cron"
  cronjob: "0 9 * * *"  # 毎日9時(UTC)

5. 実践的な設定パターン

5.1. パターン1:複数ディレクトリの管理

モノレポなど、複数のディレクトリにマニフェストファイルがある場合、directoriesキーを使用します。

version: 2
updates:
  - package-ecosystem: "bundler"
    directories:
      - "/frontend"
      - "/backend"
      - "/admin"
    schedule:
      interval: "weekly"

グロビングパターンの使用:

updates:
  - package-ecosystem: "composer"
    directories:
      - "/"
      - "/lib-*"  # lib-で始まるすべてのディレクトリ
    schedule:
      interval: "weekly"

5.2. パターン2:特定の依存関係のみ許可

allowキーを使用して、更新対象の依存関係を制限できます。

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "daily"
    allow:
      # すべてのタイプの依存関係を許可
      - dependency-type: "all"

依存関係タイプ:

タイプ サポートされるパッケージマネージャー 説明
direct すべて 明示的に定義された依存関係
indirect bundler, pip, composer, cargo, gomod 直接依存関係の依存関係(サブ依存関係)
all すべて すべての明示的依存関係
production bundler, composer, mix, maven, npm, pip 本番環境用依存関係
development bundler, composer, mix, maven, npm, pip 開発環境用依存関係

5.3. パターン3:特定の依存関係を無視

ignoreキーを使用して、更新を無視する依存関係を指定できます。

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    ignore:
      # awsで始まるすべてのパッケージを無視
      - dependency-name: "aws*"
      # expressの特定バージョン範囲を無視
      - dependency-name: "express"
        versions: ["4.x", "5.x"]
      # すべてのパッチアップデートを無視
      - dependency-name: "*"
        update-types: ["version-update:semver-patch"]

5.4. パターン4:グループ化されたアップデート

複数の依存関係を1つのPRにまとめることで、レビュー効率を向上させます。

version: 2
updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    groups:
      # グループ名を定義(ブランチ名とPRタイトルに使用)
      production-dependencies:
        dependency-type: "production"
      
      development-dependencies:
        dependency-type: "development"
        exclude-patterns:
          - "rubocop*"
      
      # パターンマッチングでグループ化
      angular:
        applies-to: version-updates
        patterns:
          - "@angular*"
        update-types:
          - "minor"
          - "patch"

グループ化の条件:

パラメータ 用途
applies-to version-updatesまたはsecurity-updatesを指定
dependency-type developmentまたはproductionに限定
patterns 依存関係名にマッチするパターン
exclude-patterns グループから除外するパターン
update-types minorpatchmajorから選択

5.5. パターン5:クールダウン期間の設定

新しいバージョンがリリースされてから一定期間待機してからアップデートを行います。

updates:
  - package-ecosystem: "pip"
    directory: "/"
    schedule:
      interval: "daily"
    cooldown:
      default-days: 5
      semver-major-days: 30
      semver-minor-days: 7
      semver-patch-days: 3
      include:
        - "requests"
        - "numpy"
        - "pandas*"
      exclude:
        - "pandas"  # 完全一致は除外

クールダウンの動作:

6. プライベートレジストリへのアクセス

プライベートレジストリの依存関係を更新するには、認証情報を設定する必要があります。

6.1. 基本的な設定パターン

version: 2

# トップレベルでレジストリを定義
registries:
  npm-private:
    type: npm-registry
    url: https://npm.example.com
    token: ${{secrets.NPM_TOKEN}}
  
  maven-artifactory:
    type: maven-repository
    url: https://artifactory.example.com/repo
    username: yamada
    password: ${{secrets.MAVEN_PASSWORD}}

updates:
  - package-ecosystem: "npm"
    directory: "/"
    # 定義したレジストリを参照
    registries:
      - npm-private
    schedule:
      interval: "weekly"
  
  - package-ecosystem: "maven"
    directory: "/"
    registries:
      - maven-artifactory
    schedule:
      interval: "weekly"

6.2. サポートされているレジストリタイプ

レジストリタイプ 必須の認証パラメータ
cargo-registry token
composer-repository username, password
docker-registry username, password
git username, password
hex-organization organization, key
hex-repository repo, auth-key
maven-repository username, password
npm-registry username, password または token
nuget-feed username, password または token
python-index username, password または token
rubygems-server username, password または token
terraform-registry token

6.3. エコシステム別のプライベートレジストリ設定

6.3.1. npm/yarn

オプション1:dependabot.ymlで設定

registries:
  npm-github:
    type: npm-registry
    url: https://npm.pkg.github.com
    token: ${{secrets.MY_GITHUB_PERSONAL_TOKEN}}

さらに、.npmrcファイルをリポジトリにコミット:

registry=https://npm.pkg.github.com

スコープ付き依存関係の場合:

@my-org:registry=https://npm.pkg.github.com

オプション2:replaces-baseを使用

すべてのレジストリリクエストを指定したベースURLを通して送信します。

registries:
  npm-private:
    type: npm-registry
    url: https://private-registry.example.com
    token: ${{secrets.NPM_TOKEN}}
    replaces-base: true

6.3.2. Python (pip/poetry)

pip用の設定:

registries:
  python-azure:
    type: python-index
    url: https://pkgs.dev.azure.com/org/_packaging/feed/pypi/example
    username: yamada@example.com
    password: ${{secrets.AZURE_DEVOPS_TOKEN}}

pip.confファイルをリポジトリにコミット:

[global]
timeout = 60
index-url = https://private-registry.example.com

poetry用の設定:

pyproject.tomlに以下を追加:

[[tool.poetry.source]]
name = "private"
url = "https://private-registry.example.com"
default = true

6.3.3. Docker

registries:
  dockerhub:
    type: docker-registry
    url: https://registry.hub.docker.com
    username: yamada
    password: ${{secrets.DOCKERHUB_PASSWORD}}

AWS ECRの場合:

registries:
  ecr-docker:
    type: docker-registry
    url: https://1234567890.dkr.ecr.us-east-1.amazonaws.com
    username: ${{secrets.ECR_AWS_ACCESS_KEY_ID}}
    password: ${{secrets.ECR_AWS_SECRET_ACCESS_KEY}}

6.3.4. Maven

registries:
  maven-artifactory:
    type: maven-repository
    url: https://artifactory.example.com/maven-repo
    username: yamada
    password: ${{secrets.ARTIFACTORY_PASSWORD}}

pom.xmlに以下を追加:

<repositories>
  <repository>
    <id>private-repo</id>
    <url>https://artifactory.example.com/maven-repo</url>
  </repository>
</repositories>

6.4. シークレットの管理

認証情報は、リポジトリまたは組織レベルのDependabotシークレットとして保存します。

リポジトリシークレットの追加手順:

  1. リポジトリのSettingsタブに移動
  2. Security > Secrets and variables > Dependabotを選択
  3. New repository secretをクリック
  4. シークレット名と値を入力

組織シークレットの追加手順:

  1. 組織のSettingsタブに移動
  2. Security > Secrets and variables > Dependabotを選択
  3. New organization secretをクリック
  4. アクセスポリシーを選択(すべてのリポジトリ、プライベートリポジトリのみ、指定したリポジトリのみ)

シークレット名の規則:

  • 英数字([A-Z][0-9])またはアンダースコア(_)のみ
  • GITHUB_で始めることはできない
  • 数字で始めることはできない

7. PRのカスタマイズ

Dependabotが作成するPRを、プロジェクトのワークフローに合わせてカスタマイズできます。

7.1. ラベルの設定

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    labels:
      - "dependencies"
      - "javascript"
      - "triage-board"

デフォルトでは、Dependabotは以下のラベルを自動的に付与します:

  • dependencies(すべてのPR)
  • エコシステムラベル(例:javanpmdocker
  • SemVerラベル(majorminorpatch)※リポジトリに存在する場合

7.2. アサイン担当者の設定

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    assignees:
      - "yamada-taro"
      - "suzuki-hanako"

7.3. マイルストーンの関連付け

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "monthly"
    milestone: 4  # マイルストーンの数値IDを指定

7.4. コミットメッセージのカスタマイズ

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "daily"
    commit-message:
      # すべてのコミットメッセージに"npm"プレフィックスを追加
      prefix: "npm"
      # スコープ(更新された依存関係のリスト)を含める
      include: "scope"

開発依存関係用に別のプレフィックスを使用:

commit-message:
  prefix: "pip prod"
  prefix-development: "pip dev"
  include: "scope"

7.5. ブランチ名のセパレータ変更

デフォルトでは、Dependabotはdependabot/PACKAGE_MANAGER/DEPENDENCY形式でブランチを作成します。

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    pull-request-branch-name:
      separator: "-"  # dependabot-npm-dependency となる

利用可能な値:"-"_/

8. 自動トリアージルール

大規模なプロジェクトでは、すべてのアラートを手動で管理するのは現実的ではありません。自動トリアージルールを使用して、アラートの管理を自動化できます。

8.1. GitHubプリセットルール

開発スコープの低影響な問題を自動的に却下するプリセットルールが用意されています。

有効化手順:

  1. リポジトリのSettings > Security > Advanced Securityに移動
  2. Dependabot rulesの歯車アイコンをクリック
  3. Dismiss low impact issues for development-scoped dependenciesを有効化

このルールで自動却下される問題:

  • npm依存関係で発見される特定タイプの脆弱性
  • 開発環境でのみ使用される依存関係
  • 悪用される可能性が低い脆弱性
  • リソース管理、プログラミングロジック、情報開示に関する問題

対象となるCWE(Common Weakness Enumeration):

カテゴリ 主なCWE
リソース管理 CWE-400(制御されないリソース消費)、CWE-770(制限なしリソース割り当て)
プログラミングエラー CWE-185(不正な正規表現)、CWE-754(例外的条件の不適切なチェック)
情報開示 CWE-544(標準化されたエラー処理メカニズムの欠如)、CWE-451(UIの重要情報の誤表示)

8.2. カスタム自動トリアージルール

独自のルールを作成して、より細かく制御できます。

カスタムルールで使用できるメタデータ:

  • CVE ID
  • CWE
  • 依存関係スコープ(devDependencyまたはruntime)
  • エコシステム
  • GHSA ID
  • マニフェストパス(リポジトリレベルルールのみ)
  • パッケージ名
  • パッチの可用性
  • 重大度
  • EPSSスコア

ルール作成手順:

  1. リポジトリのSettings > Security > Advanced Securityに移動
  2. Dependabot rulesの歯車アイコンをクリック
  3. New ruleをクリック
  4. ルール名と条件を設定
  5. アクション(却下、スヌーズ、PRを開く)を選択

アクションの種類:

  • 却下:アラートを無期限に却下、またはパッチが利用可能になるまで却下
  • スヌーズ:指定期間アラートを非表示
  • PRを開く:DependabotにセキュリティアップデートのPRを作成させる

8.3. 自動却下されたアラートの管理

自動却下されたアラートは、フィルタリングして表示できます:

  1. リポジトリのSecurity > Dependabot alertsに移動
  2. is:openフィルタをクリア
  3. Closed as > Auto-dismissedを選択

再度開く必要がある場合は、チェックボックスを選択してReopenをクリックします。

9. マルチエコシステムアップデート

複数のパッケージエコシステムにまたがる依存関係を、1つのPRでまとめて更新できます。

9.1. 基本的な設定

version: 2

# マルチエコシステムグループを定義
multi-ecosystem-groups:
  infrastructure:
    schedule:
      interval: "weekly"
    assignees: ["@platform-team"]
    labels: ["infrastructure", "dependencies"]

updates:
  # Docker依存関係
  - package-ecosystem: "docker"
    directory: "/"
    patterns: ["nginx", "redis", "postgres"]
    multi-ecosystem-group: "infrastructure"

  # Terraform依存関係
  - package-ecosystem: "terraform"
    directory: "/"
    patterns: ["aws", "terraform-*"]
    multi-ecosystem-group: "infrastructure"

  # Python依存関係
  - package-ecosystem: "pip"
    directory: "/"
    patterns: ["boto3", "requests", "pyyaml"]
    multi-ecosystem-group: "infrastructure"

9.2. マルチエコシステムアップデートのワークフロー

9.3. ユースケース

9.3.1. インフラストラクチャプロジェクト

Docker、Terraform、自動化スクリプトなど、複数の技術を使用するインフラコードをまとめて管理します。

multi-ecosystem-groups:
  infrastructure:
    schedule:
      interval: "weekly"

updates:
  - package-ecosystem: "docker"
    directory: "/"
    patterns: ["nginx", "redis", "postgres"]
    multi-ecosystem-group: "infrastructure"
    
  - package-ecosystem: "terraform"
    directory: "/"
    patterns: ["aws", "terraform-*"]
    multi-ecosystem-group: "infrastructure"
    
  - package-ecosystem: "pip"
    directory: "/"
    patterns: ["boto3", "requests"]
    multi-ecosystem-group: "infrastructure"

9.3.2. フルスタックアプリケーション

フロントエンドとバックエンドの依存関係を一緒に更新して、互換性を確保します。

multi-ecosystem-groups:
  app-dependencies:
    schedule:
      interval: "daily"

updates:
  - package-ecosystem: "npm"
    directory: "/frontend"
    patterns: ["react", "lodash", "@types/*"]
    multi-ecosystem-group: "app-dependencies"
    
  - package-ecosystem: "bundler"
    directory: "/backend"
    patterns: ["rails", "pg", "sidekiq"]
    multi-ecosystem-group: "app-dependencies"

9.3.3. クロスプラットフォームライブラリ

異なる言語実装で同じプロトコルライブラリのバージョンを同期します。

multi-ecosystem-groups:
  grpc-and-protobuf:
    schedule:
      interval: "daily"

updates:
  - package-ecosystem: "bundler"
    directory: "/grpc-proto-test/"
    patterns: ["grpc", "google-protobuf"]
    multi-ecosystem-group: "grpc-and-protobuf"
    
  - package-ecosystem: "npm"
    directory: "/grpc-proto-test/"
    patterns: ["@grpc/grpc-js", "google-protobuf"]
    multi-ecosystem-group: "grpc-and-protobuf"

9.4. 設定キーの動作

マルチエコシステムアップデートでは、グループレベルとエコシステムレベルの2階層で設定を行います。

加算的キー(両方のレベルの値がマージされる):

  • assignees:グループレベルとエコシステムレベルの担当者がすべて割り当てられる
  • labels:グループレベルとエコシステムレベルのラベルがすべて適用される

グループ専用キー(グループレベルのみで設定可能):

  • milestone
  • commit-message
  • target-branch
  • pull-request-branch-name

エコシステムレベルでこれらのキーを設定しようとすると、設定エラーが発生します。

10. GitHub Actionsとの統合

Dependabotは、GitHub Actionsと緊密に統合されています。

10.1. デフォルトの動作

リポジトリでGitHub Actionsが有効な場合、DependabotはGitHub-hostedランナー上で自動的に実行されます。これにより:

  • ジョブエラーの特定が容易
  • 失敗した実行の手動検出とトラブルシューティングが可能
  • GitHub Actions APIとWebhookを使用したCI/CDパイプライン統合が可能

重要な点:

  • Dependabotが有効なリポジトリでは、Actionsポリシーチェックとリポジトリ/組織レベルでの無効化をバイパスして、常にGitHub Actions上で実行される
  • これにより、セキュリティとバージョン更新ワークフローが常に実行される

10.2. セルフホステッドランナーでの実行

プライベートレジストリや内部ネットワークリソースへのアクセスをより細かく制御したい場合、セルフホステッドランナーを使用できます。

システム要件:

  • Linuxオペレーティングシステム
  • x64アーキテクチャ
  • Dockerがインストールされ、ランナーユーザーがアクセス可能

設定手順:

  1. セルフホステッドランナーの追加

    • リポジトリまたは組織レベルでランナーをプロビジョニング
    • Dockerをインストールし、ランナーユーザーがアクセスできるように設定
  2. dependabotラベルの割り当て

    • ランナーにdependabotラベルを割り当て
  3. 組織設定で有効化

    • 組織のSettings > Security > Advanced Security > Global settingsに移動
    • Dependabot on self-hosted runnersを選択

10.3. Actions Runner Controller (ARC)の使用

Kubernetesクラスタ上でDependabotを実行するために、ARCを使用できます。

ARCのセットアップフロー:

ARCインストールスクリプト例:

#!/bin/bash
NAMESPACE="arc-systems"
helm install arc \
    --namespace "${NAMESPACE}" \
    --create-namespace \
    oci://ghcr.io/actions/actions-runner-controller-charts/gha-runner-scale-set-controller

ランナースケールセット設定:

#!/bin/bash
INSTALLATION_NAME="dependabot"  # 必ずdependabotという名前にする
NAMESPACE="arc-runners"
GITHUB_CONFIG_URL="https://github.com/your-org/your-repo"
GITHUB_PAT="your-personal-access-token"

helm install "${INSTALLATION_NAME}" \
    --namespace "${NAMESPACE}" \
    --create-namespace \
    --set githubConfigUrl="${GITHUB_CONFIG_URL}" \
    --set githubConfigSecret.github_token="${GITHUB_PAT}" \
    --set containerMode.type="dind" \
    oci://ghcr.io/actions/actions-runner-controller-charts/gha-runner-scale-set

10.4. Azure VNETの使用

Azure Virtual Network (VNET)を使用して、GitHub-hostedランナー上でDependabotをプライベートネットワークで実行できます。

設定フロー:

重要な設定:

  • ランナー名は必ずdependabotにする
  • Linux x64プラットフォームを選択
  • 組織レベルでDependabot on self-hosted runnersを有効化

10.5. ワークフローの自動化

DependabotがPRを作成したときに、GitHub Actionsワークフローをトリガーできます。

メタデータの取得例:

name: Dependabotメタデータ取得
on: pull_request

permissions:
  pull-requests: write
  issues: write

jobs:
  dependabot:
    runs-on: ubuntu-latest
    if: github.event.pull_request.user.login == 'dependabot[bot]'
    steps:
      - name: Dependabotメタデータ
        id: metadata
        uses: dependabot/fetch-metadata@v2
        with:
          github-token: "${{ secrets.GITHUB_TOKEN }}"
      
      # 以下のプロパティが利用可能:
      # - steps.metadata.outputs.dependency-names
      # - steps.metadata.outputs.dependency-type
      # - steps.metadata.outputs.update-type

自動ラベル付け例:

name: Dependabot自動ラベル
on: pull_request

permissions:
  pull-requests: write

jobs:
  dependabot:
    runs-on: ubuntu-latest
    if: github.event.pull_request.user.login == 'dependabot[bot]'
    steps:
      - name: メタデータ取得
        id: metadata
        uses: dependabot/fetch-metadata@v2
        with:
          github-token: "${{ secrets.GITHUB_TOKEN }}"
      
      - name: 本番依存関係にラベル追加
        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}}
          GH_TOKEN: ${{secrets.GITHUB_TOKEN}}

自動承認例:

name: Dependabot自動承認
on: pull_request

permissions:
  pull-requests: write

jobs:
  dependabot:
    runs-on: ubuntu-latest
    if: github.event.pull_request.user.login == 'dependabot[bot]'
    steps:
      - name: メタデータ取得
        id: metadata
        uses: dependabot/fetch-metadata@v2
        with:
          github-token: "${{ secrets.GITHUB_TOKEN }}"
      
      - name: PRを承認
        run: gh pr review --approve "$PR_URL"
        env:
          PR_URL: ${{github.event.pull_request.html_url}}
          GH_TOKEN: ${{secrets.GITHUB_TOKEN}}

自動マージの有効化例:

name: Dependabot自動マージ
on: pull_request

permissions:
  contents: write
  pull-requests: write

jobs:
  dependabot:
    runs-on: ubuntu-latest
    if: github.event.pull_request.user.login == 'dependabot[bot]'
    steps:
      - name: メタデータ取得
        id: metadata
        uses: dependabot/fetch-metadata@v2
        with:
          github-token: "${{ secrets.GITHUB_TOKEN }}"
      
      - name: 自動マージを有効化
        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}}

10.6. Dependabotとワークフローの制限事項

Dependabotによってトリガーされるワークフローには、いくつかの制限があります:

制限事項:

  1. GITHUB_TOKENはデフォルトで読み取り専用権限
  2. シークレットはDependabotシークレットから取得(GitHub Actionsシークレットは利用不可)
  3. pull_requestpull_request_reviewpull_request_review_commentpushcreatedeploymentdeployment_statusイベントに適用

回避策:

# ワークフローの権限を明示的に設定
permissions:
  pull-requests: write
  issues: write
  contents: write

11. PRの管理

11.1. コメントコマンド

Dependabotは、PRのコメントに特定のコマンドを書くことで制御できます。

コマンド 説明
@dependabot cancel merge 以前にリクエストしたマージをキャンセル
@dependabot close PRを閉じ、再作成を防止
@dependabot ignore this dependency この依存関係の更新を無視
@dependabot ignore this major version このメジャーバージョンの更新を無視
@dependabot ignore this minor version このマイナーバージョンの更新を無視
@dependabot ignore this patch version このパッチバージョンの更新を無視
@dependabot merge CIテスト成功後にPRをマージ
@dependabot rebase PRをリベース
@dependabot recreate PRを再作成(編集内容を上書き)
@dependabot reopen 閉じたPRを再度開く
@dependabot show DEPENDENCY_NAME ignore conditions 無視条件を表示
@dependabot squash and merge CIテスト成功後にスカッシュマージ

11.2. グループ化されたPR用のコマンド

グループ化されたPRでは、特定の依存関係を無視/無視解除できます:

コマンド 説明
@dependabot ignore DEPENDENCY_NAME この依存関係の更新を無視
@dependabot ignore DEPENDENCY_NAME major version メジャーバージョンの更新を無視
@dependabot ignore DEPENDENCY_NAME minor version マイナーバージョンの更新を無視
@dependabot ignore DEPENDENCY_NAME patch version パッチバージョンの更新を無視
@dependabot unignore * すべての無視条件をクリアして新しいPRを開く
@dependabot unignore DEPENDENCY_NAME 特定の依存関係の無視条件をクリア
@dependabot unignore DEPENDENCY_NAME IGNORE_CONDITION 特定の無視条件のみクリア

11.3. リベース戦略の変更

デフォルトでは、DependabotはPRを自動的にリベースします。これを無効化できます:

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "daily"
    rebase-strategy: "disabled"

12. ログの確認とトラブルシューティング

12.1. ジョブログの表示

Dependabotのアクティビティを確認するには:

  1. リポジトリのInsightsタブに移動
  2. Dependency graph > Dependabotをクリック
  3. 対象のマニフェストファイルの右側にあるRecent update jobsをクリック
  4. 詳細なログを確認するには、view logsをクリック

ログに記録されるジョブタイプ:

  • Version update:Dependabotバージョンアップデートの実行
  • Security update:Dependabotセキュリティアップデートの実行
  • Rebase update:PRのリベース実行

12.2. 依存関係の確認

設定した依存関係が正しく監視されているか確認するには:

  1. リポジトリのInsights > Dependency graph > Dependabotに移動
  2. 各パッケージマネージャーの右側にある歯車アイコンをクリック
  3. 監視対象のファイルを確認

12.3. よくあるエラーと対処法

12.3.1. Dependabotが非脆弱性バージョンに更新できない

**原因:**依存関係グラフを壊さずに脆弱な依存関係を更新できない

対処法:

  • バージョン更新を有効化して、すべての依存関係を最新に保つ
  • 依存関係グラフ全体のアップグレードを検討

12.3.2. 必要なバージョンに更新できない(最新バージョンのPRが既に開いている)

**原因:**同じ依存関係の最新バージョンへのPRが既に存在

対処法:

  • 既存のPRをレビューしてマージ
  • 既存のPRを閉じて新しいセキュリティアップデートPRをトリガー

12.3.3. Dependabotがタイムアウト

**原因:**大規模なリポジトリや多数のマニフェストファイル

対処法:

  • allowパラメータで更新対象を制限
  • ignoreパラメータで一部の依存関係を除外
  • セキュリティアップデートの場合、バージョン更新を有効化して依存関係を最新に保つ

12.3.4. これ以上PRを開けない

**原因:**オープンなPRの数が上限に達している

対処法:

  • 既存のPRをレビュー・マージ
  • open-pull-requests-limitパラメータを調整(デフォルト:バージョン更新5、セキュリティ更新10)

12.3.5. 依存関係を解決またはアクセスできない

**原因:**プライベート依存関係にアクセスできない

対処法:

  • dependabot.ymlでプライベートレジストリへのアクセスを設定
  • 組織設定でDependabotにプライベートリポジトリへのアクセスを許可

12.3.6. グループ化に失敗

**原因:**設定の競合や空のグループの作成

対処法:

# 誤った設定例
allow:
  dependency-type: production  # ジョブ全体を本番依存関係に制限
groups:
  development-dependencies:
    dependency-type: "development"  # このグループは常に空になる

# 正しい設定例
groups:
  production-dependencies:
    dependency-type: "production"
  development-dependencies:
    dependency-type: "development"

13. バージョニング戦略

Dependabotが依存関係のバージョン要件をどのように更新するか制御できます。

13.1. 戦略の種類

戦略 動作
auto デフォルト動作(アプリはincrease、ライブラリはwiden
increase 最小バージョン要件を常に新しいバージョンに引き上げ
increase-if-necessary 既存の要件で新しいリリースが許可されている場合は変更せず、それ以外の場合は拡大
lockfile-only ロックファイルのみ更新、マニフェストは変更しない
widen 新旧両方のバージョンを含むように要件を拡大

13.2. 例

現在のバージョンが1.0.0で、現在の制約が^1.0.0の場合:

新しいバージョン1.2.0

  • increase: 新しい制約 ^1.2.0
  • increase-if-necessary: 新しい制約 ^1.0.0(変更なし)
  • widen: 新しい制約 ^1.0.0(変更なし)

新しいバージョン2.0.0

  • increase: 新しい制約 ^2.0.0
  • increase-if-necessary: 新しい制約 ^2.0.0
  • widen: 新しい制約 >=1.0.0 <3.0.0

13.3. 設定例

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "daily"
    # すべてのnpm依存関係の最小バージョンを引き上げ
    versioning-strategy: increase

  - package-ecosystem: "pip"
    directory: "/"
    schedule:
      interval: "daily"
    # 必要な場合のみバージョン要件を引き上げ
    versioning-strategy: increase-if-necessary

14. ベストプラクティス

14.1. セキュリティに焦点を当てた依存関係管理

version: 2
updates:
  # セキュリティアップデートを最優先
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "daily"  # 毎日チェック
    open-pull-requests-limit: 10
    
    # 本番依存関係を別グループで管理
    groups:
      production-critical:
        dependency-type: "production"
        update-types: ["patch", "minor"]  # 破壊的変更を避ける

14.2. 段階的なロールアウト

updates:
  # ステージング環境用の設定
  - package-ecosystem: "npm"
    directory: "/"
    target-branch: "staging"
    schedule:
      interval: "weekly"
      day: "monday"
    
    # 本番環境用の設定(ステージングで検証後)
  - package-ecosystem: "npm"
    directory: "/"
    target-branch: "main"
    schedule:
      interval: "weekly"
      day: "friday"

14.3. 通知の最適化

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    
    # 重要な更新のみ通知
    labels:
      - "dependencies"
      - "requires-review"
    
    assignees:
      - "@security-team"

14.4. CI/CDとの統合

# .github/workflows/dependabot-auto-merge.yml
name: Dependabot自動マージ
on: pull_request

permissions:
  contents: write
  pull-requests: write

jobs:
  dependabot:
    runs-on: ubuntu-latest
    if: github.event.pull_request.user.login == 'dependabot[bot]'
    steps:
      - name: メタデータ取得
        id: metadata
        uses: dependabot/fetch-metadata@v2
        with:
          github-token: "${{ secrets.GITHUB_TOKEN }}"
      
      # パッチアップデートは自動マージ
      - name: 自動マージ有効化
        if: 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}}

14.5. ロックファイルとvendoringの使用

updates:
  - package-ecosystem: "bundler"
    directory: "/"
    schedule:
      interval: "weekly"
    # vendorディレクトリの依存関係も維持
    vendor: true
    
  - package-ecosystem: "gomod"
    directory: "/"
    schedule:
      interval: "weekly"
    # Go modulesは自動的にvendoringをサポート

14.6. モノレポの管理

version: 2
updates:
  # フロントエンドアプリ
  - package-ecosystem: "npm"
    directories:
      - "/apps/web"
      - "/apps/mobile"
    schedule:
      interval: "weekly"
    groups:
      frontend-shared:
        patterns:
          - "react*"
          - "@types/*"
  
  # バックエンドサービス
  - package-ecosystem: "gomod"
    directories:
      - "/services/api"
      - "/services/worker"
    schedule:
      interval: "weekly"
    groups:
      backend-shared:
        patterns:
          - "github.com/our-org/*"

15. パフォーマンスの最適化

15.1. PRの数を制限

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    # 同時に開くPRの最大数を制限
    open-pull-requests-limit: 5
    
    # グループ化で更新をまとめる
    groups:
      dependencies:
        patterns:
          - "*"
        update-types:
          - "minor"
          - "patch"

15.2. 更新頻度の調整

updates:
  # 本番依存関係:毎日チェック
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "daily"
    allow:
      - dependency-type: "production"
  
  # 開発依存関係:週1回チェック
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    allow:
      - dependency-type: "development"

15.3. 不要な更新を除外

updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    ignore:
      # 内部パッケージは別プロセスで管理
      - dependency-name: "@our-company/*"
      
      # 特定のバージョン範囲を無視
      - dependency-name: "typescript"
        versions: ["5.x"]
      
      # パッチアップデートは手動で管理
      - dependency-name: "react"
        update-types: ["version-update:semver-patch"]

16. セキュリティの強化

16.1. プライベートレジストリのみにアクセス制限

公開レジストリへのアクセスを削除し、プライベートレジストリのみを使用できます。

npm例:

# dependabot.yml
registries:
  npm-private:
    type: npm-registry
    url: https://npm.example.com
    token: ${{secrets.NPM_TOKEN}}

updates:
  - package-ecosystem: "npm"
    directory: "/"
    registries:
      - npm-private
    schedule:
      interval: "weekly"

.npmrcファイル:

registry=https://npm.example.com

NuGet例:

# dependabot.yml
registries:
  nuget-private:
    type: nuget-feed
    url: https://nuget.example.com/v3/index.json
    token: ${{secrets.NUGET_TOKEN}}

nuget.configファイル:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="private-nuget" value="https://nuget.example.com/v3/index.json" />
  </packageSources>
</configuration>

16.2. セルフホステッドランナーでのネットワーク分離

ネットワーク要件:

  • プライベートレジストリへのアクセスを許可
  • dependabot-actions.githubapp.comへの送信トラフィックを許可
  • 内部ネットワークへのアクセスを最小限に制限

16.3. 証明書の設定

自己署名証明書を使用するレジストリの場合:

  1. セルフホステッドランナーのVMに証明書をインストール
  2. Node.jsが証明書を使用するように設定
# Ubuntu例
sudo cp your-cert.crt /usr/local/share/ca-certificates/
sudo update-ca-certificates

# Node.jsの設定
export NODE_EXTRA_CA_CERTS=/path/to/your-cert.crt

17. 監視とレポート

17.1. アラートの追跡

17.2. フィルタリングとソート

Dependabotアラートページで利用可能なフィルタ:

フィルタ 説明
ecosystem エコシステムで絞り込み ecosystem:npm
severity 深刻度で絞り込み severity:high
is ステータスで絞り込み is:open
package パッケージ名で絞り込み package:django
scope スコープで絞り込み scope:development
resolution 解決ステータスで絞り込み resolution:no-bandwidth
sort ソート順を指定 sort:most-important

17.3. 監査ログの確認

Dependabotに関連するアクションは、組織の監査ログに記録されます:

  1. 組織のSettings > Logs > Audit logに移動
  2. repository_vulnerability_alertカテゴリでフィルタ
  3. 以下のアクションを確認:
    • create:アラート作成
    • dismiss:アラート却下
    • resolve:アラート解決
    • reopen:アラート再オープン

18. まとめ

Dependabotは、依存関係管理を自動化し、セキュリティを向上させる強力なツールです。本記事で紹介した設定とベストプラクティスを活用することで:

  1. セキュリティリスクの低減:脆弱性を迅速に検出し、自動的に修正
  2. 開発効率の向上:手動での依存関係管理から解放
  3. コードベースの健全性維持:依存関係を常に最新かつ安全な状態に保つ
  4. チーム全体の可視性向上:依存関係の状態を一元管理

適切な設定とモニタリングにより、Dependabotは開発チームの生産性を大きく向上させます。まずは基本的な設定から始めて、徐々にカスタマイズを進めていくことをお勧めします。

次のステップ:

  1. .github/dependabot.ymlファイルを作成
  2. 主要なパッケージエコシステムの更新を有効化
  3. Dependabotアラートを確認
  4. 自動トリアージルールを設定
  5. GitHub Actionsとの統合を検討
  6. チームのワークフローに合わせてカスタマイズ

依存関係管理の自動化は、現代のソフトウェア開発における必須の投資です。Dependabotを活用して、より安全で効率的な開発環境を構築しましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?