系列:版本控制基础
本地跑检查很重要,但只靠人记不住。GitHub Actions 可以在 push 或 PR 时自动跑构建和测试,挡住一部分低级错误。
工作流文件
创建 .github/workflows/verify.yml:
name: Verify
on: pull_request: push: branches: [main]
jobs: verify: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 22 cache: npm - run: npm ci - run: npx playwright install --with-deps chromium - run: npm run verify这类检查适合和 GitHub Pull Request 工作流入门 配合。
为什么用 npm ci
CI 里优先用 npm ci,它严格按 package-lock.json 安装。这样本地和 CI 的依赖更一致。
常见问题
Playwright 在 CI 里需要浏览器依赖,所以要跑:
npx playwright install --with-deps chromium如果构建需要 Cloudflare 或外部服务变量,把它们放到 GitHub Secrets,不要写进仓库。
总结
CI 不会保证代码一定没问题,但能保证每次合并前至少跑过同一套检查。提交规范看 Git Commit 规范:怎么写清楚提交信息,分支策略看 Git 分支管理指南:main、dev、feature、release 怎么用。
如果检查流程已经稳定,下一步可以把发布也接进流水线,参考 GitHub Actions 自动部署实战:从检查到发布的完整流程。更多配置看 GitHub Actions 文档。