Dependabot 更新项目依赖
概述
Dependabot 是 GitHub 内置的依赖管理工具,可以自动检测项目中的过时依赖,创建 Pull Request 提示升级。本节介绍如何配置 Dependabot 实现项目依赖的自动更新管理。
Dependabot 功能
| 功能 | 说明 |
|---|---|
| 版本更新 | 自动检测过时依赖并创建 PR |
| 安全更新 | 检测有安全漏洞的依赖并自动修复 |
| 自动合并 | 可配置 CI 通过后自动合并 PR |
启用 Dependabot
方式一:GitHub 网页配置
- 进入仓库 → Settings → Code security
- 启用 Dependabot alerts
- 启用 Dependabot security updates
- 启用 Dependabot version updates
方式二:配置文件(推荐)
# .github/dependabot.yml
version: 2
updates:
# npm/pnpm 依赖
- package-ecosystem: 'npm'
directory: '/'
schedule:
interval: 'weekly' # 每周检查
day: 'monday' # 每周一
open-pull-requests-limit: 5 # 最多 5 个 PR
reviewers:
- 'username' # 指定审核人
labels:
- 'dependencies'
- 'automated'
# GitHub Actions 版本
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: 'monthly' # 每月检查
yaml
配置项说明
| 配置项 | 说明 | 可选值 |
|---|---|---|
package-ecosystem | 包管理器 | npm、pip、maven、github-actions |
directory | 项目路径 | /(根目录) |
schedule.interval | 检查频率 | daily、weekly、monthly |
open-pull-requests-limit | 最大 PR 数 | 数字(默认 5) |
reviewers | PR 审核人 | GitHub 用户名 |
labels | PR 标签 | 自定义标签名 |
Dependabot 工作流程
Dependabot 定期检查依赖版本
↓
发现新版本
↓
创建 Pull Request(自动修改 package.json + lockfile)
↓
CI 运行测试
↓
测试通过 → 人工审核合并 / 自动合并
测试失败 → 关闭 PR,通知维护者
text
自定义域名配置补充
Dependabot 配置过程中涉及的 GitHub Pages 自定义域名步骤:
- Settings → Pages → 输入自定义域名
- 添加 TXT 记录验证域名所有权
- 配置 DNS A 记录或 CNAME 记录
- 等待 HTTPS 自动启用
PR 示例
Dependabot 创建的 PR 通常包含:
Bump element-plus from 2.4.0 to 2.5.0
Release notes:
- Fixed: xxx
- Added: xxx
Changelog:
...
Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself.
text
实践要点
- Dependabot 是 GitHub 内置功能,无需额外安装,通过
.github/dependabot.yml配置 - 支持多种包管理器:npm、pnpm、pip、maven、GitHub Actions 等
- 建议设置
open-pull-requests-limit限制同时打开的 PR 数量 - CI 测试通过后可配置自动合并,减少人工操作
- 安全更新(Security Updates)应始终启用,及时修复已知漏洞
↑