GitLab CI 简介 #

一、什么是CI/CD? #

CI/CD是持续集成(Continuous Integration)和持续部署(Continuous Deployment/Continuous Delivery)的缩写,是现代软件开发的核心实践。

持续集成(CI) #

持续集成是一种开发实践,开发者频繁地将代码集成到主干分支。每次集成都通过自动化的构建和测试来验证,从而尽早发现集成错误。

text
代码提交 → 自动构建 → 自动测试 → 结果反馈

持续交付(CD) #

持续交付是持续集成的延伸,将集成后的代码部署到预生产环境,确保代码可以随时安全地部署到生产环境。

text
CI通过 → 自动部署到测试环境 → 人工审批 → 部署到生产环境

持续部署(CD) #

持续部署更进一步,将通过测试的代码自动部署到生产环境,无需人工干预。

text
CI通过 → 自动部署到测试环境 → 测试通过 → 自动部署到生产环境

二、GitLab CI/CD核心组件 #

1. Pipeline(流水线) #

Pipeline是CI/CD的核心概念,由多个Stage(阶段)组成,按顺序执行。

text
┌─────────────────────────────────────────────────────────┐
│                      Pipeline                            │
├─────────────────────────────────────────────────────────┤
│  ┌─────────┐   ┌─────────┐   ┌─────────┐   ┌─────────┐ │
│  │  Build  │ → │  Test   │ → │ Staging │ → │   Prod  │ │
│  │ (阶段1) │   │ (阶段2) │   │ (阶段3) │   │ (阶段4) │ │
│  └─────────┘   └─────────┘   └─────────┘   └─────────┘ │
└─────────────────────────────────────────────────────────┘

2. Stage(阶段) #

Stage是Pipeline的逻辑分组,同一Stage中的Job并行执行,不同Stage按顺序执行。

Stage 说明 典型Job
build 构建阶段 compile、package
test 测试阶段 unit-test、integration-test
deploy 部署阶段 deploy-staging、deploy-prod

3. Job(作业) #

Job是Pipeline的最小执行单元,定义了具体的执行任务。

yaml
job_name:
  script:
    - echo "Hello, GitLab CI!"
    - npm test

4. Runner(运行器) #

Runner是执行Job的代理程序,可以是Shell、Docker、Kubernetes等类型。

text
GitLab Server → 分配Job → Runner → 执行Job → 返回结果

5. .gitlab-ci.yml #

配置文件,使用YAML语法定义Pipeline的所有内容。

yaml
stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - echo "Building..."

test_job:
  stage: test
  script:
    - echo "Testing..."

三、GitLab CI工作流程 #

完整工作流程 #

text
1. 开发者推送代码到GitLab
         ↓
2. GitLab检测到.gitlab-ci.yml文件
         ↓
3. 创建新的Pipeline
         ↓
4. 按Stage顺序执行Job
         ↓
5. Runner执行具体任务
         ↓
6. 返回执行结果
         ↓
7. 通知开发者(邮件、Slack等)

触发方式 #

触发方式 说明
Push触发 推送代码时自动触发
Merge Request触发 创建或更新MR时触发
定时触发 使用Cron表达式定时执行
手动触发 在界面上手动点击执行
API触发 通过API调用触发
Webhook触发 接收外部Webhook触发

四、GitLab CI应用场景 #

1. 代码质量检查 #

yaml
lint:
  stage: test
  script:
    - npm install
    - npm run lint

2. 自动化测试 #

yaml
test:
  stage: test
  script:
    - npm install
    - npm test
  coverage: '/Coverage: \d+%/'

integration_test:
  stage: test
  script:
    - npm run test:integration

3. 自动构建镜像 #

yaml
docker_build:
  stage: build
  image: docker:latest
  services:
    - docker:dind
  script:
    - docker build -t myapp:$CI_COMMIT_SHA .
    - docker push myapp:$CI_COMMIT_SHA

4. 自动部署 #

yaml
deploy_staging:
  stage: deploy
  environment: staging
  script:
    - kubectl apply -f k8s/staging.yaml
  only:
    - develop

deploy_production:
  stage: deploy
  environment: production
  script:
    - kubectl apply -f k8s/production.yaml
  only:
    - main
  when: manual

五、GitLab CI vs 其他工具 #

特性 GitLab CI GitHub Actions Jenkins
配置文件 .gitlab-ci.yml .github/workflows/*.yml Jenkinsfile
学习曲线
内置功能 丰富 丰富 需要插件
Runner管理 简单 托管 复杂
费用 免费 有限免费 免费
集成度 高(GitLab) 高(GitHub)

六、GitLab CI优势 #

1. 内置集成 #

  • 与GitLab代码仓库无缝集成
  • 内置Container Registry
  • 内置Package Registry
  • 内置Pages静态站点

2. 配置即代码 #

  • 使用YAML格式,简单易学
  • 版本化管理,可追溯
  • 支持模板和继承

3. 强大的可视化 #

  • Pipeline图形化展示
  • Job日志实时查看
  • 测试报告可视化

4. 安全特性 #

  • Secret Variables安全存储
  • 环境保护规则
  • 审批流程

七、快速体验 #

最简单的Pipeline #

yaml
stages:
  - hello

hello_world:
  stage: hello
  script:
    - echo "Hello, GitLab CI!"

完整示例 #

yaml
stages:
  - build
  - test
  - deploy

variables:
  NODE_VERSION: "18"

build:
  stage: build
  image: node:${NODE_VERSION}
  script:
    - npm ci
    - npm run build
  artifacts:
    paths:
      - dist/

test:
  stage: test
  image: node:${NODE_VERSION}
  script:
    - npm ci
    - npm test
  coverage: '/Coverage: \d+%/'

deploy:
  stage: deploy
  image: alpine:latest
  script:
    - echo "Deploying to production..."
  only:
    - main
  when: manual

八、最佳实践 #

1. 分层设计 #

text
.gitlab-ci.yml (主配置)
    ↓ include
templates/ (模板文件)
    ↓ include
scripts/ (脚本文件)

2. 使用变量 #

yaml
variables:
  APP_NAME: "myapp"
  REGISTRY: "registry.example.com"

build:
  script:
    - docker build -t $REGISTRY/$APP_NAME:$CI_COMMIT_SHA .

3. 缓存依赖 #

yaml
cache:
  paths:
    - node_modules/

test:
  script:
    - npm ci
    - npm test

4. 使用Artifacts传递产物 #

yaml
build:
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

deploy:
  script:
    - cp -r dist/* /var/www/html/

下一步 #

现在你已经了解了GitLab CI的基本概念,接下来让我们创建 第一个流水线

最后更新:2026-03-28