CI/CD 最佳实践:用 GitHub Actions 构建自动化部署流水线
CI CD Best Practices Building Automated Deployment Pipelines with GitHub Actions
| iDev Team | 2026-08-15T05:32:00
手动部署容易出错且耗时。本文分享 iDev 团队使用 GitHub Actions 构建 CI/CD 流水线的完整实践,覆盖自动测试、构建、部署和回滚。
Manual deployment is error-prone and time-consuming. This article shares iDev's complete CI/CD pipeline practice with GitHub Actions, covering automated testing, building, deployment, and rollback.
为什么需要 CI/CD 在 iDev 早期,部署流程是这样的:本地打包 → SCP 上传到服务器 → SSH 登录重启服务。每次部署需要 15 分钟,而且经常因为忘记切换环境变量或遗漏某个步骤导致线上事故。 引入 CI/CD 后,部署变成了:git push → 自动运行测试 → 自动构建 → 自动部署。全程零人工干预,5 分钟完成。 GitHub Actions 基础 GitHub Actions 的核心概念是 Workflow(工作流),由 YAML 文件定义,存放在 .github/workflows/ 目录。每个 Workflow 包含一个或多个 Job,每个 Job 包含多个 Step。 前端项目 CI/CD 配置 name: Deploy Frontend on: push: branches: [main] jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: { node-version: 20 } - run: npm ci - run: npm run lint - run: npm test -- --coverage - run: npm run build - name: Deploy to server uses: appleboy/scp-action@v0.1.7 with: host: ${{ secrets.SERVER_HOST }} username: root key: ${{ secrets.SSH_KEY }} source: dist/ target: /www/wwwroot/mysite/ 后端项目 CI/CD 配置 Spring Boot 项目的流水线类似,但增加了 Maven 构建和 JAR 部署步骤。关键点:使用 GitHub Secrets 存储数据库密码和 JWT 密钥,避免硬编码。 进阶实践 分支策略:main 分支自动部署到生产环境,develop 分支部署到测试环境 缓存优化:使用 actions/cache 缓存 node_modules 和 Maven 依赖,构建速度提升 50% 部署通知:部署成功/失败自动发送消息到团队 Slack/飞书频道 回滚机制:保留最近 5 个版本的制品,支持一键回滚 健康检查:部署后自动请求健康检查端点,确认服务正常启动 成本 GitHub Actions 对公开仓库完全免费,私有仓库每月有 2,000 分钟免费额度。对大多数中小项目来说,免费额度绑绑有余。
Why CI/CD In iDev's early days, deployment looked like this: local build → SCP upload to server → SSH login to restart service. Each deployment took 15 minutes and frequently caused production incidents from forgotten environment variables or missed steps. With CI/CD: git push → auto-test → auto-build → auto-deploy. Zero manual intervention, done in 5 minutes. GitHub Actions Basics GitHub Actions centers on Workflows defined in YAML files under .github/workflows/. Each Workflow contains one or more Jobs, each with multiple Steps. Advanced Practices Branch Strategy: main auto-deploys to production, develop deploys to staging Cache Optimization: actions/cache for node_modules and Maven dependencies, 50% faster builds Deploy Notifications: Auto-send success/failure messages to team Slack/Lark channels Rollback Mechanism: Keep last 5 version artifacts for one-click rollback Health Checks: Auto-ping health endpoint post-deploy to confirm service is running Cost GitHub Actions is completely free for public repos. Private repos get 2,000 free minutes monthly — more than enough for most small to medium projects.