GitLab CI Patterns
Skill files1 file
SKILL.md273 lines
1
---
2
name: gitlab-ci-patterns
3
description: Build GitLab CI/CD pipelines with multi-stage workflows, caching, and distributed runners for scalable automation. Use when implementing GitLab CI/CD, optimizing pipeline performance, or setting up automated testing and deployment.
4
---
5
6
# GitLab CI Patterns
7
8
Comprehensive GitLab CI/CD pipeline patterns for automated testing, building, and deployment.
9
10
## Purpose
11
12
Create efficient GitLab CI pipelines with proper stage organization, caching, and deployment strategies.
13
14
## When to Use
15
16
- Automate GitLab-based CI/CD
17
- Implement multi-stage pipelines
18
- Configure GitLab Runners
19
- Deploy to Kubernetes from GitLab
20
- Implement GitOps workflows
21
22
## Basic Pipeline Structure
23
24
```yaml
25
stages:
26
- build
27
- test
28
- deploy
29
30
variables:
31
DOCKER_DRIVER: overlay2
32
DOCKER_TLS_CERTDIR: "/certs"
33
34
build:
35
stage: build
36
image: node:20
37
script:
38
- npm ci
39
- npm run build
40
artifacts:
41
paths:
42
- dist/
43
expire_in: 1 hour
44
cache:
45
key: ${CI_COMMIT_REF_SLUG}
46
paths:
47
- node_modules/
48
49
test:
50
stage: test
51
image: node:20
52
script:
53
- npm ci
54
- npm run lint
55
- npm test
56
coverage: '/Lines\s*:\s*(\d+\.\d+)%/'
57
artifacts:
58
reports:
59
coverage_report:
60
coverage_format: cobertura
61
path: coverage/cobertura-coverage.xml
62
63
deploy:
64
stage: deploy
65
image: bitnami/kubectl:1.31
66
script:
67
- kubectl apply -f k8s/
68
- kubectl rollout status deployment/my-app
69
only:
70
- main
71
environment:
72
name: production
73
url: https://app.example.com
74
```
75
76
## Docker Build and Push
77
78
```yaml
79
build-docker:
80
stage: build
81
image: docker:24
82
services:
83
- docker:24-dind
84
before_script:
85
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
86
script:
87
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
88
- docker build -t $CI_REGISTRY_IMAGE:latest .
89
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
90
- docker push $CI_REGISTRY_IMAGE:latest
91
only:
92
- main
93
- tags
94
```
95
96
## Multi-Environment Deployment
97
98
Set `KUBE_CA_CERT_FILE` as a GitLab file variable containing the cluster CA certificate,
99
and provide `KUBE_TOKEN` through a protected, masked CI variable. The file variable
100
contains a path that kubectl uses to verify the API server certificate. Protect both
101
`develop` and `main` so the deployment jobs can read the protected token.
102
103
```yaml
104
.deploy_template: &deploy_template
105
image: bitnami/kubectl:1.31
106
before_script:
107
- kubectl config set-cluster k8s --server="$KUBE_URL" --certificate-authority="$KUBE_CA_CERT_FILE" --embed-certs=true
108
- kubectl config set-credentials admin --token="$KUBE_TOKEN"
109
- kubectl config set-context default --cluster=k8s --user=admin
110
- kubectl config use-context default
111
112
deploy:staging:
113
<<: *deploy_template
114
stage: deploy
115
script:
116
- kubectl apply -f k8s/ -n staging
117
- kubectl rollout status deployment/my-app -n staging
118
environment:
119
name: staging
120
url: https://staging.example.com
121
only:
122
- develop
123
124
deploy:production:
125
<<: *deploy_template
126
stage: deploy
127
script:
128
- kubectl apply -f k8s/ -n production
129
- kubectl rollout status deployment/my-app -n production
130
environment:
131
name: production
132
url: https://app.example.com
133
when: manual
134
only:
135
- main
136
```
137
138
## Terraform Pipeline
139
140
```yaml
141
stages:
142
- validate
143
- plan
144
- apply
145
146
variables:
147
TF_ROOT: ${CI_PROJECT_DIR}/terraform
148
TF_VERSION: "1.6.0"
149
150
before_script:
151
- cd ${TF_ROOT}
152
- terraform --version
153
154
validate:
155
stage: validate
156
image: hashicorp/terraform:${TF_VERSION}
157
script:
158
- terraform init -backend=false
159
- terraform validate
160
- terraform fmt -check
161
162
plan:
163
stage: plan
164
image: hashicorp/terraform:${TF_VERSION}
165
script:
166
- terraform init
167
- terraform plan -out=tfplan
168
artifacts:
169
paths:
170
- ${TF_ROOT}/tfplan
171
expire_in: 1 day
172
173
apply:
174
stage: apply
175
image: hashicorp/terraform:${TF_VERSION}
176
script:
177
- terraform init
178
- terraform apply -auto-approve tfplan
179
dependencies:
180
- plan
181
when: manual
182
only:
183
- main
184
```
185
186
## Security Scanning
187
188
```yaml
189
include:
190
- template: Security/SAST.gitlab-ci.yml
191
- template: Security/Dependency-Scanning.gitlab-ci.yml
192
- template: Security/Container-Scanning.gitlab-ci.yml
193
194
trivy-scan:
195
stage: test
196
image: aquasec/trivy:0.58.0
197
script:
198
- trivy image --exit-code 1 --severity HIGH,CRITICAL $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
199
allow_failure: true
200
```
201
202
## Caching Strategies
203
204
```yaml
205
# Cache node_modules
206
build:
207
cache:
208
key: ${CI_COMMIT_REF_SLUG}
209
paths:
210
- node_modules/
211
policy: pull-push
212
213
# Global cache
214
cache:
215
key: ${CI_COMMIT_REF_SLUG}
216
paths:
217
- .cache/
218
- vendor/
219
220
# Separate cache per job
221
job1:
222
cache:
223
key: job1-cache
224
paths:
225
- build/
226
227
job2:
228
cache:
229
key: job2-cache
230
paths:
231
- dist/
232
```
233
234
## Dynamic Child Pipelines
235
236
```yaml
237
generate-pipeline:
238
stage: build
239
script:
240
- python generate_pipeline.py > child-pipeline.yml
241
artifacts:
242
paths:
243
- child-pipeline.yml
244
245
trigger-child:
246
stage: deploy
247
trigger:
248
include:
249
- artifact: child-pipeline.yml
250
job: generate-pipeline
251
strategy: depend
252
```
253
254
255
## Best Practices
256
257
1. **Use specific image tags** (node:20, not node:latest)
258
2. **Cache dependencies** appropriately
259
3. **Use artifacts** for build outputs
260
4. **Implement manual gates** for production
261
5. **Use environments** for deployment tracking
262
6. **Enable merge request pipelines**
263
7. **Use pipeline schedules** for recurring jobs
264
8. **Implement security scanning**
265
9. **Use CI/CD variables** for secrets
266
10. **Monitor pipeline performance**
267
268
## Related Skills
269
270
- `github-actions-templates` - For GitHub Actions
271
- `deployment-pipeline-design` - For architecture
272
- `secrets-management` - For secrets handling
273

© Seth Hobson · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…