Skip to content

Instantly share code, notes, and snippets.

@yanaokahiroki
Created October 22, 2022 10:14
Show Gist options
  • Save yanaokahiroki/b9fcfb0983d85532579f659a627188f9 to your computer and use it in GitHub Desktop.
Save yanaokahiroki/b9fcfb0983d85532579f659a627188f9 to your computer and use it in GitHub Desktop.
DependabotのPRを自動でマージする

概要

ソースコードをGitHubで管理しているとDependabotでライブラリの脆弱性を発見するのが便利。 しかしその脆弱性対応で必要なのがパッチレベルの更新だったりすると自動でマージしてしまいたい、と思った。 調べてみるとGitHub公式のサイトに載っていたのでメモ。 実際のファイルは自分のリポジトリから持ってきた。

ワークフロー

name: Dependabot Auto Merge
on:
  pull_request_target:

permissions:
  pull-requests: write
  contents: write

jobs:
  dependabot:
    runs-on: ubuntu-latest
    if: ${{ github.event.pull_request.user.login == 'dependabot[bot]' }}
    steps:
      - name: Dependabot metadata
        id: metadata
        uses: dependabot/[email protected]

      # 更新がパッチかマイナーの場合だけマージ
      - name: 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}}
          GITHUB_TOKEN: ${{ secrets.DEPENDABOT_AUTOMATION_TOKEN }}

Dependabotの設定

version: 2
updates:
  - package-ecosystem: 'npm'
    # リポジトリのルートに保存されているファイルが対象
    directory: '/'
    # 毎週土曜日午前10時(JST, UTC+9h)
    schedule:
      interval: 'weekly'
      day: 'saturday'
      time: '10:00'
      timezone: 'Asia/Tokyo'
    # mainブランチからブランチを切る
    target-branch: 'main'
    reviewers:
      - yanaokahiroki

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment