-
Notifications
You must be signed in to change notification settings - Fork 3
/
.gitlab-ci.yml
151 lines (135 loc) · 3.86 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
stages:
- check
- build
- test
- release
- deploy
variables:
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
DOCKER_TLS_VERIFY: 1
DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
IMAGE_RELEASE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-$CI_PIPELINE_ID
IMAGE_TEST_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-$CI_PIPELINE_ID-test
CACHE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-cache
#CACHE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
APP_IMG_VER: $CI_COMMIT_SHORT_SHA
workflow:
rules:
# Rule for direct commit to production branch, MR is recommended
- if: $CI_COMMIT_TAG
variables:
WORKFLOW_PRODUCTION: "true"
IMAGE_RELEASE_TAG: $CI_REGISTRY_IMAGE/production:$CI_COMMIT_TAG
IMAGE_TEST_TAG: $CI_REGISTRY_IMAGE/production:$CI_COMMIT_SHORT_SHA-$CI_COMMIT_TAG-test
IMAGE_CACHE_TAG: $CI_REGISTRY_IMAGE/production:cache
CURVE_IMG_VER: $CI_COMMIT_TAG
- if: $CI_COMMIT_BRANCH == "main"
variables:
WORKFLOW_STAGING: "true"
- when: always
variables:
WORKFLOW_FEATURES: "true"
## Common CI/CD targets used with extends keyword
.build_image_base:
stage: build
image: docker:20.10.21
services:
- docker:20.10.21-dind
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-$CI_PIPELINE_ID
CACHE_TAG: $CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}-cache
before_script:
- set -x
- env
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN mcr.gitlab.montel.fi
script:
# defaults: --build-arg REQUIREMENTS=requirements_prod.txt
#- 'eval docker pull $CACHE_TAG || true'
- >
docker build
--network host
--build-arg APP_IMG_VER=$APP_IMG_VER $BUILD_EXTRA_ARGS
--cache-from $CACHE_TAG
--tag $IMAGE_TAG
--tag $CACHE_TAG .
- docker push $IMAGE_TAG
- docker push $CACHE_TAG
.test_image_base:
stage: test
image: docker:20.10.21
services:
- docker:20.10.21-dind
before_script:
- set -x
- env
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN mcr.gitlab.montel.fi
- apk add docker-compose
script:
- 'eval export TEST_IMG=$IMAGE_TEST_TAG'
- 'eval docker pull $TEST_IMG'
- docker-compose -f docker-compose.yml -f docker-compose.test.override.yml run --rm --name unittest django
#####
## WORKFLOW_FEATURES: Build image
###
build-notest:
extends: .build_image_base
variables:
IMAGE_TAG: $IMAGE_RELEASE_TAG
rules:
- if: $WORKFLOW_FEATURES
####
## WORKFLOW_STAGING: Build test image,Run unittest, build release image,
##
build-test-image:
extends: .build_image_base
variables:
IMAGE_TAG: $IMAGE_TEST_TAG
BUILD_EXTRA_ARGS: '--target=development'
rules:
- if: $WORKFLOW_STAGING
run-unittest:
extends: .test_image_base
rules:
- if: $WORKFLOW_STAGING
build-staging-image:
extends: .build_image_base
stage: release
variables:
IMAGE_TAG: $IMAGE_RELEASE_TAG
BUILD_EXTRA_ARGS: '--target=production'
rules:
- if: $WORKFLOW_STAGING
####
## WORKFLOW_PRODUCTION: Check semver, Build test image, Run unittest, build release image
##
accept-valid-tag:
# test passes if $CI_COMMIT_TAG is release-<semver>
image: alpine/semver
stage: check
script:
- set -ex
- env
- semver $(echo $CI_COMMIT_TAG | sed -e "s/release-//")
rules:
- if: $WORKFLOW_PRODUCTION
build-production-test-image:
extends: .build_image_base
variables:
IMAGE_TAG: $IMAGE_TEST_TAG
CACHE_TAG: $IMAGE_CACHE_TAG
BUILD_EXTRA_ARGS: '--target=development'
rules:
- if: $WORKFLOW_PRODUCTION
run-production-unittest:
extends: .test_image_base
rules:
- if: $WORKFLOW_PRODUCTION
build-production-release-image:
extends: .build_image_base
stage: release
variables:
IMAGE_TAG: $IMAGE_RELEASE_TAG
BUILD_EXTRA_ARGS: '--target=production'
rules:
- if: $WORKFLOW_PRODUCTION