关于 gitlab-ci-patterns
为自动化测试、构建和部署的团队提供 GitLab CI/CD 管道模式的技能,包含分阶段管道、Docker、多环境部署、Terraform、安全扫描、缓存和动态子管道示例。
主要功能
- 01多阶段管道脚手架
- 02Docker 构建与推送示例
- 03多环境部署模式
- 04Terraform 管道示例
- 05安全扫描与缓存
- 06动态子管道
适用场景
- 01组织 GitLab CI/CD 阶段
- 02构建并发布 Docker 镜像
- 03从 GitLab 部署到 Kubernetes
- 04添加安全扫描与依赖缓存
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill gitlab-ci-patterns安装后在 Claude Code 中输入/gitlab-ci-patterns确认已加载。
详细介绍
GitLab CI 模式
用于自动化测试、构建和部署的全面 GitLab CI/CD 流水线模式。
不要在以下情况下使用此技能
- 任务与 GitLab CI 模式无关
- 你需要使用此范围之外的其他领域知识或工具
说明
- 明确目标、约束和所需输入。
- 应用相关最佳实践并验证结果。
- 提供可执行的步骤和验证方法。
用途
创建高效的 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: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:
- printf '%s' "$CI_REGISTRY_PASSWORD" | docker login --username "$CI_REGISTRY_USER" --password-stdin "$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" --certificate-authority="$KUBE_CA_FILE"
- 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: false
缓存策略
# 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
模板范围
这些示例是内联脚手架。请使用经过验证的镜像摘要和已安装的命令版本、受保护且作用域受限的 kubeconfig,以及受保护的生产环境。KUBE_CA_FILE 必须指向经过验证的 CI 文件变量。不要从不受信任的合并请求进行部署,也不要自动授予新凭据。内联示例无需外部模板。
最佳实践
- 使用具体的镜像标签(例如 node:20,而非 node:latest)
- 适当缓存依赖项
- 使用构件保存构建输出
- 为生产环境设置人工审批关卡
- 使用环境跟踪部署情况
- 启用合并请求流水线
- 使用流水线计划执行定期任务
- 实施安全扫描
- 使用 CI/CD 变量保存机密
- 监控流水线性能
相关技能
github-actions-templates- 用于 GitHub Actionsdeployment-pipeline-design- 用于架构设计secrets-management- 用于机密管理
限制
- 仅当任务明确符合上述范围时,才使用此技能。
- 不要将输出视为针对特定环境进行验证、测试或专家审核的替代品。
- 如果缺少所需输入、权限、安全边界或成功标准,请停止并请求澄清。
评论
支持 Markdown