mcpField
G
gitlab-ci-patterns

gitlab-ci-patterns

github.com/wshobson云与 DevOps41.9k13.3k 次安装2 周前更新
代码仓库

关于 gitlab-ci-patterns

用于构建 GitLab CI/CD 流水线的技能,支持自动化测试、构建和部署,提供阶段组织、缓存、安全扫描、Terraform、Kubernetes 部署及动态子流水线模式。

主要功能

  1. 01多阶段流水线结构
  2. 02Docker 构建与推送模式
  3. 03多环境 Kubernetes 部署
  4. 04Terraform 流水线示例
  5. 05安全扫描与依赖缓存
  6. 06动态生成子流水线

适用场景

  1. 01在 GitLab 中自动化测试与部署
  2. 02跨环境部署到 Kubernetes
  3. 03运行 Terraform 验证、计划和应用阶段
  4. 04添加安全扫描并缓存构建依赖

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/wshobson/agents --skill gitlab-ci-patterns

安装后在 Claude Code 中输入/gitlab-ci-patterns确认已加载。

详细介绍

GitLab CI 模式

用于自动化测试、构建和部署的全面 GitLab CI/CD 流水线模式。

目的

创建高效的 GitLab CI 流水线,具备合理的阶段组织、缓存和部署策略。

何时使用

  • 自动化基于 GitLab 的 CI/CD
  • 实现多阶段流水线
  • 配置 GitLab Runner
  • 从 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:1.31
  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

多环境部署

将 KUBE_CA_CERT_FILE 设置为包含集群 CA 证书的 GitLab 文件变量, 并通过受保护、已屏蔽的 CI 变量提供 KUBE_TOKEN。该文件变量 包含一个路径,kubectl 使用该路径验证 API 服务器证书。同时保护 develop 和 main,以便部署作业能够读取受保护的令牌。

.deploy_template: &deploy_template
  image: bitnami/kubectl:1.31
  before_script:
    - kubectl config set-cluster k8s --server="$KUBE_URL" --certificate-authority="$KUBE_CA_CERT_FILE" --embed-certs=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:0.58.0
  script:
    - trivy image --exit-code 1 --severity HIGH,CRITICAL $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
  allow_failure: true

缓存策略

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

# Global cache
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .cache/
    - vendor/

# Separate cache per job
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

最佳实践

  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 - 用于密钥处理

评论

支持 Markdown