跳至主要内容

配置 Dependabot 版本更新

您可以配置仓库,使 Dependabot 自动更新您使用的包。

谁可以使用此功能?

具有 写入 访问权限的用户

启用 Dependabot 版本更新

您可以通过将 dependabot.yml 配置文件提交到仓库来启用 Dependabot 版本更新。如果您在设置页面启用此功能,GitHub 会创建一个基本文件,您可以编辑它;否则,您可以使用任何文件编辑器创建该文件。

  1. 在 GitHub 上,导航至仓库的主页面。

  2. 在仓库名称下,点击 Settings。如果看不到 “Settings” 选项卡,请选择 下拉菜单,然后点击 Settings

    Screenshot of a repository header showing the tabs. The "Settings" tab is highlighted by a dark orange outline.

  3. 在侧边栏的Security部分,点击 Advanced Security

  4. 在 “Dependabot” 下,位于 “Dependabot version updates” 右侧,点击 Enable(启用),在仓库的 .github 目录中打开一个基本的 dependabot.yml 配置文件。有关可用于自定义 Dependabot 如何维护仓库的选项,请参阅 Dependabot 选项参考

    YAML
    # To get started with Dependabot version updates, you'll need to specify which
    # package ecosystems to update and where the package manifests are located.
    
    version: 2
    updates:
    - package-ecosystem: "" # See documentation for possible values
      directory: "/" # Location of package manifests
      schedule:
        interval: "weekly"
    
  5. 添加 version。此键为必需。文件必须以 version: 2 开头。

  6. 如果您在私有注册表中拥有依赖,可选地添加一个包含身份验证详情的 registries 部分。有关更多信息,请参阅 为 Dependabot 配置对私有注册表的访问

  7. 添加一个 updates 部分,其中为您希望 Dependabot 监视的每个包管理器提供一个条目。此键为必需。您使用它来配置 Dependabot 如何更新项目依赖的版本。每个条目针对特定的包管理器配置更新设置。更多信息请参阅 关于 dependabot.yml 文件Dependabot 选项参考

  8. 对于每个包管理器,使用

    • package-ecosystem 指定使用的包管理器。有关受支持的包管理器的更多信息,请参阅 package-ecosystem
    • directoriesdirectory 用于指定多个清单文件或其他定义文件的位置。更多信息请参阅 为清单文件定义多个位置
    • schedule.interval 用于指定检查新版本的频率。
  9. dependabot.yml 配置文件提交到仓库的 .github 目录中。

示例 dependabot.yml 文件

下面的示例 dependabot.yml 文件为三个包管理器(npm、Docker 和 GitHub Actions)配置了版本更新。当此文件被提交后,Dependabot 会在默认分支上检查清单文件是否存在过时的依赖。如果发现过时的依赖,它会针对默认分支创建拉取请求以更新这些依赖。

YAML
# Basic `dependabot.yml` file with
# minimum configuration for three package managers

version: 2
updates:
  # Enable version updates for npm
  - package-ecosystem: "npm"
    # Look for `package.json` and `lock` files in the `root` directory
    directory: "/"
    # Check the npm registry for updates every day (weekdays)
    schedule:
      interval: "daily"

  # Enable version updates for Docker
  - package-ecosystem: "docker"
    # Look for a `Dockerfile` in the `root` directory
    directory: "/"
    # Check for updates once a week
    schedule:
      interval: "weekly"

  # Enable version updates for GitHub Actions
  - package-ecosystem: "github-actions"
    # Workflow files stored in the default location of `.github/workflows`
    # You don't need to specify `/.github/workflows` for `directory`. You can use `directory: "/"`.
    directory: "/"
    schedule:
      interval: "weekly"

在上面的示例中,如果 Docker 依赖非常陈旧,您可能希望先使用 daily(每日)计划,等依赖全部更新后再切换回每周计划。

在分叉仓库上启用版本更新

如果您想在分叉仓库上启用版本更新,需要额外的步骤。仅有 dependabot.yml 配置文件并不会在分叉仓库上自动启用版本更新。这是为了防止在从原始仓库拉取包含 dependabot.yml 配置文件的更改时,分叉所有者意外地启用版本更新。

在分叉仓库上,您还需要显式启用 Dependabot。

  1. 在 GitHub 上,导航至仓库的主页面。

  2. 在仓库名称下,点击 Settings。如果看不到 “Settings” 选项卡,请选择 下拉菜单,然后点击 Settings

    Screenshot of a repository header showing the tabs. The "Settings" tab is highlighted by a dark orange outline.

  3. 在侧边栏的Security部分,点击 Advanced Security

  4. 在 “Dependabot” 下,位于 “Dependabot version updates” 右侧,点击 Enable(启用),允许 Dependabot 发起版本更新。

接收间接依赖的更新

默认情况下,只有在清单文件中显式定义的直接依赖会被 Dependabot 版本更新保持最新。您可以选择接收在锁定文件中定义的间接依赖的更新。更多信息请参阅 控制 Dependabot 更新哪些依赖

启用对私有依赖的访问

在执行安全或版本更新时,某些生态系统必须能够从其来源解析所有依赖,以验证更新是否成功。如果您的清单或锁定文件中包含任何私有依赖,Dependabot 必须能够访问这些依赖所在的位置。组织所有者可以授予 Dependabot 对同一组织内项目所使用的私有仓库的访问权限。更多信息请参阅 管理组织的安全和分析设置。您可以在仓库的 dependabot.yml 配置文件中配置对私有注册表的访问。更多信息请参阅 为 Dependabot 配置对私有注册表的访问

此外,Dependabot 并未对所有包管理器都支持私有 GitHub 依赖。更多信息请参阅 Dependabot 支持的生态系统和仓库 以及 GitHub 语言支持

检查版本更新的状态

启用版本更新后,仓库的依赖关系图中会出现 Dependabot 选项卡。此选项卡显示 Dependabot 配置监视的包管理器以及最近一次检查新版本的时间。

Screenshot of the Dependency graph page. A tab, titled "Dependabot," is highlighted with an orange outline.

更多信息请参阅 列出已配置用于版本更新的依赖项

禁用 Dependabot 版本更新

您可以通过删除仓库中的 dependabot.yml 文件来完全禁用版本更新。更常见的做法是暂时为一个或多个依赖或包管理器禁用更新。

  • 包管理器:通过将 open-pull-requests-limit: 0 设置为 0,或在配置文件中注释掉相应的 package-ecosystem 来禁用。
  • 特定依赖:通过为要排除在更新之外的包或应用添加 ignore 属性来禁用。

禁用依赖时,您可以使用通配符匹配一组相关库,也可以指定要排除的特定版本。这在您需要阻止对某个库的更新(因为仍在进行对其 API 破坏性变更的适配工作),但仍希望获取该版本的安全修复时特别有用。

示例:为某些依赖禁用版本更新

下面的示例 dependabot.yml 文件展示了多种禁用对某些依赖更新的方式,同时允许其他更新继续进行。

# `dependabot.yml` file with updates
# disabled for Docker and limited for npm

version: 2
updates:
  # Configuration for Dockerfile
  - package-ecosystem: "docker"
    directory: "/"
    schedule:
      interval: "weekly"
      # Disable all pull requests for Docker dependencies
    open-pull-requests-limit: 0

  # Configuration for npm
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    ignore:
      # Ignore updates to packages that start with 'aws'
      # Wildcards match zero or more arbitrary characters
      - dependency-name: "aws*"
      # Ignore some updates to the 'express' package
      - dependency-name: "express"
        # Ignore only new versions for 4.x and 5.x
        versions: ["4.x", "5.x"]
      # For all packages, ignore all patch updates
      - dependency-name: "*"
        update-types: ["version-update:semver-patch"]

有关检查已存在的 ignore 偏好设置的更多信息,请参阅 Dependabot 选项参考

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