如何自动合并 dependabot 更新(配置版本 2)?

How do I automerge dependabot updates (config version 2)?

在“Dependabot 正在本机迁移到 GitHub!”之后,我不得不更新我的 dependabot 配置文件以使用版本 2 格式。

我的 .dependabot/config.yaml 看起来像:

version: 1
update_configs:
  - package_manager: "python"
    directory: "/"
    update_schedule: "live"
    automerged_updates:
      - match:
          dependency_type: "all"
          update_type: "all"

我有以下工作:

version: 2
updates:
- package-ecosystem: pip
  directory: "/"
  schedule:
    interval: daily

但我似乎无法再次添加自动合并选项(使用 dependabot validator 检查时)?

Auto-merge 在 Dependabot 上被禁用为 GitHub:

Auto-merge will not be supported in GitHub-native Dependabot for the foreseeable future. We know some of you have built great workflows that rely on auto-merge, but right now, we’re concerned about auto-merge being used to quickly propagate a malicious package across the ecosystem. We recommend always verifying your dependencies before merging them.

有一些 hack 可以完成这项工作,您可以查看 GitHub dependabot-core issue #1973 以获取一些想法。

这是一个不需要任何额外市场安装的解决方案(最初发现 here)。只需创建一个新的 GitHub 工作流程(例如 .github/workflows/dependabotautomerge.yml),其中包含:

name: "Dependabot Automerge - Action"

on:
  pull_request:

jobs:
  worker:
    runs-on: ubuntu-latest

    if: github.actor == 'dependabot[bot]'
    steps:
      - name: automerge
        uses: actions/github-script@0.2.0
        with:
          script: |
            github.pullRequests.createReview({
              owner: context.payload.repository.owner.login,
              repo: context.payload.repository.name,
              pull_number: context.payload.pull_request.number,
              event: 'APPROVE'
            })
            github.pullRequests.merge({
              owner: context.payload.repository.owner.login,
              repo: context.payload.repository.name,
              pull_number: context.payload.pull_request.number
            })
          github-token: ${{github.token}}

GitHub Marketplace 上还有各种 third-party 解决方案可用。

现在是 officially documented feature。您可以批准 Dependabot 拉取请求并将其设置为与 GitHub 操作工作流程自动合并,例如...

name: Dependabot auto-approve
on: pull_request_target
    
permissions:
  contents: write
  pull-requests: write
    
jobs:
  dependabot:
    runs-on: ubuntu-latest
    if: ${{ github.actor == 'dependabot[bot]' }}
    steps:
      - name: Dependabot metadata
        id: metadata
        uses: dependabot/fetch-metadata@v1.1.1
        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}}
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

如果您使用 code owners 并且分支受到保护,您可能会发现这仍将等待代码所有者审查以合并。不幸的是,代码所有者不允许您取消受影响的文件,因此您需要在代码所有者中明确列出拥有的文件以启用完全非交互式合并步骤。