github-actions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGitHub Actions Code Review Rules
GitHub Actions 代码审查规则
Security (Critical)
安全(关键)
- Pin actions to full commit SHA (not or
@v1)@main - Use minimal block (principle of least privilege)
permissions - Never echo secrets or use them in URLs
- Use instead of PATs when possible
secrets.GITHUB_TOKEN - Audit third-party actions before use
- Review expressions () for injection risks; never interpolate untrusted user input
${{ }} - Validate all inputs to reusable workflows and custom actions
- 将 actions 固定到完整的提交 SHA(而非 或
@v1)@main - 使用最小化的 块(最小权限原则)
permissions - 切勿输出 secrets 或将其用于 URL 中
- 尽可能使用 而非 PATs
secrets.GITHUB_TOKEN - 使用第三方 actions 前先进行审计
- 检查表达式()的注入风险;切勿插入不可信的用户输入
${{ }} - 验证可复用工作流和自定义 actions 的所有输入
Permissions
权限设置
yaml
permissions:
contents: read # Minimal by default
# Add only what's needed:
# pull-requests: write
# issues: writeyaml
permissions:
contents: read # Minimal by default
# Add only what's needed:
# pull-requests: write
# issues: writeSecrets
密钥管理
- Store secrets in repository/organization secrets
- Use environments for production secrets with approvals
- Don't pass secrets as command arguments (visible in logs)
- Mask sensitive output with
::add-mask:: - Never write secrets to files or artifacts (can be exposed)
- Avoid passing secrets via environment variables unless absolutely required
- Secrets in env vars can be visible in process listings
- 将密钥存储在仓库/组织的 secrets 中
- 对生产环境密钥使用带审批流程的环境配置
- 切勿将密钥作为命令参数传递(会在日志中可见)
- 使用 屏蔽敏感输出
::add-mask:: - 切勿将密钥写入文件或制品(可能会泄露)
- 除非绝对必要,否则避免通过环境变量传递密钥
- 环境变量中的密钥可能会在进程列表中暴露
Performance
性能优化
- Use caching for dependencies (or built-in)
actions/cache - Run independent jobs in parallel
- Use to cancel redundant runs
concurrency - Consider self-hosted runners for heavy workloads
- 对依赖项使用缓存(或内置缓存功能)
actions/cache - 并行运行独立的任务
- 使用 取消冗余的运行
concurrency - 对于繁重的工作负载,考虑使用自托管运行器
Workflow Structure
工作流结构
- Use reusable workflows for common patterns
- Use composite actions for shared steps
- Set appropriate to prevent hung jobs
timeout-minutes - Use conditions to skip unnecessary jobs
if: - Separate CI (testing), CD (deployments), and PR checks into distinct workflows
- Use environments to distinguish between dev, staging, and production
- Avoid mixing all concerns in a single monolithic workflow
- 对通用模式使用可复用工作流
- 对共享步骤使用复合 actions
- 设置合适的 以防止任务挂起
timeout-minutes - 使用 条件跳过不必要的任务
if: - 将CI(测试)、CD(部署)和PR检查拆分为独立的工作流
- 使用环境来区分开发、预发布和生产环境
- 避免将所有关注点混在一个单一的大型工作流中
Triggers
触发器
- Be specific with and
pathsfiltersbranches - Use for manual triggers
workflow_dispatch - Consider security implications
pull_request_target
- 明确设置 和
paths过滤器branches - 使用 进行手动触发
workflow_dispatch - 考虑 的安全影响
pull_request_target
Common Anti-patterns
常见反模式
- Avoid with
actions/checkoutunless neededpersist-credentials: true - Avoid running on to all branches
push - Avoid hardcoding versions that need updates
- 除非必要,否则避免使用带有 的
persist-credentials: trueactions/checkout - 避免在所有分支的 事件上运行
push - 避免硬编码需要更新的版本
Action Updates and Maintenance
Action 更新与维护
- Monitor pinned action SHAs for security fixes
- Subscribe to security advisories for actions you use
- Update actions regularly to get new features and fixes
- Document why specific SHAs are pinned (security, stability)
- Consider using Dependabot for action version updates
- 监控已固定的 action SHA 是否有安全修复
- 订阅你所使用的 actions 的安全公告
- 定期更新 actions 以获取新功能和修复
- 记录固定特定 SHA 的原因(安全、稳定性)
- 考虑使用 Dependabot 进行 action 版本更新
Testing and Validation
测试与验证
- Lint workflows with tools like
actionlint - Test complex workflows in feature branches before merging
- Validate workflow syntax before committing
- Use workflow templates for consistency
- Add job-level tests for workflow logic validation
- 使用 等工具对工作流进行代码检查
actionlint - 在合并前,在特性分支中测试复杂工作流
- 提交前验证工作流语法
- 使用工作流模板以保持一致性
- 添加任务级测试以验证工作流逻辑
Error Handling
错误处理
- Use as default (explicit failure)
continue-on-error: false - Set for matrix jobs to stop on first failure
fail-fast: true - Only use when failure is acceptable
continue-on-error: true - Provide clear error messages in job outputs
- Use status checks to ensure critical jobs pass
- 默认使用 (显式失败)
continue-on-error: false - 对于矩阵任务,设置 以在首次失败时停止
fail-fast: true - 仅当失败可接受时使用
continue-on-error: true - 在任务输出中提供清晰的错误信息
- 使用状态检查确保关键任务通过
Documentation
文档
- Add inline comments for complex workflow logic
- Document workflow purpose and triggers
- Maintain workflow README or documentation
- Explain environment variables and their usage
- Document required secret names and their purpose (never include actual secret values)
- 为复杂的工作流逻辑添加内联注释
- 记录工作流的用途和触发器
- 维护工作流的 README 或文档
- 说明环境变量及其用法
- 记录所需的密钥名称及其用途(切勿包含实际的密钥值)