【问题标题】:How do I automerge dependabot updates (config version 2)?如何自动合并依赖机器人更新(配置版本 2)?
【发布时间】:2021-01-14 21:33:11
【问题描述】:

在“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 检查时)?

【问题讨论】:

  • 听起来他们现在可能会推迟这个功能:github.com/dependabot/dependabot-core/issues/1973
  • 您应该将此添加为答案
  • 此外,请注意,此功能永远不会添加到 GitHub 上的 Dependabot。检查@milton-castro 的答案
  • 看起来 GitHub 不打算将其作为一项功能直接添加到 Dependabot,但他们已经正式记录了如何使用 Actions workflow 来做到这一点。

标签: python github yaml github-actions dependabot


【解决方案1】:

这是一种不需要任何额外市场安装的解决方案(最初发现 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 上还有各种第三方解决方案。

【讨论】:

【解决方案2】:

在 Dependabot 上禁用了自动合并到 GitHub:

在可预见的将来,GitHub-native Dependabot 将不支持自动合并。我们知道你们中的一些人已经构建了依赖自动合并的出色工作流程,但现在,我们担心自动合并被用于在整个生态系统中快速传播恶意程序包。我们建议始终在合并之前验证您的依赖项。

有一些技巧可以完成这项工作,您可以查看 GitHub dependabot-core issue #1973 了解一些想法。

【讨论】:

    【解决方案3】:

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

    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 并且分支受到保护,您可能会发现这仍将等待代码所有者审查合并。不幸的是,代码所有者不允许您否定受影响的文件,因此您需要在代码所有者中明确列出拥有的文件以启用完全非交互式的合并步骤。

    【讨论】:

    • 两个警告:1. 你必须在你的 repo 设置中启用自动合并,2. 你必须通过“检查必须在合并之前通过”来保护目标分支。如果您不同时执行这两项操作,则上面的命令行(出于某种原因)会在 PR 运行后立即合并 ?
    • @mxcl 可以订购工作流并设置依赖项。假设有构建和自动合并工作流,您可以设置构建工作流结果成功后的自动合并运行。
    猜你喜欢
    • 2017-12-26
    • 2019-06-15
    • 2022-11-10
    • 2016-03-06
    • 2015-10-18
    • 1970-01-01
    • 2018-12-07
    • 2022-08-03
    • 2017-09-07
    相关资源
    最近更新 更多