$npx -y skills add BagelHole/DevOps-Security-Agent-Skills --skill gitlab-ciConfigure GitLab CI/CD pipelines and runners for automated building, testing, and deployment. Create .gitlab-ci.yml configurations, manage runners, and implement DevOps workflows. Use when working with GitLab repositories or self-hosted GitLab instances.
| 1 | # GitLab CI/CD |
| 2 | |
| 3 | Automate your software delivery pipeline with GitLab's integrated CI/CD system. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Setting up CI/CD pipelines in GitLab |
| 9 | - Configuring GitLab runners (shared or self-hosted) |
| 10 | - Creating multi-stage deployment pipelines |
| 11 | - Implementing GitLab Auto DevOps |
| 12 | - Managing CI/CD variables and secrets |
| 13 | |
| 14 | ## Prerequisites |
| 15 | |
| 16 | - GitLab repository (gitlab.com or self-hosted) |
| 17 | - Basic understanding of YAML |
| 18 | - For self-hosted runners: Linux server or Kubernetes cluster |
| 19 | |
| 20 | ## Pipeline Configuration |
| 21 | |
| 22 | Create `.gitlab-ci.yml` in repository root: |
| 23 | |
| 24 | ```yaml |
| 25 | stages: |
| 26 | - build |
| 27 | - test |
| 28 | - deploy |
| 29 | |
| 30 | variables: |
| 31 | NODE_VERSION: "20" |
| 32 | |
| 33 | build: |
| 34 | stage: build |
| 35 | image: node:${NODE_VERSION} |
| 36 | script: |
| 37 | - npm ci |
| 38 | - npm run build |
| 39 | artifacts: |
| 40 | paths: |
| 41 | - dist/ |
| 42 | expire_in: 1 hour |
| 43 | |
| 44 | test: |
| 45 | stage: test |
| 46 | image: node:${NODE_VERSION} |
| 47 | script: |
| 48 | - npm ci |
| 49 | - npm test |
| 50 | coverage: '/Coverage: \d+\.\d+%/' |
| 51 | |
| 52 | deploy: |
| 53 | stage: deploy |
| 54 | script: |
| 55 | - ./deploy.sh |
| 56 | environment: |
| 57 | name: production |
| 58 | url: https://example.com |
| 59 | only: |
| 60 | - main |
| 61 | ``` |
| 62 | |
| 63 | ## Job Configuration |
| 64 | |
| 65 | ### Rules-Based Execution |
| 66 | |
| 67 | ```yaml |
| 68 | deploy: |
| 69 | script: ./deploy.sh |
| 70 | rules: |
| 71 | - if: $CI_COMMIT_BRANCH == "main" |
| 72 | when: manual |
| 73 | - if: $CI_PIPELINE_SOURCE == "merge_request_event" |
| 74 | when: never |
| 75 | - when: on_success |
| 76 | ``` |
| 77 | |
| 78 | ### Parallel Jobs |
| 79 | |
| 80 | ```yaml |
| 81 | test: |
| 82 | stage: test |
| 83 | parallel: 3 |
| 84 | script: |
| 85 | - npm test -- --shard=$CI_NODE_INDEX/$CI_NODE_TOTAL |
| 86 | ``` |
| 87 | |
| 88 | ### Matrix Builds |
| 89 | |
| 90 | ```yaml |
| 91 | test: |
| 92 | stage: test |
| 93 | parallel: |
| 94 | matrix: |
| 95 | - NODE_VERSION: ["18", "20", "22"] |
| 96 | OS: ["alpine", "slim"] |
| 97 | image: node:${NODE_VERSION}-${OS} |
| 98 | script: |
| 99 | - npm test |
| 100 | ``` |
| 101 | |
| 102 | ## Caching |
| 103 | |
| 104 | ```yaml |
| 105 | cache: |
| 106 | key: |
| 107 | files: |
| 108 | - package-lock.json |
| 109 | paths: |
| 110 | - node_modules/ |
| 111 | policy: pull-push |
| 112 | |
| 113 | build: |
| 114 | cache: |
| 115 | key: build-cache |
| 116 | paths: |
| 117 | - .cache/ |
| 118 | policy: pull |
| 119 | ``` |
| 120 | |
| 121 | ## Artifacts |
| 122 | |
| 123 | ```yaml |
| 124 | build: |
| 125 | artifacts: |
| 126 | paths: |
| 127 | - dist/ |
| 128 | - coverage/ |
| 129 | reports: |
| 130 | junit: junit.xml |
| 131 | coverage_report: |
| 132 | coverage_format: cobertura |
| 133 | path: coverage/cobertura.xml |
| 134 | expire_in: 1 week |
| 135 | when: always |
| 136 | ``` |
| 137 | |
| 138 | ## Environments and Deployments |
| 139 | |
| 140 | ```yaml |
| 141 | deploy_staging: |
| 142 | stage: deploy |
| 143 | script: |
| 144 | - deploy --env staging |
| 145 | environment: |
| 146 | name: staging |
| 147 | url: https://staging.example.com |
| 148 | on_stop: stop_staging |
| 149 | |
| 150 | stop_staging: |
| 151 | stage: deploy |
| 152 | script: |
| 153 | - undeploy --env staging |
| 154 | environment: |
| 155 | name: staging |
| 156 | action: stop |
| 157 | when: manual |
| 158 | ``` |
| 159 | |
| 160 | ## Docker Builds |
| 161 | |
| 162 | ```yaml |
| 163 | build_image: |
| 164 | stage: build |
| 165 | image: docker:24 |
| 166 | services: |
| 167 | - docker:24-dind |
| 168 | variables: |
| 169 | DOCKER_TLS_CERTDIR: "/certs" |
| 170 | script: |
| 171 | - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY |
| 172 | - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA . |
| 173 | - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA |
| 174 | ``` |
| 175 | |
| 176 | ## GitLab Runners |
| 177 | |
| 178 | ### Install Runner |
| 179 | |
| 180 | ```bash |
| 181 | # Download and install |
| 182 | curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash |
| 183 | sudo apt install gitlab-runner |
| 184 | |
| 185 | # Register runner |
| 186 | sudo gitlab-runner register \ |
| 187 | --url https://gitlab.com/ \ |
| 188 | --registration-token TOKEN \ |
| 189 | --executor docker \ |
| 190 | --docker-image alpine:latest |
| 191 | ``` |
| 192 | |
| 193 | ### Runner Configuration |
| 194 | |
| 195 | ```toml |
| 196 | # /etc/gitlab-runner/config.toml |
| 197 | [[runners]] |
| 198 | name = "docker-runner" |
| 199 | url = "https://gitlab.com/" |
| 200 | token = "TOKEN" |
| 201 | executor = "docker" |
| 202 | [runners.docker] |
| 203 | image = "alpine:latest" |
| 204 | privileged = true |
| 205 | volumes = ["/cache", "/var/run/docker.sock:/var/run/docker.sock"] |
| 206 | ``` |
| 207 | |
| 208 | ### Runner Tags |
| 209 | |
| 210 | ```yaml |
| 211 | build: |
| 212 | tags: |
| 213 | - docker |
| 214 | - linux |
| 215 | script: |
| 216 | - make build |
| 217 | ``` |
| 218 | |
| 219 | ## CI/CD Variables |
| 220 | |
| 221 | ### Protected Variables |
| 222 | |
| 223 | Define in Settings > CI/CD > Variables: |
| 224 | - `AWS_ACCESS_KEY_ID` (protected, masked) |
| 225 | - `AWS_SECRET_ACCESS_KEY` (protected, masked) |
| 226 | |
| 227 | ### Using Variables |
| 228 | |
| 229 | ```yaml |
| 230 | deploy: |
| 231 | script: |
| 232 | - aws s3 sync dist/ s3://$S3_BUCKET |
| 233 | variables: |
| 234 | AWS_DEFAULT_REGION: us-east-1 |
| 235 | ``` |
| 236 | |
| 237 | ## Include and Extend |
| 238 | |
| 239 | ### Include Templates |
| 240 | |
| 241 | ```yaml |
| 242 | include: |
| 243 | - template: Security/SAST.gitlab-ci.yml |
| 244 | - project: 'group/shared-ci' |
| 245 | file: '/templates/deploy.yml' |
| 246 | - local: '/ci/jobs.yml' |
| 247 | ``` |
| 248 | |
| 249 | ### Extend Jobs |
| 250 | |
| 251 | ```yaml |
| 252 | .base_job: |
| 253 | image: node:20 |
| 254 | before_script: |
| 255 | - npm ci |
| 256 | |
| 257 | build: |
| 258 | extends: .base_job |
| 259 | script: |
| 260 | - npm run build |
| 261 | |
| 262 | test: |
| 263 | extends: .base_job |
| 264 | script: |
| 265 | - npm test |
| 266 | ``` |
| 267 | |
| 268 | ## Multi-Project Pipelines |
| 269 | |
| 270 | ```yaml |
| 271 | trigger_downstream: |
| 272 | stage: deploy |
| 273 | trigger: |
| 274 | project: group/downstream-project |
| 275 | branch: main |
| 276 | strategy: depend |
| 277 | ``` |
| 278 | |
| 279 | ## Common Issues |
| 280 | |
| 281 | ### Issue: Pipeline Stuck |
| 282 | **Problem**: Jobs stay pending |
| 283 | **Solution**: Check runner availability and tags ma |