-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
309 lines (285 loc) · 6.97 KB
/
.gitlab-ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
image: nixos/nix
workflow:
auto_cancel:
on_new_commit: interruptible
default:
interruptible: true
variables:
IMAGE_TAG: v0.1.6
BANTERBUS_CI_IMAGE: $CI_REGISTRY_IMAGE/ci:$IMAGE_TAG
POSTGRES_USER: banterbus
POSTGRES_PASSWORD: banterbus
POSTGRES_HOST_AUTH_METHOD: trust
stages:
- pre
- deps
- test
- build
- deploy
- release
.task:
stage: test
image: $BANTERBUS_CI_IMAGE
variables:
GOPATH: $CI_PROJECT_DIR/.go
cache:
paths:
- ${GOPATH}/pkg/mod
policy: pull
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_PIPELINE_SOURCE == "schedule"
when: never
before_script:
# INFO: Used to create directory, needed for tests and linter
- mkdir -p /tmp
.test:
extends:
- .task
variables:
GOTESTSUM_JUNITFILE: "report.xml"
XDG_DATA_HOME: "/tmp/"
GOTEST_EXTRA_ARGS: '-args -test.gocoverdir=$CI_PROJECT_DIR/coverage'
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main"
- if: $CI_PIPELINE_SOURCE == "schedule"
when: never
before_script:
- mkdir -p $CI_PROJECT_DIR/coverage /tmp $CI_PROJECT_DIR/e2e/screenshots
artifacts:
paths:
- coverage
reports:
junit: report.xml
.docker:
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
IMMUTABLE: "true"
services:
- docker:dind
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
when: never
script:
- echo "experimental-features = nix-command flakes" > /etc/nix/nix.conf
- nix-env -iA nixpkgs.docker nixpkgs.go-task
- task docker:publish
.deploy:
stage: deploy
image:
name: ghcr.io/fluxcd/flux-cli:v2.0.0
entrypoint: ['']
timeout: 5m
script:
- IMAGE=$(cat image_name.txt)
- kubectl config use-context hmajid2301/k3s-config:ms01
- flux reconcile image repository banterbus
- |
echo "Waiting for deployment to update to image: $IMAGE"
until kubectl get deployment banterbus -n $NAMESPACE -o jsonpath="{.spec.template.spec.containers[0].image}" | grep -q "$IMAGE"; do
echo "Deployment not updated yet. Waiting..."
sleep 10
done
echo "Deployment updated to image: $IMAGE"
- kubectl wait --for=condition=available --timeout=600s deployment/banterbus -n $NAMESPACE
publish:docker:ci:
stage: pre
variables:
IMAGE: $BANTERBUS_CI_IMAGE
FLAKE_TARGET: container-ci
LOCAL_IMAGE: banterbus-dev
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
changes:
- "containers/ci.nix"
- "flake.nix"
- "flake.lock"
extends:
- .docker
before_script:
- |
if ! git diff --name-only HEAD~1 | grep -q ".nix"; then
echo "No changes in *.nix in the current commit. Skipping job."
exit 0
fi
download:dependency:
extends:
- .task
stage: deps
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
changes:
- go.mod
- go.sum
script:
- go mod download
cache:
policy: pull-push
lint:
extends:
- .task
script:
- task lint
format:
extends:
- .task
script:
- task format
- git diff --exit-code
generate:
extends:
- .task
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
script:
- task generate
- git diff --exit-code
tests:unit:
extends:
- .test
script:
- task tests:unit -- ${GOTEST_EXTRA_ARGS}
tests:integration:
extends:
- .test
services:
- redis:7.4.1
- name: postgres:16.6
alias: postgres
command:
- "postgres"
- "-c"
- "max_connections=1000"
- name: ghcr.io/navikt/mock-oauth2-server:2.1.10
alias: oauth2
variables:
BANTERBUS_REDIS_ADDRESS: redis:6379
BANTERBUS_DB_URI: postgresql://banterbus:banterbus@postgres:5432
BANTERBUS_JWKS_URL: http://oauth2:8080/default/jwks
script:
- task tests:integration -- ${GOTEST_EXTRA_ARGS}
test:e2e:
extends:
- .test
services:
- redis:7.4.1
- postgres:16.6
- name: ghcr.io/navikt/mock-oauth2-server:2.1.10
alias: oauth2
variables:
BANTERBUS_REDIS_ADDRESS: redis:6379
BANTERBUS_DB_URI: postgresql://banterbus:banterbus@postgres:5432
BANTERBUS_JWKS_URL: http://oauth2:8080/default/jwks
script:
- task tests:e2e -- ${GOTEST_EXTRA_ARGS}
artifacts:
when: always
reports:
junit: report.xml
paths:
- coverage
- tests/e2e/videos/*
- tests/e2e/*.png
combine:coverage:
stage: build
extends:
- .task
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main"
- if: $CI_PIPELINE_SOURCE == "schedule"
when: never
script:
- task coverage
coverage: /total:\s+\(statements\)\s+\d+.\d+%/
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
publish:dev:docker:
stage: build
needs: []
variables:
FLAKE_TARGET: container
LOCAL_IMAGE: banterbus
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
extends:
- .docker
before_script:
- export IMAGE="$CI_REGISTRY_IMAGE:dev-$CI_COMMIT_SHA-$(date +%s)"
- echo $IMAGE > image_name.txt
artifacts:
paths:
- image_name.txt
check_deploy:dev:
extends: .deploy
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
variables:
NAMESPACE: dev
dependencies:
- publish:dev:docker
#INFO: Because Nix docker builds are immutable, we can just rebuild the image here for production
# If we wanted to be more efficient we could work out way to retag a dev image. But this is good enough for now.
publish:prod:docker:
stage: build
variables:
FLAKE_TARGET: container
LOCAL_IMAGE: banterbus
services:
- docker:27-dind
extends:
- .docker
rules:
- if: $CI_COMMIT_TAG || $CI_COMMIT_BRANCH == "main"
- if: $CI_PIPELINE_SOURCE == "schedule"
when: never
before_script:
- export IMAGE="$CI_REGISTRY_IMAGE:prod-$CI_COMMIT_SHA-$(date +%s)"
- echo $IMAGE > image_name.txt
artifacts:
paths:
- image_name.txt
check_deploy:prod:
extends: .deploy
rules:
- if: $CI_COMMIT_TAG || $CI_COMMIT_BRANCH == "main"
- if: $CI_PIPELINE_SOURCE == "schedule"
when: never
variables:
NAMESPACE: prod
dependencies:
- publish:prod:docker
release:
stage: release
rules:
- if: $CI_COMMIT_TAG
extends:
- .task
variables:
GIT_DEPTH: 0
script:
- task release
smoke:tests:e2e:
extends:
- .test
variables:
BANTERBUS_PLAYWRIGHT_URL: https://banterbus.games
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
when: always
script:
- task tests:e2e -- ${GOTEST_EXTRA_ARGS}
after_script:
- |
if [ "$CI_JOB_STATUS" != "success" ]; then
curl -X POST \
-H "Authorization: Bearer $NOTIFY_TOKEN" "https://notify.homelab.haseebmajid.dev/message" \
-F "title=Job Failed" \
-F "message=The smoke:tests:e2e job has failed in the pipeline. Job URL: $CI_JOB_URL"
fi