{"meta":{"title":"通过 GitHub Actions 自动化 Dependabot","intro":"如何使用 GitHub Actions 来自动执行常见 Dependabot 相关任务的示例。","product":"安全性和代码质量","breadcrumbs":[{"href":"/zh/code-security","title":"安全性和代码质量"},{"href":"/zh/code-security/tutorials","title":"Tutorials"},{"href":"/zh/code-security/tutorials/secure-your-dependencies","title":"保护依赖项"},{"href":"/zh/code-security/tutorials/secure-your-dependencies/automating-dependabot-with-github-actions","title":"结合使用 Dependabot 与 Actions"}],"documentType":"article"},"body":"# 通过 GitHub Actions 自动化 Dependabot\n\n如何使用 GitHub Actions 来自动执行常见 Dependabot 相关任务的示例。\n\n> \\[!NOTE] 本文解释了如何使用 Dependabot 自动执行与 GitHub Actions 相关的任务。 有关使用 Dependabot updates 运行 GitHub Actions 的详细信息，请参阅 [关于 GitHub Actions 运行程序上的 Dependabot](/zh/code-security/dependabot/working-with-dependabot/about-dependabot-on-github-actions-runners)。\n\n当 GitHub Actions 创建拉取请求以更新依赖项时，可以使用 Dependabot 执行自动化任务。 如果你想执行以下操作，可能会发现这很有用：\n\n* 确保使用正确的数据为你的工作流程创建 Dependabot 拉取请求（版本更新和安全更新），包括标签和名称。\n\n* 触发工作流以将 Dependabot 拉取请求（版本更新和安全更新）发送到你的评审过程或自动合并。\n\n## 关于 Dependabot 与 GitHub Actions\n\n> \\[!IMPORTANT]\n> 如果为存储库启用了 Dependabot，它将始终在 GitHub Actions 上运行，并**绕过存储库或组织级别的操作策略检查和禁用**。 这可确保在启用 Dependabot 时始终运行安全和版本更新工作流。\n\nDependabot 会创建拉取请求以使你的依赖项保持最新状态。 创建这些拉取请求时，可以使用 GitHub Actions 执行自动化任务。 例如，获取其他构件、添加标签、运行测试或修改拉取请求。\n\nDependabot 能够在其拉取请求和评论上触发 GitHub Actions 工作流程；但是，某些事件的处理方式不同。 有关详细信息，请参阅 [对 GitHub Actions 上的 Dependabot 进行故障排除](/zh/code-security/dependabot/troubleshooting-dependabot/troubleshooting-dependabot-on-github-actions)。\n\n以下是使用 GitHub Actions 可以实现自动化处理的几种常见的拉取请求场景。\n\n## 获取有关拉取请求的元数据\n\n大多数自动化要求你了解拉取请求内容的信息：依赖项名称是什么，是否为生产依赖项，以及是否为主要、次要或补丁更新。 可以使用操作来检索有关由 Dependabot 生成的拉取请求所更新的依赖项的信息。\n\n示例：\n\n```yaml copy\n# 此工作流使用未经 GitHub 认证的操作。\n# 它们由第三方提供，并受\n# 单独的服务条款、隐私政策和支持\n# 文档。\nname: Dependabot fetch metadata\non: pull_request\n\npermissions:\n  pull-requests: write\n  issues: write\n\njobs:\n  dependabot:\n    runs-on: ubuntu-latest\n    if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'\n    steps:\n      - name: Dependabot metadata\n        id: metadata\n        uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7\n        with:\n          github-token: \"${{ secrets.GITHUB_TOKEN }}\"\n      # The following properties are now available:\n      #  - steps.metadata.outputs.dependency-names\n      #  - steps.metadata.outputs.dependency-type\n      #  - steps.metadata.outputs.update-type\n```\n\n有关详细信息，请参阅 [`dependabot/fetch-metadata`](https://github.com/dependabot/fetch-metadata) 存储库。\n\n## 标记拉取请求\n\n如果你有基于 GitHub 标签的其他自动化或分类工作流程，则可以配置操作以根据提供的元数据分配标签。\n\n使用标签标记所有生产依赖项更新的示例：\n\n```yaml copy\n# 此工作流使用未经 GitHub 认证的操作。\n# 它们由第三方提供，并受\n# 单独的服务条款、隐私政策和支持\n# 文档。\nname: Dependabot auto-label\non: pull_request\n\npermissions:\n  pull-requests: write\n  issues: write\n\njobs:\n  dependabot:\n    runs-on: ubuntu-latest\n    if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'\n    steps:\n      - name: Dependabot metadata\n        id: metadata\n        uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7\n        with:\n          github-token: \"${{ secrets.GITHUB_TOKEN }}\"\n      - name: Add a label for all production dependencies\n        if: steps.metadata.outputs.dependency-type == 'direct:production'\n        run: gh pr edit \"$PR_URL\" --add-label \"production\"\n        env:\n          PR_URL: ${{github.event.pull_request.html_url}}\n```\n\n## 自动批准拉取请求\n\n可以通过在工作流中使用 Dependabot 来自动批准 GitHub CLI 拉取请求。\n\n示例：\n\n```yaml copy\n# 此工作流使用未经 GitHub 认证的操作。\n# 它们由第三方提供，并受\n# 单独的服务条款、隐私政策和支持\n# 文档。\nname: Dependabot auto-approve\non: pull_request\n\npermissions:\n  pull-requests: write\n\njobs:\n  dependabot:\n    runs-on: ubuntu-latest\n    if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'\n    steps:\n      - name: Dependabot metadata\n        id: metadata\n        uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7\n        with:\n          github-token: \"${{ secrets.GITHUB_TOKEN }}\"\n      - name: Approve a PR\n        run: gh pr review --approve \"$PR_URL\"\n        env:\n          PR_URL: ${{github.event.pull_request.html_url}}\n          GH_TOKEN: ${{secrets.GITHUB_TOKEN}}\n```\n\n## 在拉取请求上启用自动合并\n\n如果要允许维护者标记某些拉取请求以进行自动合并，可以使用 GitHub 的自动合并功能。 这样，如果分支保护规则所需的任何测试和批准都成功满足，拉取请求就可合并。\n\n有关详细信息，请参阅 [自动合并拉取请求](/zh/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/automatically-merging-a-pull-request) 和 [管理分支保护规则](/zh/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/managing-a-branch-protection-rule)。\n\n可以改为使用 GitHub Actions 和 GitHub CLI。 以下示例会将所有补丁更新自动合并为 `my-dependency`：\n\n```yaml copy\n# 此工作流使用未经 GitHub 认证的操作。\n# 它们由第三方提供，并受\n# 单独的服务条款、隐私政策和支持\n# 文档。\nname: Dependabot auto-merge\non: pull_request\n\npermissions:\n  contents: write\n  pull-requests: write\n\njobs:\n  dependabot:\n    runs-on: ubuntu-latest\n    if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'owner/my_repo'\n    steps:\n      - name: Dependabot metadata\n        id: metadata\n        uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7\n        with:\n          github-token: \"${{ secrets.GITHUB_TOKEN }}\"\n      - name: Enable auto-merge for Dependabot PRs\n        if: contains(steps.metadata.outputs.dependency-names, 'my-dependency') && steps.metadata.outputs.update-type == 'version-update:semver-patch'\n        run: gh pr merge --auto --merge \"$PR_URL\"\n        env:\n          PR_URL: ${{github.event.pull_request.html_url}}\n          GH_TOKEN: ${{secrets.GITHUB_TOKEN}}\n```\n\n> \\[!NOTE]\n> 如果使用状态检查来测试拉取请求，则应为 Dependabot 拉取请求的目标分支启用**合并前需要通过状态检查**。 此分支保护规则可确保除非**所有必需的状态检查都通过**，否则不合并拉取请求。 有关详细信息，请参阅“[管理分支保护规则](/zh/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/managing-a-branch-protection-rule)”。\n\n## Dependabot 与 GitHub Actions 策略\n\n```\n          **\n          **\n          **\n          **通常，工作流是否可以在存储库中运行取决于 GitHub Actions 策略检查，以及是在组织级别还是在存储库级别启用 GitHub Actions。 这些控件可能会限制工作流运行，尤其是在外部操作受到阻止或完全禁用了 GitHub Actions 时。\n\n          **\n          **但是当为存储库启用了 Dependabot 时，其工作流将始终在 GitHub Actions 上运行，会同时绕过操作策略检查和禁用措施。\n```\n\n* Dependabot 工作流不受操作禁用或企业策略限制的影响。\n* 即使禁止外部操作，这些工作流中引用的操作也仍允许运行。\n\n有关详细信息，请参阅 [关于 GitHub Actions 运行程序上的 Dependabot](/zh/code-security/dependabot/working-with-dependabot/about-dependabot-on-github-actions-runners)。\n\n## 调查失败的工作流运行\n\n如果您的工作流程运行失败，请检查以下情况：\n\n* 只有当正确的角色触发工作流程时，才运行工作流程。\n* 你正在检查 `ref` 的正确 `pull_request` 值。\n* 您的机密在 Dependabot 机密中可用，而不是作为 GitHub Actions 机密。\n* 你有一个具有适当权限的 `GITHUB_TOKEN`。\n\n有关编写和调试 GitHub Actions 的详细信息，请参阅 [撰写工作流程](/zh/actions/learn-github-actions)。\n\n有关帮助解决工作流问题的更多提示，请参阅 [对 GitHub Actions 上的 Dependabot 进行故障排除](/zh/code-security/dependabot/troubleshooting-dependabot/troubleshooting-dependabot-on-github-actions)。"}