{"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/automate-dependabot-with-actions","title":"使用 Actions 自动化 Dependabot"}],"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/concepts/supply-chain-security/dependabot-on-actions)。\n\n当 GitHub Actions 创建用于更新依赖项的拉取请求时，您可以使用 Dependabot 执行自动化任务。 如果你想执行以下操作，可能会发现这很有用：\n\n* 确保 Dependabot 创建的拉取请求（版本更新和安全更新）包含适合您的工作流程的正确数据，包括标签和名称。\n\n* 触发工作流，将 Dependabot 拉取请求（版本更新和安全更新）纳入您的审核流程，或自动合并。\n\n## 关于 Dependabot 和 GitHub Actions\n\n> \\[!IMPORTANT]\n> 如果为仓库启用了 Dependabot，它将始终在 GitHub Actions 上运行，**并绕过 Actions 策略检查以及仓库或组织级别的禁用设置**。 这可确保在启用 Dependabot 时始终运行安全和版本更新工作流。\n\nDependabot 创建拉取请求，使依赖项保持最新。 您可以在创建这些拉取请求时使用 GitHub Actions 执行自动化任务。 例如，获取其他构件、添加标签、运行测试或修改拉取请求。\n\nDependabot 能够在其拉取请求和评论上触发 GitHub Actions 工作流程；但是，某些事件的处理方式不同。 有关详细信息，请参阅 [对 GitHub Actions 上的 Dependabot 进行故障排除](/zh/code-security/reference/supply-chain-security/troubleshoot-dependabot/dependabot-on-actions)。\n\n> \\[!NOTE]\n> 还可以用于 agentic workflows 依赖项分析、更新摘要和修正建议，同时在确定性 GitHub Actions 工作流中保持安全强制和合并限制。 有关详细信息，请参阅“[创建GitHub代理工作流](/zh/copilot/how-tos/github-agentic-workflows/creating-github-agentic-workflows)”。\n\n下面是几个可借助 GitHub Actions 实现自动化的拉取请求常见场景。\n\n## 获取有关拉取请求的元数据\n\n大多数自动化要求你了解拉取请求内容的信息：依赖项名称是什么，是否为生产依赖项，以及是否为主要、次要或补丁更新。 您可以使用某个操作来检索由 Dependabot 生成的拉取请求中正在更新的依赖项的信息。\n\nExample:\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\nExample:\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/how-tos/merge-and-close-pull-requests/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如果目标分支使用合并队列，则内置 `GITHUB_TOKEN` 无法向队列添加拉取请求。 在这种情况下，必须使用具有合并权限的 personal access token 或 GitHub App 令牌对工作流进行身份验证，并在 `GITHUB_TOKEN` 步骤中使用该令牌替代 `gh pr merge`。\n\n## Dependabot 和 GitHub Actions 策略\n\n通常，工作流是否可以在存储库中运行取决于 GitHub Actions**策略检查** ，以及是在 GitHub Actions 组织级别还是存储库级别 **启用** 。 这些控件可以限制工作流的运行，尤其是在外部操作被阻止或 GitHub Actions 完全禁用时。\n\n但是，当为某个存储库启用 Dependabot 时，其工作流将始终在 GitHub Actions 上运行，**绕过 Actions 策略检查和禁用限制**。\n\n* Dependabot 工作流不受操作禁用或企业策略限制阻止。\n* 即使禁止外部操作，这些工作流中引用的操作也仍允许运行。\n\n有关详细信息，请参阅“[GitHub Actions 运行器上的 Dependabot](/zh/code-security/concepts/supply-chain-security/dependabot-on-actions)”。\n\n## 调查失败的工作流运行\n\n如果您的工作流程运行失败，请检查以下情况：\n\n* 只有当正确的角色触发工作流程时，才运行工作流程。\n* 你正在检查 `ref` 的正确 `pull_request` 值。\n* 您的机密信息以 Dependabot 机密的形式提供，而不是以 GitHub Actions 机密的形式提供。\n* 你有一个具有适当权限的 `GITHUB_TOKEN`。\n\n有关编写和调试 GitHub Actions的信息，请参阅 [撰写工作流程](/zh/actions/how-tos/write-workflows)。\n\n有关帮助解决工作流问题的更多提示，请参阅 [对 GitHub Actions 上的 Dependabot 进行故障排除](/zh/code-security/reference/supply-chain-security/troubleshoot-dependabot/dependabot-on-actions)。"}