-
Notifications
You must be signed in to change notification settings - Fork 7
/
.gitlab-ci.yml
383 lines (329 loc) · 9.83 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
image: python:3.11
stages:
- setup
- lint
- test
- integration-test
- bump
- build
- release
- deploy
# Pipeline rules
workflow:
rules:
# Prevent running a push pipeline for a release commit that's not a tag
- if: $CI_COMMIT_MESSAGE =~ /^chore\(release\):.*/ && $CI_COMMIT_TAG == null
when: never
# If a push to branch with open merge request then ignore
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: never
# If source is push and no open merge request then run
- if: $CI_COMMIT_BRANCH
# Run tag pipelines
- if: $CI_COMMIT_TAG
# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
VERSION_FILE: ".version"
CI_GHCR_IMAGE: ${CI_GHCR_REGISTRY}/${CI_GHCR_REPO}
# Default cache values
default:
cache: &global_cache
key: $CI_COMMIT_REF_SLUG
paths:
- .cache/pypoetry
- .cache/pip
- .venv/
- ./task
policy: pull-push
# Anchor to use pull only cache
.use_cached_venv: &use_cached_venv
before_script:
- source .venv/bin/activate
cache:
<<: *global_cache
policy: pull
.only_tag: &only_tag
rules:
- if: $CI_COMMIT_TAG
when: on_success
- when: never
.no_tag: &no_tag
rules:
- if: $CI_COMMIT_TAG
when: never
- when: on_success
.only_protected: &only_protected
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_TAG == null
when: on_success
- when: never
.k8s-job:
image: dtzar/helm-kubectl:3.12
before_script:
- kubectl config use-context $AGENT_CONFIG
- kubectl version
- helm version
.deploy-common:
extends: .k8s-job
stage: deploy
# variables:
# DEPLOY_HOSTS
# NAMESPACE
# RELEASE_NAME
# REPLICAS
# CHART_PATH
# IMAGE_TAG (optional)
needs:
- get-version
script:
- TLS_SECRET="$RELEASE_NAME-tls"
- HOSTS_ARR=(${DEPLOY_HOSTS//,/ })
# If Tag is set use that, if not use CI_COMMIT_TAG or CI_COMMIT_SHORT_SHA if the CI tag isn't available
- IMAGE_TAG=${IMAGE_TAG:-${CI_COMMIT_TAG:-$CI_COMMIT_SHORT_SHA}}
- HELM_SET_ARGS="--set image.tag=${IMAGE_TAG} --set image.repository=$CI_GHCR_IMAGE --set ingress.tlsSecretName=$TLS_SECRET --set replicas=$REPLICAS"
- |
for i in "${!HOSTS_ARR[@]}"; do
HELM_SET_ARGS="$HELM_SET_ARGS --set ingress.hosts[$i]=${HOSTS_ARR[$i]}"
done
- helm upgrade --install --namespace $NAMESPACE $HELM_SET_ARGS $RELEASE_NAME $CHART_PATH
- kubectl wait --namespace $NAMESPACE --for=condition=available --timeout=180s deployment/$RELEASE_NAME
# Anchor for docker jobs
.docker-job:
image: docker:latest
services:
- docker:dind
# Setup job to recreate venv when dependencies change
# or there is no existing cache already
build-env:
stage: .pre
script:
# Setup virtual environment
- python -m venv .venv/
- pip install -U pip
- pip install poetry
- poetry install --all-extras
# Install go-task
- sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b .
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: never
# If project dependencies were changed then run
- changes:
- pyproject.toml
- poetry.lock
when: always
# If .venv exists then skip
- exists:
- .venv/
when: never
# Default to manual, but continue
# pipeline if not run
- when: manual
allow_failure: true
# Main Jobs
# ------------
lint:
<<: *use_cached_venv
<<: *no_tag
stage: lint
script:
- ./task lint
lint-commit:
<<: *use_cached_venv
<<: *no_tag
stage: lint
script:
- |
# Get the commit message of the last commit
commit_message=$(git log -1 --pretty=format:%s)
# If the commit message starts with "Merge branch", it is a merge commit, so skip the linting.
if [[ $commit_message == 'Merge branch'* ]]; then
echo "Merge commit detected, skipping lint."
exit 0
fi
pip install gitlint
# Ensure the commit message is valid
# We should always pass this as long as the pusher has the pre-commit hooks installed
# but just as a sanity check we'll run it here
git log -1 --pretty=%B | gitlint
# We need to always run the unit-tests job so the build-docs-docker job
# can access the artifact since downloading via API is broken:
# https://gitlab.com/gitlab-org/gitlab/-/issues/352644
unit-tests:
<<: *use_cached_venv
stage: test
script:
- ./task unit-tests
coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
junit: report.xml
paths:
- coverage-report/
bump-version:
<<: *use_cached_venv
<<: *only_protected
variables:
REPO_URL: "https://release-token:${RELEASE_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
stage: bump
before_script:
- pip install -U commitizen
# Set git info
- git config --global user.email "$GIT_COMMIT_EMAIL"
- git config --global user.name "$GIT_COMMIT_USER"
# NOTE: Must be set to CLONE strategy in gitlab CI otherwise there will be
# excess commits and tags from a dirty cache
- git checkout $CI_COMMIT_BRANCH
- git pull $REPO_URL $CI_COMMIT_BRANCH
# Explicitly checkout branch since gitlab CI detaches
script:
# Bump the version
- export CZ_BUMP_OUTPUT=$(cz bump --yes 2>&1)
- export CZ_BUMP_EXIT_CODE=$?
- echo $CZ_BUMP_OUTPUT
- echo $CZ_BUMP_EXIT_CODE
# Push new tag to trigger release workflow if bumped
- |
if [[ $CZ_BUMP_OUTPUT == *"[NO_COMMITS_TO_BUMP]"* ]]; then
echo "Version unchanged."
else
echo "Version changed."
git push $REPO_URL $CI_COMMIT_BRANCH --tags
fi
get-version:
<<: *use_cached_venv
stage: setup
script:
- export PROJECT_VERSION=$(poetry version --short)
- 'echo "Project: ${PROJECT_VERSION}"'
- echo "PROJECT_VERSION=$PROJECT_VERSION" >> $VERSION_FILE
artifacts:
reports:
dotenv: $VERSION_FILE
build-wheel:
<<: *use_cached_venv
<<: *only_tag
image: mateusoliveira43/poetry:1.5.1-python3.11.4
stage: build
script:
- poetry build --format wheel
artifacts:
name: dist
paths:
- dist/
build-docs:
<<: *use_cached_venv
<<: *only_protected
stage: build
script:
- ./task build-docs
artifacts:
paths:
- site/
expire_in: 1 week
build-docker:
<<: *use_cached_venv
extends: .docker-job
stage: build
rules:
- if: $CI_COMMIT_TAG
when: on_success
- when: manual
allow_failure: true
needs: ["get-version"]
script:
- export COMMIT_TAG=${CI_COMMIT_TAG:-$CI_COMMIT_SHORT_SHA}
- echo $CI_GHCR_PAT | docker login $CI_GHCR_REGISTRY -u $CI_GHCR_USER --password-stdin
- ./task build-docker-base IMAGE_NAME=$CI_GHCR_IMAGE IMAGE_TAG=$COMMIT_TAG PYTHON_VERSION=3.11 DOCKER_BUILD_ARGS="--pull"
- if [ -n "$CI_COMMIT_TAG" ]; then
docker tag "$CI_GHCR_IMAGE:$COMMIT_TAG" "$CI_GHCR_IMAGE:latest";
fi
- docker push "$CI_GHCR_IMAGE" --all-tags
build-docker-full:
<<: *use_cached_venv
extends: .docker-job
stage: build
rules:
- if: $CI_COMMIT_TAG
when: on_success
- when: manual
allow_failure: true
needs: ["get-version", "build-docker"]
script:
- export COMMIT_TAG=${CI_COMMIT_TAG:-$CI_COMMIT_SHORT_SHA}
- echo $CI_GHCR_PAT | docker login $CI_GHCR_REGISTRY -u $CI_GHCR_USER --password-stdin
- ./task build-docker-full IMAGE_NAME=$CI_GHCR_IMAGE IMAGE_TAG=$COMMIT_TAG PYTHON_VERSION=3.11 DOCKER_BUILD_ARGS="--pull"
- if [ "$CI_COMMIT_BRANCH" = "$CI_DEFAULT_BRANCH" ]; then
docker tag "$CI_GHCR_IMAGE:$COMMIT_TAG" "$CI_GHCR_IMAGE:latest";
fi
- docker push "$CI_GHCR_IMAGE" --all-tags
build-docker-gpu:
<<: *use_cached_venv
extends: .docker-job
stage: build
rules:
- if: $CI_COMMIT_TAG
when: on_success
- when: manual
allow_failure: true
needs: ["get-version", "build-docker", "build-docker-full"]
script:
- export COMMIT_TAG=${CI_COMMIT_TAG:-$CI_COMMIT_SHORT_SHA}
- echo $CI_GHCR_PAT | docker login $CI_GHCR_REGISTRY -u $CI_GHCR_USER --password-stdin
- ./task build-docker-gpu IMAGE_NAME=$CI_GHCR_IMAGE IMAGE_TAG=$COMMIT_TAG PYTHON_VERSION=3.11 DOCKER_BUILD_ARGS="--pull"
- if [ "$CI_COMMIT_BRANCH" = "$CI_DEFAULT_BRANCH" ]; then
docker tag "$CI_GHCR_IMAGE:$COMMIT_TAG" "$CI_GHCR_IMAGE:latest";
fi
- docker push "$CI_GHCR_IMAGE" --all-tags
build-docs-docker:
<<: *use_cached_venv
<<: *only_protected
extends: .docker-job
stage: build
needs: ["build-docs", "unit-tests"]
script:
- echo $CI_GHCR_PAT | docker login $CI_GHCR_REGISTRY -u $CI_GHCR_USER --password-stdin
- ./task build-docker-docs IMAGE_NAME=$CI_GHCR_IMAGE IMAGE_TAG=$CI_COMMIT_SHORT_SHA PYTHON_VERSION=3.11 DOCKER_BUILD_ARGS="--pull"
- docker push "$CI_GHCR_IMAGE" --all-tags
make-release:
<<: *only_tag
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
script:
- echo "Building release $CI_COMMIT_TAG"
release:
name: Release $CI_COMMIT_TAG
tag_name: $CI_COMMIT_TAG
description: $CHANGELOG
publish-wheel:
<<: *use_cached_venv
<<: *only_tag
stage: release
needs: ["build-wheel"]
image: mateusoliveira43/poetry:1.5.1-python3.11.4
variables:
PACKAGE_NAME: flowdapt
script: |
# Publish the built wheel to the registry
poetry config pypi-token.pypi $PYPI_TOKEN
poetry publish
deploy-docs:
<<: *only_protected
stage: deploy
extends: .deploy-common
environment:
name: documentation
url: https://docs.flowdapt.ai
needs: ["build-docs-docker", "get-version"]
variables:
DEPLOY_HOSTS: docs.flowdapt.ai
RELEASE_NAME: flowdapt-docs
NAMESPACE: website
REPLICAS: 1
CHART_PATH: charts/docs
IMAGE_TAG: docs-latest