mcpField
G
gitlab-ci-patterns

gitlab-ci-patterns

github.com/ormus-solutions云与 DevOps12377 次安装4 周前更新
代码仓库

关于 gitlab-ci-patterns

gitlab-ci-patterns 是用于设计 GitLab CI/CD 流水线的参考技能,面向构建自动化流程的开发者。内容涵盖阶段组织、缓存、安全扫描与部署,包括 Kubernetes 和 GitOps 工作流。

主要功能

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

适用场景

  1. 01在 GitLab 自动化测试、构建和部署
  2. 02通过 GitLab 部署到 Kubernetes
  3. 03安排 Terraform 验证与应用阶段
  4. 04添加安全扫描和依赖缓存

安装

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

$npx skills add https://github.com/ormus-solutions/libreuiux-claude-code --skill gitlab-ci-patterns

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

详细介绍

GitLab CI 模式

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

用途

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

适用场景

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

缓存策略

# 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

参考文件

  • 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 - 用于机密管理

评论

支持 Markdown