github-actions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGitHub Actions
GitHub Actions
Overview
概述
GitHub Actions is a CI/CD platform that automates build, test, and deployment pipelines directly from GitHub repositories. Workflows are YAML files in triggered by events like pushes, pull requests, schedules, or manual dispatch. Each workflow contains one or more jobs that run on GitHub-hosted or self-hosted runners.
.github/workflows/When to use: Automated testing, continuous deployment, release automation, scheduled tasks, multi-platform builds, dependency updates, container publishing, code quality checks, security scanning.
When NOT to use: Long-running services (use a proper hosting platform), heavy compute tasks exceeding runner limits (6-hour job timeout), tasks requiring persistent state between runs (use external storage), real-time event processing (use webhooks with a server).
GitHub Actions 是一款CI/CD平台,可直接从GitHub仓库中自动化构建、测试和部署流水线。工作流是存储在目录下的YAML文件,由推送、拉取请求、定时任务或手动触发等事件触发。每个工作流包含一个或多个在GitHub托管或自托管运行器上执行的任务。
.github/workflows/适用场景: 自动化测试、持续部署、发布自动化、定时任务、多平台构建、依赖更新、容器发布、代码质量检查、安全扫描。
不适用场景: 长时间运行的服务(请使用专业托管平台)、超过运行器限制的重计算任务(任务超时时间为6小时)、需要在多次运行间保持持久状态的任务(请使用外部存储)、实时事件处理(请搭配服务器使用webhook)。
Quick Reference
快速参考
| Pattern | Syntax / Action | Key Points |
|---|---|---|
| Push trigger | | Filter by branch, path, or tag |
| PR trigger | | Defaults to opened, synchronize, reopened |
| Scheduled trigger | | UTC only, minimum 5-minute interval |
| Manual trigger | | Define typed inputs for manual runs |
| Job dependencies | | Run jobs in sequence or parallel |
| Conditional job | | Expression-based job/step filtering |
| Matrix strategy | | Generates jobs for each combination |
| Dependency cache | | Hash-based keys with restore-keys fallback |
| Setup with cache | | Built-in caching for package managers |
| Upload artifact | | Share data between jobs or preserve outputs |
| Download artifact | | Retrieve artifacts from earlier jobs |
| Reusable workflow | | Called with |
| Composite action | | Bundle multiple steps into one action |
| Concurrency | | Cancel or queue duplicate runs |
| Environment secrets | | Scoped to repo, org, or environment |
| OIDC authentication | | Short-lived tokens for cloud providers |
| Step outputs | | Pass data between steps and jobs |
| Service containers | | Sidecar containers for integration tests |
| Timeout | | Fail fast on hung jobs or steps |
| Attestations | | SLSA build provenance for supply chain |
| 模式 | 语法 / 动作 | 核心要点 |
|---|---|---|
| 推送触发器 | | 按分支、路径或标签筛选 |
| PR触发器 | | 默认触发条件为开启、同步、重新开启 |
| 定时触发器 | | 仅支持UTC时区,最小间隔为5分钟 |
| 手动触发器 | | 为手动运行定义类型化输入 |
| 任务依赖 | | 按顺序或并行运行任务 |
| 条件任务 | | 基于表达式的任务/步骤筛选 |
| 矩阵策略 | | 为每个组合生成任务 |
| 依赖缓存 | | 基于哈希的缓存键,支持restore-keys回退 |
| 带缓存的环境配置 | | 包管理器内置缓存支持 |
| 上传制品 | | 在任务间共享数据或保存输出 |
| 下载制品 | | 从之前的任务中获取制品 |
| 可复用工作流 | | 通过 |
| 复合动作 | | 将多个步骤打包为单个动作 |
| 并发控制 | | 取消或排队重复运行 |
| 环境密钥 | | 作用域覆盖仓库、组织或环境 |
| OIDC认证 | | 用于云服务商的短期令牌 |
| 步骤输出 | | 在步骤和任务间传递数据 |
| 服务容器 | | 用于集成测试的边车容器 |
| 超时设置 | | 针对挂起的任务或步骤快速失败 |
| 构建证明 | | 用于供应链的SLSA构建证明 |
Expressions and Contexts
表达式与上下文
| Context | Example | Description |
|---|---|---|
| | Event metadata, repo info, actor |
| | Environment variables at current scope |
| | Encrypted secrets (masked in logs) |
| | Workflow dispatch or reusable inputs |
| | Current matrix combination values |
| | Outputs from previous steps |
| | Outputs from dependent jobs |
| | Runner environment info |
| | Repository or org configuration variables |
| 上下文 | 示例 | 说明 |
|---|---|---|
| | 事件元数据、仓库信息、执行者 |
| | 当前作用域下的环境变量 |
| | 加密密钥(在日志中会被掩码处理) |
| | 工作流触发或可复用工作流的输入参数 |
| | 当前矩阵组合的值 |
| | 之前步骤的输出 |
| | 依赖任务的输出 |
| | 运行器环境信息 |
| | 仓库或组织的配置变量 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Using | Pin to current major version ( |
Missing | Set on checkout when using custom tokens or OIDC |
Broad | Set |
| Cache key without dependency file hash | Include |
Secrets in | Secrets cannot be used in |
Using | Never run PR code with write permissions from |
| Not cancelling stale runs | Use |
| Storing structured data as a single secret | Create individual secrets per value for proper log redaction |
| Referencing action tags without SHA pinning | Pin third-party actions to full commit SHA for supply chain safety |
| Hardcoding runner OS in scripts | Use |
Using | Always provide restore-keys for partial cache matches |
Interpolating user input in | Pass untrusted values through |
No | Set explicit timeouts to fail fast on hung processes |
Using | Combine with status checks: |
| 错误做法 | 正确方式 |
|---|---|
使用 | 固定到当前主版本( |
缺少 | 当使用自定义令牌或OIDC时,在checkout步骤中设置该参数 |
在工作流级别设置宽泛的 | 在工作流级别设置 |
| 缓存密钥中不包含依赖文件哈希 | 在缓存密钥中包含 |
在 | 密钥不能直接在 |
随意使用 | 永远不要使用 |
| 未取消过时的运行 | 使用 |
| 将结构化数据存储为单个密钥 | 为每个值创建单独的密钥,以确保日志正确脱敏 |
| 引用动作标签但未固定SHA | 为供应链安全,将第三方动作固定到完整的提交SHA |
| 在脚本中硬编码运行器OS | 使用 |
使用 | 始终提供restore-keys以支持部分缓存匹配 |
在 | 通过 |
未为任务设置 | 设置明确的超时时间,以便挂起的进程快速失败 |
未限定范围使用 | 结合状态检查使用: |
Delegation
任务委托
- Workflow debugging: Use agent to inspect workflow run logs
Explore - Security auditing: Use agent to review permissions and secret usage
Task - Code review: Delegate to agent for workflow PR reviews
code-reviewer
- 工作流调试:使用工具查看工作流运行日志
Explore - 安全审计:使用工具审查权限和密钥使用情况
Task - 代码审查:将工作流PR审查任务委托给工具
code-reviewer
References
参考资料
- Workflow syntax, triggers, jobs, steps, and concurrency
- Caching strategies and artifact management
- Matrix strategies, reusable workflows, and composite actions
- Security, secrets, OIDC, and permissions hardening
- 工作流语法、触发器、任务、步骤及并发控制
- 缓存策略与制品管理
- 矩阵策略、可复用工作流与复合动作
- 安全、密钥、OIDC及权限加固