GitLab CI 配置
GitLab CI 是 GitLab 提供的内置 CI/CD 工具,用户可以通过配置项目根目录的 .gitlab-ci.yml 文件来定义自动化的构建、测试、部署等流程。以下是详细的配置说明、文件路径和具体操作步骤。
1. GitLab CI 工作原理
- 
核心概念:
- Pipeline:完整的 CI/CD 工作流,由多个阶段(Stages)和作业(Jobs)组成。
 - Job:Pipeline 中的具体任务,如编译、运行测试、部署。
 - Stage:一组按顺序执行的作业。一个 Pipeline 包含多个 Stage。
 - Runner:负责执行作业的工具,可分为共享 Runner 和私有 Runner。
 
 - 
执行流程:
- 触发:推送代码或创建 Merge Request 时触发 CI/CD。
 - 读取配置 :GitLab 读取 
.gitlab-ci.yml文件。 - 执行:Runner 按照配置依次执行作业。
 
 
2. 配置步骤
2.1 配置 .gitlab-ci.yml 文件
- 
创建配置文件 :
在项目根目录下创建
.gitlab-ci.yml文件:bashtouch .gitlab-ci.yml - 
文件结构:
- 
一个
.gitlab-ci.yml文件的基本结构包含以下部分:yamlstages: # 定义阶段顺序 - build - test - deploy build_job: # 定义一个构建作业 stage: build script: - echo "Building the application..." test_job: # 定义一个测试作业 stage: test script: - echo "Running tests..." 
 - 
 
2.2 提交代码并触发 Pipeline
- 
推送文件到 GitLab 仓库:
bashgit add .gitlab-ci.yml git commit -m "Add CI configuration" git push origin <branch-name> - 
查看 Pipeline:
- 打开 GitLab 项目页面。
 - 点击 CI/CD > Pipelines 查看运行状态。
 
 
3. 配置文件详解
3.1 基本配置参数
- 
stages:定义流水线的阶段。yamlstages: - build - test - deploy - 
script:作业的执行脚本。yamlbuild_job: stage: build script: - echo "Compiling code..." - 
only和except:限制作业运行的分支。yamltest_job: stage: test script: - echo "Running tests..." only: - main # 仅在 main 分支运行 except: - feature/* # 不在 feature 分支运行 - 
artifacts:定义作业生成的文件以供后续作业使用。yamlbuild_job: stage: build script: - mkdir build - echo "Build completed!" > build/status.txt artifacts: paths: - build/ 
3.2 复杂作业配置
1. 并行作业
同时运行多个作业,适用于多版本测试:
            
            
              yaml
              
              
            
          
          test_job:
  stage: test
  parallel:
    matrix:
      - NODE_VERSION: ["14", "16"]
  script:
    - echo "Testing with Node.js version $NODE_VERSION"
        2. 手动触发和延迟执行
控制作业的执行时机:
            
            
              yaml
              
              
            
          
          deploy_job:
  stage: deploy
  script:
    - echo "Deploying to production..."
  when: manual       # 手动触发
        3. 环境配置
定义环境变量和部署环境:
            
            
              yaml
              
              
            
          
          deploy_job:
  stage: deploy
  script:
    - echo "Deploying to $CI_ENVIRONMENT_NAME..."
  environment:
    name: staging    # 部署到 staging 环境
  variables:
    APP_VERSION: "1.0.0"
        4. GitLab CI 配置示例
4.1 基本 CI 配置
一个简单的构建、测试和部署流水线:
            
            
              yaml
              
              
            
          
          stages:
  - build
  - test
  - deploy
build_job:
  stage: build
  script:
    - echo "Building the application..."
    - mkdir build
    - echo "Build complete" > build/status.txt
test_job:
  stage: test
  script:
    - echo "Running tests..."
    - pytest
deploy_job:
  stage: deploy
  script:
    - echo "Deploying to production..."
  only:
    - main
        4.2 Node.js 项目 CI 配置
使用 Node.js 测试和部署:
            
            
              yaml
              
              
            
          
          stages:
  - install
  - test
  - deploy
install_dependencies:
  stage: install
  script:
    - npm install
unit_tests:
  stage: test
  script:
    - npm test
  artifacts:
    paths:
      - coverage/
deploy_to_production:
  stage: deploy
  script:
    - echo "Deploying to production..."
  environment:
    name: production
  only:
    - main
        4.3 容器化部署配置
使用 Docker 构建镜像并部署到 Kubernetes:
            
            
              yaml
              
              
            
          
          stages:
  - build
  - deploy
docker_build:
  stage: build
  image: docker:20.10.16
  services:
    - docker:dind
  script:
    - docker build -t myapp:$CI_COMMIT_SHA .
    - docker push myrepo/myapp:$CI_COMMIT_SHA
deploy_to_kubernetes:
  stage: deploy
  script:
    - kubectl apply -f deployment.yaml
  environment:
    name: production
  only:
    - main
        5. 高阶配置和动态控制
5.1 动态控制规则
根据提交分支或文件变化动态触发作业:
            
            
              yaml
              
              
            
          
          test_job:
  stage: test
  script:
    - echo "Running tests..."
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: always
    - if: $CI_COMMIT_BRANCH =~ /feature\/.*/
      when: manual
        5.2 缓存与加速
缓存依赖或构建结果,提高执行速度:
            
            
              yaml
              
              
            
          
          cache:
  paths:
    - node_modules/
        6. 触发 Pipeline 的完整操作
1. 创建分支并推送代码
            
            
              bash
              
              
            
          
          git checkout -b feature/new-feature
# 添加代码和 CI 配置文件
git add .
git commit -m "Add CI/CD configuration"
git push origin feature/new-feature
        2. 查看 Pipeline
- 在 GitLab 项目页面,点击 CI/CD > Pipelines。
 - 查看是否触发了 Pipeline。
 
3. 手动触发作业
- 在 Pipeline 页面,点击 Manual 按钮手动执行特定作业。
 
总结
- 文件路径 :
.gitlab-ci.yml文件必须位于项目根目录。 - 基础配置 :
- 定义 
stages和jobs。 - 设置 
script执行具体任务。 
 - 定义 
 - 高阶功能 :
- 动态触发规则。
 - 缓存和依赖管理。
 - 多环境部署和容器化支持。
 
 
通过实践逐步熟悉 GitLab CI 的功能,你可以构建出适合团队需求的高效自动化流水线!