用 GitHub Actions 构建完整的 CI/CD 流水线

Building a Complete CI/CD Pipeline with GitHub Actions

| Alex | 2026-08-29T09:07:07

从代码提交到自动部署,用 GitHub Actions 搭建了一套完整的 CI/CD 流水线。分享一下配置过程和一些实用技巧。

Built a complete CI/CD pipeline from code push to auto-deployment using GitHub Actions. Sharing the configuration and practical tips.

之前我们的部署方式很原始:本地打包、scp 上传、ssh 进去重启服务。每次发版都要手动操作十几分钟,而且容易出错。用 GitHub Actions 搭了一套自动化流水线之后,push 代码就自动部署了。 流水线设计 我们的流水线分四个阶段: 代码检查:Lint、格式检查 测试:单元测试 + 集成测试 构建:Maven 打包 / Docker 构建 部署:推送到服务器并重启 核心配置 name: Deploy Pipeline on: push: branches: [main] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 with: java-version: '17' distribution: 'temurin' cache: 'maven' - run: mvn verify build-and-deploy: needs: test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-java@v4 with: java-version: '17' distribution: 'temurin' cache: 'maven' - run: mvn clean package -DskipTests - name: Deploy to server uses: appleboy/scp-action@master with: host: ${{ secrets.SERVER_HOST }} username: ${{ secrets.SERVER_USER }} key: ${{ secrets.SSH_KEY }} source: "target/*.jar" target: "/www/wwwroot/app/" - name: Restart service uses: appleboy/ssh-action@master with: host: ${{ secrets.SERVER_HOST }} username: ${{ secrets.SERVER_USER }} key: ${{ secrets.SSH_KEY }} script: | systemctl restart myapp 实用技巧 1. Maven 缓存 actions/setup-java 自带 Maven 缓存支持,加上 cache: 'maven' 就行。首次构建 3 分钟,后续只要 1 分钟。 2. 并行任务 Lint 和测试可以并行跑,不用串行等待。 3. 环境区分 用 GitHub Environments 区分 staging 和 production,production 环境加上审批保护:只有手动批准后才能部署。 4. 回滚方案 每次部署前先备份当前版本。如果新版本有问题,一条命令就能回滚。 效果 从代码 push 到线上生效,全流程 3 分钟。之前手动操作要 15 分钟还容易出错。团队幸福感直线上升。


Replaced manual deploy (local build → scp → ssh restart) with a fully automated GitHub Actions pipeline. Pipeline Stages Code checks (lint, format) Testing (unit + integration) Build (Maven package) Deploy (SCP + SSH restart) Tips Maven caching (3min → 1min builds), parallel lint/test jobs, environment-based deployment protection, and automated pre-deploy backups for rollback. Result: push-to-production in 3 minutes, down from 15 minutes of manual work.

← Back to News