Git Flow 工作流与 GitHub Actions CI/CD 实战
Introduction
现代软件开发离不开高效的代码管理和自动化流程。Git Flow 提供了一套成熟的分支管理模型,适用于有计划发版周期的团队项目;GitHub Actions 则是 GitHub 内置的 CI/CD 工具,可以实现代码提交后的自动化测试、构建和部署。本文结合两者,讲解从代码管理到自动化发布的完整实践。
Git Flow 分支模型
# Git Flow 主要分支类型
main/master: 正式发布版本,只接受 release 和 hotfix 合并
develop: 开发主分支,所有功能分支合并到这里
feature/*: 功能分支,从 develop 拉取,开发完成后合并回 develop
release/*: 发布准备分支,从 develop 拉取,修复 bug 后合并到 main 和 develop
hotfix/*: 紧急修复分支,从 main 拉取,修复后合并到 main 和 develop
Git Flow 工具安装与基本命令
git flow init # 初始化 Git Flow 仓库
git flow feature start <name> # 开始新功能
git flow feature finish <name> # 完成功能(合并到 develop)
git flow release start <version> # 开始发布
git flow release finish <version># 完成发布(合并到 main 和 develop)
git flow hotfix start <name> # 开始热修复
git flow hotfix finish <name> # 完成热修复
GitHub Actions CI/CD 基础
# GitHub Actions 核心概念
workflow: 完整自动化流程,定义在 .github/workflows/*.yml
job: 一个 workflow 包含多个 job
step: 每个 job 包含多个 step
action: 每个 step 可以运行一个 action(可复用动作)
workflow 文件基本结构
name: CI/CD Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm ci
- run: npm test
- run: npm run build</code></pre>
完整代码示例
# ===== Git Flow 与 GitHub Actions 实战 =====
1. 初始化 Git Flow 项目
git init my-app && cd my-app
git flow init -d
默认分支:main, develop(直接回车接受默认值)
2. 创建功能分支开发新功能
git flow feature start user-auth
echo "用户认证模块" > auth.py
git add . && git commit -m "feat: add user authentication module"
echo "登录接口" >> auth.py
git add . && git commit -m "feat: add login endpoint"
3. 完成功能,合并到 develop
git flow feature finish user-auth
自动切换到 develop 分支并合并 feature/user-auth
4. 创建发布分支,准备发布
git flow release start v1.0.0
在发布分支上可以修复小问题
echo "版本:1.0.0" > version.txt
git add . && git commit -m "chore: bump version to 1.0.0"
5. 完成发布
git flow release finish v1.0.0
自动:合并到 main、合并到 develop、打 tag v1.0.0
git push --all && git push --tags
===== GitHub Actions 配置 =====
6. 创建 workflow 文件
mkdir -p .github/workflows
cat > .github/workflows/ci.yml << 'EOF'
name: CI Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run linter
run: npm run lint
- name: Run tests
run: npm test
env:
CI: true
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
file: ./coverage/lcov.info
build:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Build Docker image
run: |
docker build -t my-app:${{ github.sha }} .
docker tag my-app:${{ github.sha }} my-app:latest
- name: Push to Registry
run: |
echo ${{ secrets.DOCKER_TOKEN }} | docker login -u ${{ secrets.DOCKER_USER }} --password-stdin
docker push my-app:${{ github.sha }}
docker push my-app:latest
deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_HOST }}
username: ubuntu
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
docker pull my-app:latest
docker stop my-app || true
docker rm my-app || true
docker run -d --name my-app -p 80:3000 my-app:latest
7. 在 GitHub 仓库设置 Secrets
Settings -> Secrets and variables -> Actions
添加:DOCKER_USER, DOCKER_TOKEN, SERVER_HOST, SERVER_SSH_KEY
8. 查看 Actions 运行状态
在 GitHub 仓库页面点击 Actions 标签页
可以看到每次 push/pull_request 触发的 workflow 运行记录
9. 添加 CD 作业:自动发布到 GitHub Releases
cat >> .github/workflows/ci.yml << 'EOF'
release:
needs: build
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
steps:
- uses: actions/checkout@v4
- name: Create Release
uses: softprops/action-gh-release@v1
with:
files: dist/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
EOF
echo "Git Flow 与 CI/CD 配置完成!"
常见问题
Q1: Git Flow 适合所有项目吗?
不是。Git Flow 适用于有固定发版周期、需要并行开发多个版本或特性的中大型团队。对于持续部署的小团队或个人项目,GitHub Flow(简化的分支模型,只有 main + feature 分支)更轻量。如果你的项目只需要 hotfix 和 feature 两种分支,Git Flow 可能过于复杂。
Q2: GitHub Actions 的 runners 是什么?如何选择?
Runner 是执行 workflow 的机器。GitHub 提供 ubuntu-latest、windows-latest、macos-latest 等托管 runner。对于中国开发者,可以考虑使用国内的 CI 服务或自建 runner 以提高速度。私有 runner 需要在服务器上安装 GitHub Actions Runner 应用并注册到仓库。
Q3: 如何避免 CI 构建时间过长?
几个优化建议:1)使用缓存(如 actions/cache 或 actions/setup-node 的 cache 参数);2)并行执行独立 job(当前示例中 test 和 build 可以并行,但 build 需要等 test);3)只跑受影响的测试(需要配置 monorepo 工具);4)使用 Docker 层缓存。
Q4: secrets 和 variables 有什么区别?
secrets 用于存储敏感信息(密码、密钥、token),在 workflow 日志中会被隐藏。variables 用于存储非敏感配置(如环境名称、版本号)。两者都在 Settings -> Secrets and variables -> Actions 中管理。secrets 不能在条件表达式中直接打印,但 variables 可以。
延伸阅读
- A successful Git branching model - Vincent Driessen 原始博客
- GitHub Actions 官方文档 - 完整的语法和示例
- actions/upload-artifact - 构建产物保存
- softprops/action-gh-release - 自动发布到 GitHub Releases