09. 从 0 到 1:Go + React 项目的 GitLab CI/CD 实战部署
「GitLab」系列已发布: 00. GitLab 不只是代码托管:一套 DevOps 平台的 6 大核心能力 | 01. GitLab 怎么装最省心?Omnibus/Docker/K8s 三种姿势 | 02. 装完 GitLab 先配啥?管理员视角的初始化设置清单 | 03. GitLab 角色搞不清?Member/Maintainer/Owner 权限全解 | 04. 从零建好第一个 GitLab 项目:权限、变量、CI 配置入门 | 05. GitLab MR 代码审查:Code Review 最佳实践 | 06. CI/CD 到底是什么?GitLab 流水线架构从概念到实战 | 07. Runner 装不好?GitLab Runner 单机/分布式/高可用全攻略 | 08. .gitlab-ci.yml 怎么写?从 Hello World 到生产配置 | 09. 从 0 到 1:Go + React 项目的 GitLab CI/CD 实战部署(本文)
理论说完就上手。以 Go 后端 + React 前端为例,从 .gitlab-ci.yml 一路写到自动部署,跑通一条真实可用的 CI/CD 流水线。
前置条件
已完成 Runner 安装与注册(第 07 篇),理解 .gitlab-ci.yml 基础语法(第 08 篇)。下面以一个典型的 Go 后端 + React 前端项目为例,搭一套完整 CI/CD Pipeline。
一、项目背景
项目结构:
app/
├── backend/ # Go 后端服务
│ ├── cmd/
│ ├── internal/
│ ├── go.mod
│ └── Dockerfile
├── frontend/ # React 前端
│ ├── src/
│ ├── package.json
│ └── Dockerfile
└── .gitlab-ci.yml # Pipeline 配置
二、Pipeline 配置
2.1 完整配置示例
stages:
- build
- test
- security
- deploy
variables:
DOCKER_REGISTRY: registry.example.com
APP_NAME: myapp
# 定义通用配置
.default-build:
image: golang:1.21-alpine
before_script:
- cd backend
- go mod download
# 后端构建
backend-build:
extends: .default-build
stage: build
script:
- go build -o bin/server ./cmd/server
artifacts:
paths:
- backend/bin/
expire_in: 1 hour
# 前端构建
frontend-build:
image: node:18-alpine
before_script:
- cd frontend
- npm ci
script:
- npm run build
artifacts:
paths:
- frontend/dist/
expire_in: 1 hour
# 后端测试
backend-test:
extends: .default-build
stage: test
script:
- go test -v -coverprofile=coverage.out ./...
artifacts:
reports:
coverage_report:
coverage_format: go
path: backend/coverage.out
# 前端测试
frontend-test:
image: node:18-alpine
stage: test
before_script:
- cd frontend
- npm ci
script:
- npm test -- --coverage
artifacts:
reports:
junit: frontend/jest.junit.xml
coverage_report:
coverage_format: jacoco
path: frontend/coverage/lcov.info
# 安全扫描
security-sast:
stage: security
image: alpine:latest
script:
- echo "Running SAST scan..."
allow_failure: true
security-secrets:
stage: security
image: alpine:latest
script:
- echo "Scanning for secrets..."
allow_failure: true
# 构建 Docker 镜像
docker-build:
stage: build
image: docker:24
services:
- docker:dind
script:
- docker login -u $DOCKER_USER -p $DOCKER_PASS $DOCKER_REGISTRY
- docker build -t $DOCKER_REGISTRY/$APP_NAME:$CI_COMMIT_SHORT_SHA backend/
- docker push $DOCKER_REGISTRY/$APP_NAME:$CI_COMMIT_SHORT_SHA
only:
- main
- tags
# 部署到 staging
deploy-staging:
stage: deploy
image: alpine:latest
script:
- apk add --no-cache kubectl
- kubectl set image deployment/$APP_NAME $APP_NAME=$DOCKER_REGISTRY/$APP_NAME:$CI_COMMIT_SHORT_SHA -n staging
- kubectl rollout status deployment/$APP_NAME -n staging --timeout=300s
environment:
name: staging
url: https://staging.example.com
only:
- main
# 部署到 production
deploy-production:
stage: deploy
image: alpine:latest
script:
- apk add --no-cache kubectl
- kubectl set image deployment/$APP_NAME $APP_NAME=$DOCKER_REGISTRY/$APP_NAME:$CI_COMMIT_SHORT_SHA -n production
- kubectl rollout status deployment/$APP_NAME -n production --timeout=300s
environment:
name: production
url: https://example.com
only:
- tags
when: manual
三、Pipeline 可视化
flowchart TD A[代码推送] --> B{触发 Pipeline} B --> C[build 阶段] B --> D[test 阶段] B --> E[security 阶段] B --> F[deploy 阶段] C --> C1[后端构建] C --> C2[前端构建] C --> C3[Docker 镜像] D --> D1[后端测试] D --> D2[前端测试] E --> E1[SAST 扫描] E --> E2[密钥扫描] F --> F1[部署 Staging] F --> F2[部署 Production] F1 --> G{审批通过?} G -->|是| F2 G -->|否| H[跳过 Production]
四、优化技巧
4.1 并行执行
test:
parallel: 3
script:
- echo "Running test $CI_NODE_INDEX of $CI_NODE_TOTAL"
4.2 Job 依赖
test:
needs: [] # 不等待其他 Job
script:
- npm test
deploy:
needs: [test] # 等待 test 完成
script:
- ./deploy.sh
4.3 条件执行
deploy:
script:
- ./deploy.sh
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual
- if: $CI_COMMIT_TAG
when: auto
- when: never
五、文章引用
📌 本文首发于微信公众号 【运维知识点滴】——专注运维实战干货,微信搜索“运维知识点滴”或扫下方二维码关注,更新不迷路。
| 微信公众号【运维知识点滴】 | 微信小程序【A5单据合并&大试卷拆分转A4打印】 |
|---|---|
![]() |
![]() |
| 运维实战干货 · 关注追更 | Word 转 PDF · 试卷/发票打印神器 · 免费 |
💡 小程序【A5单据合并&大试卷拆分转A4打印】是干嘛的? 一个 Word 转 PDF 的打印神器,专治“文档版式和打印机对不上”:
- A3 试卷打不出来? 学校发的电子版试卷多是 A3,家用打印机只吃 A4——上传 Word 一键转换:可缩到一页 A4(页数不变),也可对半拆成两张 A4(1:1 原大、字更清晰),两种模式自选;
- 版式不乱:真实渲染引擎原样转换、绝不重排内容,转出的 PDF 和原 Word 排版一致,公式/表格/图片不跑版;
- 两张 A5 单据拼一张 A4:出租车票、火车票等报销凭证拼版后,一次打印、整齐粘贴;
- 隐私省心:无需注册,转换结果留存 5 天、最多下载 5 次即自动删除,不留任何历史记录;
- 微信扫码即用,免费、免安装,支持 doc / docx(≤50MB)。

