本文目录导读:

Dependabot 是 GitHub 提供的自动化依赖更新工具,在 PHP 项目中主要通过 Composer 来使用,以下是完整的配置和使用指南:
基础配置
在项目根目录创建 .github/dependabot.yml 文件:
version: 2
updates:
# 为 Composer (PHP) 配置更新
- package-ecosystem: "composer"
directory: "/" # composer.json 所在目录
schedule:
interval: "weekly" # 更新频率: daily|weekly|monthly
day: "monday" # 可选:指定星期几
time: "09:00" # 可选:指定时间
timezone: "Asia/Shanghai"
open-pull-requests-limit: 10 # 最多打开的 PR 数量
labels:
- "dependencies" # 给 PR 添加标签
- "php"
reviewers: # 指定审查者
- "your-github-username"
assignees: # 指定被分配者
- "your-github-username"
versioning-strategy: "auto" # 版本策略
commit-message:
prefix: "composer" # commit message 前缀
include: "scope" # 是否包含包的名称
版本更新策略
updates:
- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "daily"
# 只更新直接依赖
allow:
- dependency-type: "direct"
# 忽略指定包
ignore:
- dependency-name: "laravel/framework"
versions: ["<5.x", ">=6.0"]
- dependency-name: "phpunit/phpunit"
update-types: ["version-update:semver-major"] # 忽略大版本更新
# 更新 vendor 目录(加入版本控制的特殊情况)
vendor: true
私有仓库认证
对于使用私有仓库作为依赖源的情况,需要在 GitHub 设置中添加密钥:
通过 COMPOSER_AUTH 环境变量:
updates:
- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "weekly"
# 使用平台认证
insecure-external-code-execution: "never"
在 GitHub 仓库 Settings → Secrets 中添加:
📁 Name: COMPOSER_AUTH
📄 Value: {"github-oauth": {"github.com": "your-oauth-token"}}
自动化配置示例
完整的生产级配置:
# .github/dependabot.yml
version: 2
updates:
- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "weekly"
day: "monday"
time: "06:00"
open-pull-requests-limit: 15
allow:
- dependency-type: "direct"
ignore:
- dependency-name: "symfony/*"
update-types: ["version-update:semver-major"]
- dependency-name: "php"
labels:
- "dependencies"
- "automerge"
target-branch: "main" # 目标分支
# 自动合并(配合 GitHub Actions)
automerge: true
# 分组更新
groups:
symfony:
patterns:
- "symfony/*"
applies-to: "version-updates"
# 自定义提交信息
commit-message:
prefix: "chore(deps)"
include: "scope"
CI/CD 自动化合并
创建 GitHub Actions 工作流 .github/workflows/dependabot-auto-merge.yml:
name: Dependabot auto-merge
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
auto-merge:
if: github.actor == 'dependabot[bot]'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: mbstring, intl, pdo_mysql, xml
- name: Install dependencies
run: composer install --no-interaction --prefer-dist
- name: Run tests
run: vendor/bin/phpunit
- name: Enable auto-merge
if: success()
run: |
gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{ github.event.pull_request.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
安全更新
启用安全更新(默认开启):
updates:
- package-ecosystem: "composer"
directory: "/"
# 自动安全更新(无需配置,默认开启)
schedule:
interval: "daily"
# 只针对安全漏洞(可选)
security-updates-only: true
常见问题
查看 Dependabot 状态:
- 在 GitHub 仓库 → Insights → Dependency graph → Dependabot
- 查看所有更新 PR 的状态
手动触发:
# 通过 GitHub CLI gh workflow run dependabot.yml
更新本地项目:
# 手动检查 composer 更新 composer update --dry-run composer update composer outdated
验证配置
在 GitHub 上:
- 进入仓库 Settings → Security → Dependabot
- 查看更新日志和配置验证结果
或使用 Dependabot config validator:
curl -L -X POST https://api.github.com/repos/owner/repo/dependabot/alerts \ -H "Authorization: Bearer YOUR_TOKEN"
注意事项
- 避免自动合并:如果项目有严格测试,建议先人工审查再合并
- 更新策略:根据项目稳定性需求选择 daily 或 weekly
- PHP 版本兼容性:确保更新的依赖仍兼容你的 PHP 版本
- composer.lock 文件:务必提交 composer.lock 到版本控制
这样配置后,Dependabot 会自动扫描 composer.json 和 composer.lock,检测依赖更新并按计划创建 PR。