$npx -y skills add tranhieutt/software_development_department --skill gitlab-ci-patternsProvides GitLab CI/CD pipeline patterns including stages, jobs, artifacts, caching, and environment deployments. Use when working with .gitlab-ci.yml or when the user mentions GitLab CI or GitLab pipelines.
| 1 | # GitLab CI Patterns |
| 2 | |
| 3 | Comprehensive GitLab CI/CD pipeline patterns for automated testing, building, and deployment. |
| 4 | |
| 5 | ## Do not use this skill when |
| 6 | |
| 7 | - The task is unrelated to gitlab ci patterns |
| 8 | - You need a different domain or tool outside this scope |
| 9 | |
| 10 | ## Instructions |
| 11 | |
| 12 | - Clarify goals, constraints, and required inputs. |
| 13 | - Apply relevant best practices and validate outcomes. |
| 14 | - Provide actionable steps and verification. |
| 15 | |
| 16 | |
| 17 | ## Purpose |
| 18 | |
| 19 | Create efficient GitLab CI pipelines with proper stage organization, caching, and deployment strategies. |
| 20 | |
| 21 | ## Use this skill when |
| 22 | |
| 23 | - Automate GitLab-based CI/CD |
| 24 | - Implement multi-stage pipelines |
| 25 | - Configure GitLab Runners |
| 26 | - Deploy to Kubernetes from GitLab |
| 27 | - Implement GitOps workflows |
| 28 | |
| 29 | ## Basic Pipeline Structure |
| 30 | |
| 31 | ```yaml |
| 32 | stages: |
| 33 | - build |
| 34 | - test |
| 35 | - deploy |
| 36 | |
| 37 | variables: |
| 38 | DOCKER_DRIVER: overlay2 |
| 39 | DOCKER_TLS_CERTDIR: "/certs" |
| 40 | |
| 41 | build: |
| 42 | stage: build |
| 43 | image: node:20 |
| 44 | script: |
| 45 | - npm ci |
| 46 | - npm run build |
| 47 | artifacts: |
| 48 | paths: |
| 49 | - dist/ |
| 50 | expire_in: 1 hour |
| 51 | cache: |
| 52 | key: ${CI_COMMIT_REF_SLUG} |
| 53 | paths: |
| 54 | - node_modules/ |
| 55 | |
| 56 | test: |
| 57 | stage: test |
| 58 | image: node:20 |
| 59 | script: |
| 60 | - npm ci |
| 61 | - npm run lint |
| 62 | - npm test |
| 63 | coverage: '/Lines\s*:\s*(\d+\.\d+)%/' |
| 64 | artifacts: |
| 65 | reports: |
| 66 | coverage_report: |
| 67 | coverage_format: cobertura |
| 68 | path: coverage/cobertura-coverage.xml |
| 69 | |
| 70 | deploy: |
| 71 | stage: deploy |
| 72 | image: bitnami/kubectl:latest |
| 73 | script: |
| 74 | - kubectl apply -f k8s/ |
| 75 | - kubectl rollout status deployment/my-app |
| 76 | only: |
| 77 | - main |
| 78 | environment: |
| 79 | name: production |
| 80 | url: https://app.example.com |
| 81 | ``` |
| 82 | |
| 83 | ## Docker Build and Push |
| 84 | |
| 85 | ```yaml |
| 86 | build-docker: |
| 87 | stage: build |
| 88 | image: docker:24 |
| 89 | services: |
| 90 | - docker:24-dind |
| 91 | before_script: |
| 92 | - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY |
| 93 | script: |
| 94 | - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA . |
| 95 | - docker build -t $CI_REGISTRY_IMAGE:latest . |
| 96 | - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA |
| 97 | - docker push $CI_REGISTRY_IMAGE:latest |
| 98 | only: |
| 99 | - main |
| 100 | - tags |
| 101 | ``` |
| 102 | |
| 103 | ## Multi-Environment Deployment |
| 104 | |
| 105 | ```yaml |
| 106 | .deploy_template: &deploy_template |
| 107 | image: bitnami/kubectl:latest |
| 108 | before_script: |
| 109 | - kubectl config set-cluster k8s --server="$KUBE_URL" --insecure-skip-tls-verify=true |
| 110 | - kubectl config set-credentials admin --token="$KUBE_TOKEN" |
| 111 | - kubectl config set-context default --cluster=k8s --user=admin |
| 112 | - kubectl config use-context default |
| 113 | |
| 114 | deploy:staging: |
| 115 | <<: *deploy_template |
| 116 | stage: deploy |
| 117 | script: |
| 118 | - kubectl apply -f k8s/ -n staging |
| 119 | - kubectl rollout status deployment/my-app -n staging |
| 120 | environment: |
| 121 | name: staging |
| 122 | url: https://staging.example.com |
| 123 | only: |
| 124 | - develop |
| 125 | |
| 126 | deploy:production: |
| 127 | <<: *deploy_template |
| 128 | stage: deploy |
| 129 | script: |
| 130 | - kubectl apply -f k8s/ -n production |
| 131 | - kubectl rollout status deployment/my-app -n production |
| 132 | environment: |
| 133 | name: production |
| 134 | url: https://app.example.com |
| 135 | when: manual |
| 136 | only: |
| 137 | - main |
| 138 | ``` |
| 139 | |
| 140 | ## Terraform Pipeline |
| 141 | |
| 142 | ```yaml |
| 143 | stages: |
| 144 | - validate |
| 145 | - plan |
| 146 | - apply |
| 147 | |
| 148 | variables: |
| 149 | TF_ROOT: ${CI_PROJECT_DIR}/terraform |
| 150 | TF_VERSION: "1.6.0" |
| 151 | |
| 152 | before_script: |
| 153 | - cd ${TF_ROOT} |
| 154 | - terraform --version |
| 155 | |
| 156 | validate: |
| 157 | stage: validate |
| 158 | image: hashicorp/terraform:${TF_VERSION} |
| 159 | script: |
| 160 | - terraform init -backend=false |
| 161 | - terraform validate |
| 162 | - terraform fmt -check |
| 163 | |
| 164 | plan: |
| 165 | stage: plan |
| 166 | image: hashicorp/terraform:${TF_VERSION} |
| 167 | script: |
| 168 | - terraform init |
| 169 | - terraform plan -out=tfplan |
| 170 | artifacts: |
| 171 | paths: |
| 172 | - ${TF_ROOT}/tfplan |
| 173 | expire_in: 1 day |
| 174 | |
| 175 | apply: |
| 176 | stage: apply |
| 177 | image: hashicorp/terraform:${TF_VERSION} |
| 178 | script: |
| 179 | - terraform init |
| 180 | - terraform apply -auto-approve tfplan |
| 181 | dependencies: |
| 182 | - plan |
| 183 | when: manual |
| 184 | only: |
| 185 | - main |
| 186 | ``` |
| 187 | |
| 188 | ## Security Scanning |
| 189 | |
| 190 | ```yaml |
| 191 | include: |
| 192 | - template: Security/SAST.gitlab-ci.yml |
| 193 | - template: Security/Dependency-Scanning.gitlab-ci.yml |
| 194 | - template: Security/Container-Scanning.gitlab-ci.yml |
| 195 | |
| 196 | trivy-scan: |
| 197 | stage: test |
| 198 | image: aquasec/trivy:latest |
| 199 | script: |
| 200 | - trivy image --exit-code 1 --severity HIGH,CRITICAL $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA |
| 201 | allow_failure: true |
| 202 | ``` |
| 203 | |
| 204 | ## Caching Strategies |
| 205 | |
| 206 | ```yaml |
| 207 | # Cache node_modules |
| 208 | build: |
| 209 | cache: |
| 210 | key: ${CI_COMMIT_REF_SLUG} |
| 211 | paths: |
| 212 | - node_modules/ |
| 213 | policy: pull-push |
| 214 | |
| 215 | # Global cache |
| 216 | cache: |
| 217 | key: ${CI_COMMIT_REF_SLUG} |
| 218 | paths: |
| 219 | - .cache/ |
| 220 | - vendor/ |
| 221 | |
| 222 | # Separate cache per job |
| 223 | job1: |
| 224 | cache: |
| 225 | key: job1-cache |
| 226 | paths: |
| 227 | - build/ |
| 228 | |
| 229 | job2: |
| 230 | cache: |
| 231 | key: job2-cache |
| 232 | p |