GitLabCI/CD管道模式Skill gitlab-ci-patterns

这个技能用于自动化 GitLab CI/CD 管道,通过实现多阶段工作流、缓存策略和分布式运行程序,优化管道性能,实现高效的自动化测试、构建和部署。关键词包括 GitLab CI/CD、管道模式、自动化测试、缓存、部署、DevOps、多环境、安全扫描、Terraform、Docker。

CI/CD 0 次安装 0 次浏览 更新于 3/16/2026

name: gitlab-ci-patterns description: 构建具有多阶段工作流、缓存和分布式运行程序的 GitLab CI/CD 管道,以实现可扩展自动化。适用于实施 GitLab CI/CD、优化管道性能或设置自动化测试和部署时使用。

GitLab CI 模式

全面的 GitLab CI/CD 管道模式,用于自动化测试、构建和部署。

目的

创建高效的 GitLab CI 管道,具有适当的阶段组织、缓存和部署策略。

何时使用

  • 自动化基于 GitLab 的 CI/CD
  • 实施多阶段管道
  • 配置 GitLab 运行程序
  • 从 GitLab 部署到 Kubernetes
  • 实施 GitOps 工作流

基本管道结构

stages:
  - build
  - test
  - deploy

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: "/certs"

build:
  stage: build
  image: node:20
  script:
    - npm ci
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 hour
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/

test:
  stage: test
  image: node:20
  script:
    - npm ci
    - npm run lint
    - npm test
  coverage: '/Lines\s*:\s*(\d+\.\d+)%/'
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml

deploy:
  stage: deploy
  image: bitnami/kubectl:latest
  script:
    - kubectl apply -f k8s/
    - kubectl rollout status deployment/my-app
  only:
    - main
  environment:
    name: production
    url: https://app.example.com

Docker 构建和推送

build-docker:
  stage: build
  image: docker:24
  services:
    - docker:24-dind
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
    - docker build -t $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    - docker push $CI_REGISTRY_IMAGE:latest
  only:
    - main
    - tags

多环境部署

.deploy_template: &deploy_template
  image: bitnami/kubectl:latest
  before_script:
    - kubectl config set-cluster k8s --server="$KUBE_URL" --insecure-skip-tls-verify=true
    - kubectl config set-credentials admin --token="$KUBE_TOKEN"
    - kubectl config set-context default --cluster=k8s --user=admin
    - kubectl config use-context default

deploy:staging:
  <<: *deploy_template
  stage: deploy
  script:
    - kubectl apply -f k8s/ -n staging
    - kubectl rollout status deployment/my-app -n staging
  environment:
    name: staging
    url: https://staging.example.com
  only:
    - develop

deploy:production:
  <<: *deploy_template
  stage: deploy
  script:
    - kubectl apply -f k8s/ -n production
    - kubectl rollout status deployment/my-app -n production
  environment:
    name: production
    url: https://app.example.com
  when: manual
  only:
    - main

Terraform 管道

stages:
  - validate
  - plan
  - apply

variables:
  TF_ROOT: ${CI_PROJECT_DIR}/terraform
  TF_VERSION: "1.6.0"

before_script:
  - cd ${TF_ROOT}
  - terraform --version

validate:
  stage: validate
  image: hashicorp/terraform:${TF_VERSION}
  script:
    - terraform init -backend=false
    - terraform validate
    - terraform fmt -check

plan:
  stage: plan
  image: hashicorp/terraform:${TF_VERSION}
  script:
    - terraform init
    - terraform plan -out=tfplan
  artifacts:
    paths:
      - ${TF_ROOT}/tfplan
    expire_in: 1 day

apply:
  stage: apply
  image: hashicorp/terraform:${TF_VERSION}
  script:
    - terraform init
    - terraform apply -auto-approve tfplan
  dependencies:
    - plan
  when: manual
  only:
    - main

安全扫描

include:
  - template: Security/SAST.gitlab-ci.yml
  - template: Security/Dependency-Scanning.gitlab-ci.yml
  - template: Security/Container-Scanning.gitlab-ci.yml

trivy-scan:
  stage: test
  image: aquasec/trivy:latest
  script:
    - trivy image --exit-code 1 --severity HIGH,CRITICAL $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
  allow_failure: true

缓存策略

# 缓存 node_modules
build:
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
    policy: pull-push

# 全局缓存
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .cache/
    - vendor/

# 每个作业的独立缓存
job1:
  cache:
    key: job1-cache
    paths:
      - build/

job2:
  cache:
    key: job2-cache
    paths:
      - dist/

动态子管道

generate-pipeline:
  stage: build
  script:
    - python generate_pipeline.py > child-pipeline.yml
  artifacts:
    paths:
      - child-pipeline.yml

trigger-child:
  stage: deploy
  trigger:
    include:
      - artifact: child-pipeline.yml
        job: generate-pipeline
    strategy: depend

参考文件

  • assets/gitlab-ci.yml.template - 完整的管道模板
  • references/pipeline-stages.md - 阶段组织模式

最佳实践

  1. 使用特定的镜像标签(例如 node:20,而不是 node:latest)
  2. 适当缓存依赖项
  3. 使用制品存储构建输出
  4. 为生产环境实现手动门控
  5. 使用环境进行部署跟踪
  6. 启用合并请求管道
  7. 使用管道调度进行重复作业
  8. 实施安全扫描
  9. 使用 CI/CD 变量管理密钥
  10. 监控管道性能

相关技能

  • github-actions-templates - 用于 GitHub Actions
  • deployment-pipeline-design - 用于架构设计
  • secrets-management - 用于密钥处理