跳至主要内容

使用 GitHub Actions 自动化 Dependabot

示例:如何使用 GitHub Actions 自动化常见的 Dependabot 相关任务。

谁可以使用此功能?

具有 写入 访问权限的用户

注意

本文解释了如何使用 GitHub Actions 自动化 Dependabot 相关任务。如需了解使用 GitHub Actions 运行 Dependabot 更新的更多信息,请改为参阅 关于 GitHub Actions Runner 上的 Dependabot

当 Dependabot 创建用于更新依赖项的拉取请求时,您可以使用 GitHub Actions 执行自动化任务。如果您希望

  • 确保 Dependabot 的拉取请求(版本更新和安全更新)携带符合工作流程的正确数据,包括标签和名称。

  • 触发工作流,将 Dependabot 的拉取请求(版本更新和安全更新)送入审查流程,或自动合并。

关于 Dependabot 与 GitHub Actions

重要提示

如果在仓库中启用了 Dependabot,它将始终在 GitHub Actions 上运行,绕过组织或仓库层面的 Actions 策略检查以及禁用设置。这可确保在启用 Dependabot 时,安全更新和版本更新工作流始终执行。

Dependabot 会创建拉取请求以保持依赖项为最新。您可以使用 GitHub Actions 在这些拉取请求创建时执行自动化任务,例如获取额外的制品、添加标签、运行测试或以其他方式修改拉取请求。

Dependabot 能够在其拉取请求和评论上触发 GitHub Actions 工作流;但某些事件的处理方式有所不同。更多信息请参阅 GitHub Actions 上的 Dependabot 疑难解答

以下是可使用 GitHub Actions 自动化的几类常见拉取请求场景。

获取拉取请求的元数据

大多数自动化任务需要了解拉取请求内容的信息:依赖名称、是否为生产依赖、以及是重大、次要还是补丁更新。您可以使用一个 Action 来检索由 Dependabot 生成的拉取请求中所更新依赖的相关信息。

示例

YAML
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
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 仓库。

给拉取请求打标签

如果您有其他基于 GitHub 标签的自动化或分流工作流,可配置 Action 根据提供的元数据自动分配标签。

示例:为所有生产依赖更新添加标签

YAML
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
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}}

自动批准拉取请求

您可以在工作流中使用 GitHub CLI 自动批准 Dependabot 的拉取请求。

示例

YAML
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
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}}

在拉取请求上启用自动合并

如果希望维护者能够标记某些拉取请求以进行自动合并,可使用 GitHub 的自动合并功能。当分支保护规则要求的所有测试和批准全部通过时,拉取请求即可合并。

更多信息请参阅 自动合并拉取请求管理分支保护规则

您也可以使用 GitHub Actions 与 GitHub CLI。下面示例展示了自动合并所有对 my-dependency 的补丁更新。

YAML
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
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}}

注意

如果您使用状态检查来测试拉取请求,建议为 Dependabot 拉取请求的目标分支启用 合并前须通过状态检查。此分支保护规则可确保在 所有必需的状态检查均通过之前不会合并拉取请求。更多信息请参阅 管理分支保护规则

Dependabot 与 GitHub Actions 策略

通常,工作流是否能够在仓库中运行取决于 GitHub Actions 的 策略检查,以及组织或仓库层面是否 启用了 GitHub Actions。这些控制可以限制工作流的运行——尤其是在外部 Action 被阻止或 GitHub Actions 完全被禁用的情况下。

然而,只要在仓库中启用了 Dependabot,它的工作流将始终在 GitHub Actions 上运行,绕过策略检查和禁用设置

  • Dependabot 工作流不会受到 Actions 禁用或企业策略限制的阻断。
  • 这些工作流中引用的 Action 也都被允许运行,即使外部 Action 被禁用也是如此。

更多信息请参阅 关于 GitHub Actions Runner 上的 Dependabot

调查工作流运行失败

如果工作流运行失败,请检查以下内容:

  • 仅在正确的触发者触发时运行工作流。
  • pull_request 检出了正确的 ref
  • 您的密钥应存放在 Dependabot 的密钥中,而非 GitHub Actions 的密钥中。
  • 您拥有具有正确权限的 GITHUB_TOKEN

有关编写和调试 GitHub Actions 的信息,请参阅 编写工作流

更多帮助排查工作流问题的技巧,请参见 GitHub Actions 上的 Dependabot 疑难解答

© . This site is unofficial and not affiliated with GitHub, Inc.