把「提交代码 → 有人手动敲命令」换成「push 就自动跑」,只差一个 .github/workflows/ 下的 YAML 文件。先把模型和语法钉住,再给一条能用的 push 流水线。
模型:workflow → event → job → step
- workflow 是
.github/workflows/ 下的一个 YAML 文件;event 由 on 声明;job 默认并行(要串行只能靠 needs),每个 job 独占一台干净 runner;step 在 job 内顺序执行,要么 uses 一个 action,要么 run 一段 shell。 - 每个 step 是独立进程,
export 的变量不会带到下一步;跨 step 传值走 $GITHUB_ENV,或给 step 加 id 后用 steps.<id>.outputs.*。action 分 JavaScript / composite / Docker 三类,Docker action 只能在 Linux runner 跑;runs-on 选 runner,GitHub 托管镜像覆盖 Ubuntu / Windows / macOS(标签形如 ubuntu-latest、windows-latest、macos-latest),-latest 指「当前稳定镜像」而非最新 OS,要可复现就写 ubuntu-24.04。
最小可用工作流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| name: CI on: push: { branches: [ main ] } pull_request: permissions: contents: read jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: { node-version: 22, cache: npm } - run: npm ci - run: npm test
|
branches / paths 是 glob(*、**、?)不是正则;负向模式 - '!docs/**' 必须与正向模式共存,只想排除时用 paths-ignore / branches-ignore。pull_request 默认跑在 PR 的 merge ref(refs/pull/<n>/merge)上,和 push 的 github.ref 不是一回事。
常用触发器
1 2 3 4 5 6 7 8
| on: push: { branches: [ main, 'release/**' ], paths: [ 'src/**', 'package.json' ] } schedule: - cron: '30 5 * * 1-3' workflow_dispatch: inputs: target: { description: 部署目标, type: choice, default: staging, options: [staging, production] } release: { types: [ published ] }
|
schedule 两条硬约束:cron 按 UTC 计算;只在默认分支执行,功能分支里改 cron 不生效。公开仓库 60 天无活动自动停用;整点等高负载时段可能延迟甚至丢队列。workflow_dispatch 的 inputs 类型有 boolean / choice / number / environment / string,取值走 inputs 上下文(${{ inputs.target }}),不是 github.event.inputs;手动入口只在工作流文件进入默认分支后出现。if 里可省略 ${{ }},但表达式以 ! 开头时必须写(YAML 里 ! 是标签记法):if: ${{ !startsWith(github.ref, 'refs/tags/') }}。
上下文与表达式
github 提供 event_name、ref 与 ref_name(短名,未合并 PR 上是 <PR 号>/merge)、sha、repository、actor、workspace;needs.<job>.outputs.* 取上游输出,steps.<id>.outputs.* 取本 job 内 step 输出。env / secrets / matrix 分别是 env 字段变量、密钥、矩阵当前取值。- 属性值默认按字符串处理,数值比较要
fromJSON() 转类型;hashFiles()、contains()、format() 是常用函数。
矩阵:strategy.matrix
1 2 3 4 5 6 7 8
| strategy: fail-fast: false matrix: os: [ ubuntu-latest, macos-latest ] go: [ '1.23', '1.24' ] exclude: [ { os: macos-latest, go: '1.23' } ] include: - { os: ubuntu-latest, go: '1.24', experimental: true }
|
- 矩阵值用
${{ matrix.os }} 在 runs-on、with 里引用;include 在 exclude 之后处理,能把排掉的组合加回来;单 run 矩阵上限 256 个 job。
job 之间:needs、outputs、artifact
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| jobs: build: runs-on: ubuntu-latest outputs: { image: ${{ steps.pack.outputs.image }} } steps: - { id: pack, run: "echo \"image=registry.example.com/app:${GITHUB_SHA}\" >> \"$GITHUB_OUTPUT\"" } - uses: actions/upload-artifact@v4 with: { name: dist, path: dist/ } deploy: needs: build runs-on: ubuntu-latest steps: - uses: actions/download-artifact@v4 with: { name: dist } - run: ./deploy.sh "${{ needs.build.outputs.image }}"
|
needs 指向的 job 失败或被跳过,下游默认也跳过;要无条件执行,if 里加 always()。outputs 只适合小文本(单 job 上限 1 MB、整条 run 50 MB),二进制产物走 artifact。upload-artifact@v4 起 artifact 不可变,同名不能被多个 job 重复上传(矩阵里拼 ${{ matrix.x }});v1/v2/v3 已停用,2025-01-30 后 v3 直接失败。
secrets
1 2 3
| steps: - run: ./deploy.sh env: { API_TOKEN: "${{ secrets.API_TOKEN }}" }
|
- 在仓库 Settings → Secrets and variables → Actions 添加。fork 提交的 PR 拿不到 secrets(
GITHUB_TOKEN 例外,但被降为只读),「PR 触发就部署生产」既跑不通也不该这么设计。 - secrets 不能在
if: 里直接引用,要判断「有没有配」先落到 job 级 env 再和空串比较;也不要 echo 密钥——脱敏只覆盖已登记 secret 的字面值,编码或拼接后的形式可能原样进日志。
缓存:actions/cache
1 2 3 4 5 6
| - uses: actions/cache@v4 with: path: ~/.cache/go-build key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go-
|
hashFiles() 入参是相对 GITHUB_WORKSPACE 的 glob,对每个匹配文件算 SHA-256 再合成总 hash;没有匹配文件时返回空串——key 出现 ..- 这种残缺形式就是「永不命中」的典型原因。key 上限 512 字符。只有 key 精确相等才算 hit,否则按 restore-keys 前缀匹配取最近创建的一条;可见范围是当前分支加默认分支,PR 的写入落在 merge ref 上;条目 7 天未访问会被清理,单仓库默认额度 10 GB,缓存不做安全校验,不要往里放凭据。是否命中看 step 日志里的 Cache restored from key: 与 Cache saved with key: 两行,出现 Cache not found for input keys 就是 miss。
permissions
GITHUB_TOKEN 默认权限由企业 / 组织 / 仓库设置决定,老仓库常见读写全开。在 workflow 或 job 级只列需要的 scope 最省事:**一旦显式列出,未列出的 scope 全部置为 none**;permissions: {} 等于全关。只有写评论、发 release、推 Pages 之类动作才需要 write;fork PR 触发的 run 申请 write 通常被降级为 read。
常见报错
- workflow 显示无效:YAML 缩进 / 结构错,本地先用编辑器 schema 或
yamllint 过一遍;报 Invalid workflow file 并指向 if 行,多是表达式以 ! 开头却没写 ${{ }}。 - job 一直排队不结束:
runs-on 标签不存在或写错(多空格、ubuntu-22 这类不存在的标签),自托管 runner 的标签必须一字不差。 uses: owner/repo@master:不可复现,作者推一次提交行为就变;至少固定主版本 @v4,安全敏感的 action 固定 commit SHA。Resource not accessible by integration:几乎都是 GITHUB_TOKEN 权限不够——建 release 要 contents: write,评论 PR 要 pull-requests: write。缓存永不命中则先看 key 里是否混了 github.run_id 这类每次都变的值,或 hashFiles() 的 glob 没匹配到文件。
参考