GitHub Actions 工作流配置与排错
GitHub Actions 用 .github/workflows/*.yml 描述流水线,事件触发后在 GitHub 托管的 Runner 或自托管 Runner 上执行 Job。它不是一个本地命令,调试常借助 gh 和 act。
mkdir -p .github/workflows
gh workflow list
gh workflow run ci.yml
gh run list --limit 10
gh run watch
gh run view --log-failed
gh run rerun <run-id> --failed
最小工作流:
name: CI
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node: [22, 24]
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm test
.github/workflows/ 根目录;子目录里的 YAML 不会被执行。on 在 YAML 1.1 里可能被解析成布尔值;用引号或保持标准写法,提交前本地校验。GITHUB_TOKEN 的默认权限随仓库设置变化;需要写操作时显式声明 permissions。pull_request。pull_request_target 会在目标仓库上下文运行,处理不可信代码时风险极高。fail-fast: false。gh — GitHub CLIact — 本地运行工作流gitlab-ci — GitLab 的同类方案合上页面,先回答,再展开对照。
.github/workflows/ 的子目录里会执行吗?.github/workflows/ 根目录,子目录里的 YAML 不会被执行。pull_request。pull_request_target 处理不可信代码风险极高。