GitHub Actions 入门:让检查自动跑起来
创建最小 CI workflow,让 PR 在合并前自动安装依赖、测试并构建。
持续集成最先解决的问题不是复杂自动化,而是让每个 Pull Request 都自动运行同一套检查,减少“我忘记跑测试”的偶然性。
你会学到什么
理解 workflow、job、step 和 runner。
在 PR 上自动运行检查。
避免在日志中泄漏机密。
动手步骤
从仓库已有的稳定命令开始。
在 .github/workflows 添加 workflow 文件。
使用 lockfile 安装依赖。
在 PR 中观察失败日志并修复第一条错误。
最小示例
name: CI
on: [pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: 22 }
- run: npm ci
- run: npm run build --if-present容易踩坑的地方
不要输出 Token 或 .env。
第三方 action 要固定可信版本。
CI 失败时先读第一条真实错误。
完成前检查
PR 每次更新都触发。
lockfile 参与安装。
失败日志没有机密。
延伸阅读
GitHub Docs:Understand GitHub Actions:https://docs.github.com/en/actions/get-started/understand-github-actions
GitHub Docs:Writing workflows:https://docs.github.com/en/actions/writing-workflows
DISCUSSION
文章讨论
0 条评论
