Skill v1.0.1
currentAutomated scan100/100+4 new
version: "1.0.1" name: gitlab-ci-patterns description: "Comprehensive GitLab CI/CD pipeline patterns for automated testing, building, and deployment." risk: critical source: community date_added: "2026-02-27"
GitLab CI Patterns
Comprehensive GitLab CI/CD pipeline patterns for automated testing, building, and deployment.
Do not use this skill when
- The task is unrelated to gitlab ci patterns
- You need a different domain or tool outside this scope
Instructions
- Clarify goals, constraints, and required inputs.
- Apply relevant best practices and validate outcomes.
- Provide actionable steps and verification.
Purpose
Create efficient GitLab CI pipelines with proper stage organization, caching, and deployment strategies.
Use this skill when
- Automate GitLab-based CI/CD
- Implement multi-stage pipelines
- Configure GitLab Runners
- Deploy to Kubernetes from GitLab
- Implement GitOps workflows
Basic Pipeline Structure
stages:- build- test- deployvariables:DOCKER_DRIVER: overlay2DOCKER_TLS_CERTDIR: "/certs"build:stage: buildimage: node:20script:- npm ci- npm run buildartifacts:paths:- dist/expire_in: 1 hourcache:key: ${CI_COMMIT_REF_SLUG}paths:- node_modules/test:stage: testimage: node:20script:- npm ci- npm run lint- npm testcoverage: '/Lines\s*:\s*(\d+\.\d+)%/'artifacts:reports:coverage_report:coverage_format: coberturapath: coverage/cobertura-coverage.xmldeploy:stage: deployimage: bitnami/kubectl:latestscript:- kubectl apply -f k8s/- kubectl rollout status deployment/my-apponly:- mainenvironment:name: productionurl: https://app.example.com
Docker Build and Push
build-docker:stage: buildimage: docker:24services:- docker:24-dindbefore_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:latestonly:- main- tags
Multi-Environment Deployment
.deploy_template: &deploy_templateimage: bitnami/kubectl:latestbefore_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 defaultdeploy:staging:<<: *deploy_templatestage: deployscript:- kubectl apply -f k8s/ -n staging- kubectl rollout status deployment/my-app -n stagingenvironment:name: stagingurl: https://staging.example.comonly:- developdeploy:production:<<: *deploy_templatestage: deployscript:- kubectl apply -f k8s/ -n production- kubectl rollout status deployment/my-app -n productionenvironment:name: productionurl: https://app.example.comwhen: manualonly:- main
Terraform Pipeline
stages:- validate- plan- applyvariables:TF_ROOT: ${CI_PROJECT_DIR}/terraformTF_VERSION: "1.6.0"before_script:- cd ${TF_ROOT}- terraform --versionvalidate:stage: validateimage: hashicorp/terraform:${TF_VERSION}script:- terraform init -backend=false- terraform validate- terraform fmt -checkplan:stage: planimage: hashicorp/terraform:${TF_VERSION}script:- terraform init- terraform plan -out=tfplanartifacts:paths:- ${TF_ROOT}/tfplanexpire_in: 1 dayapply:stage: applyimage: hashicorp/terraform:${TF_VERSION}script:- terraform init- terraform apply -auto-approve tfplandependencies:- planwhen: manualonly:- main
Security Scanning
include:- template: Security/SAST.gitlab-ci.yml- template: Security/Dependency-Scanning.gitlab-ci.yml- template: Security/Container-Scanning.gitlab-ci.ymltrivy-scan:stage: testimage: aquasec/trivy:latestscript:- trivy image --exit-code 1 --severity HIGH,CRITICAL $CI_REGISTRY_IMAGE:$CI_COMMIT_SHAallow_failure: false
Caching Strategies
# Cache node_modulesbuild:cache:key: ${CI_COMMIT_REF_SLUG}paths:- node_modules/policy: pull-push# Global cachecache:key: ${CI_COMMIT_REF_SLUG}paths:- .cache/- vendor/# Separate cache per jobjob1:cache:key: job1-cachepaths:- build/job2:cache:key: job2-cachepaths:- dist/
Dynamic Child Pipelines
generate-pipeline:stage: buildscript:- python generate_pipeline.py > child-pipeline.ymlartifacts:paths:- child-pipeline.ymltrigger-child:stage: deploytrigger:include:- artifact: child-pipeline.ymljob: generate-pipelinestrategy: depend
Template scope
The examples are inline scaffolds. Use verified image digests and installed command versions, a protected scoped kubeconfig and protected production environments. KUBE_CA_FILE must point to a verified CI file variable. Do not deploy from untrusted merge requests or automatically grant new credentials. No external template is required by the inline examples.
Best Practices
- Use specific image tags (node:20, not node:latest)
- Cache dependencies appropriately
- Use artifacts for build outputs
- Implement manual gates for production
- Use environments for deployment tracking
- Enable merge request pipelines
- Use pipeline schedules for recurring jobs
- Implement security scanning
- Use CI/CD variables for secrets
- Monitor pipeline performance
Related Skills
github-actions-templates- For GitHub Actionsdeployment-pipeline-design- For architecturesecrets-management- For secrets handling
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.