forked from SocialGouv/work-in-france-backoffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
260 lines (231 loc) · 6.11 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
---
include:
- "/k8s/api/.deploy-wif-bo-api.yml"
- "/k8s/postgres/.deploy-wif-bo-postgres.yml"
- "/k8s/memcached/.deploy-wif-bo-memcached.yml"
- "/k8s/kinto/.deploy-wif-bo-kinto.yml"
variables:
DOCKER_DRIVER: overlay2
DOCKER_HOST: tcp://localhost:2375
DOCKER_VERSION: "18.06"
IMAGE_INFRA_BASE_NAME: "infra/images-docker"
DEV_ENVIRONMENT_NAME: "dev.factory"
PROD_ENVIRONMENT_NAME: "incubateur"
API_PORT: 4000
KINTO_PORT: 8888
POSTGRES_PORT: 5432
MEMCACHED_PORT: 11212
stages:
- "Code Quality"
- "Registration"
- "Deploy"
- "Send Url to GitHub"
- "Delete K8S objects"
#
.quality_stage: &quality_stage
stage: "Code Quality"
image: node:10-alpine
before_script:
- yarn --frozen-lockfile
#
Quality tests:
<<: *quality_stage
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
script:
- yarn build
- yarn lint
- yarn test
###########################################
### PREPARE ###
###########################################
#
.get_github_id_stage: &get_github_id_stage
stage: "Deploy"
image:
name: $CI_REGISTRY/$IMAGE_INFRA_BASE_NAME/docker-kube:latest
entrypoint: [""]
before_script:
- envsubst < k8s/scripts/get-deploy-id.sh > k8s/scripts/get-github-deploy-id.sh
script:
- sh k8s/scripts/get-github-deploy-id.sh
artifacts:
paths:
- github_deploy_id
#
Get Github Id (dev):
<<: *get_github_id_stage
environment:
name: $DEV_ENVIRONMENT_NAME
except:
- master
Get Github Id (prod):
<<: *get_github_id_stage
environment:
name: $PROD_ENVIRONMENT_NAME
only:
- master
###########################################
### REGISTER DOCKER IMAGES ###
###########################################
#
.register_stage: ®ister_stage
stage: "Registration"
image: docker:$DOCKER_VERSION
services:
- docker:$DOCKER_VERSION-dind
before_script:
- echo "$CI_JOB_TOKEN" | docker login $CI_REGISTRY -u gitlab-ci-token --password-stdin
- docker pull $IMAGE_NAME:$CI_COMMIT_BEFORE_SHA || true
script:
- echo "Build $IMAGE_NAME:$CI_COMMIT_SHA from $IMAGE_NAME:$CI_COMMIT_BEFORE_SHA"
- docker build --cache-from $IMAGE_NAME:$CI_COMMIT_BEFORE_SHA -t $IMAGE_NAME:$CI_COMMIT_SHA -f $DOCKERFILE_PATH $CONTEXT
- docker push $IMAGE_NAME
#
Register api image:
<<: *register_stage
variables:
CONTEXT: .
DOCKERFILE_PATH: packages/api/Dockerfile
IMAGE_NAME: $CI_REGISTRY_IMAGE/api
Register init kinto image:
<<: *register_stage
variables:
CONTEXT: packages/kinto
DOCKERFILE_PATH: packages/kinto/Dockerfile
IMAGE_NAME: $CI_REGISTRY_IMAGE/kinto
###########################################
### DEPLOY TO K8S ###
###########################################
#
.deploy_stage: &deploy_stage
stage: "Deploy"
dependencies: []
variables: &deploy_stage_variables
WIF_BO_REGISTRY: $CI_REGISTRY_IMAGE
IMAGE_TAG: $CI_COMMIT_SHA
#
# Dev Environment
Deploy @wif-bo/api (dev):
<<: *deploy_stage
extends: .deploy-wif-bo-api-k8s-dev
variables:
<<: *deploy_stage_variables
PORT: $API_PORT
environment:
name: $DEV_ENVIRONMENT_NAME
except:
- master
Deploy @wif-bo/postgres (dev):
<<: *deploy_stage
extends: .deploy-wif-bo-postgres-k8s-dev
variables:
<<: *deploy_stage_variables
PORT: $POSTGRES_PORT
environment:
name: $DEV_ENVIRONMENT_NAME
except:
- master
Deploy @wif-bo/memcached (dev):
<<: *deploy_stage
extends: .deploy-wif-bo-memcached-k8s-dev
variables:
<<: *deploy_stage_variables
PORT: $MEMCACHED_PORT
environment:
name: $DEV_ENVIRONMENT_NAME
except:
- master
Deploy @wif-bo/kinto (dev):
<<: *deploy_stage
extends: .deploy-wif-bo-kinto-k8s-dev
variables:
<<: *deploy_stage_variables
PORT: $KINTO_PORT
CACHE_PORT: $MEMCACHED_PORT
environment:
name: $DEV_ENVIRONMENT_NAME
except:
- master
# Prod Environment
Deploy @wif-bo/api (prod):
<<: *deploy_stage
extends: .deploy-wif-bo-api-k8s-prod
variables:
<<: *deploy_stage_variables
PORT: $API_PORT
environment:
name: $PROD_ENVIRONMENT_NAME
only:
- master
Deploy @wif-bo/memcached (prod):
<<: *deploy_stage
extends: .deploy-wif-bo-memcached-k8s-prod
variables:
<<: *deploy_stage_variables
PORT: $MEMCACHED_PORT
environment:
name: $PROD_ENVIRONMENT_NAME
only:
- master
Deploy @wif-bo/kinto (pro):
<<: *deploy_stage
extends: .deploy-wif-bo-kinto-k8s-prod
variables:
<<: *deploy_stage_variables
PORT: $KINTO_PORT
CACHE_PORT: $MEMCACHED_PORT
environment:
name: $PROD_ENVIRONMENT_NAME
only:
- master
###########################################
### SEND URL TO GITHUB ###
###########################################
#
.send_url_to_github_stage: &send_url_to_github_stage
stage: "Send Url to GitHub"
image:
name: $CI_REGISTRY/$IMAGE_INFRA_BASE_NAME/docker-kube:latest
entrypoint: [""]
script:
- export DEPLOY_ID=$(cat github_deploy_id)
- envsubst < k8s/scripts/send-url.sh > k8s/scripts/send-url-to-github.sh
- sh k8s/scripts/send-url-to-github.sh $CI_COMMIT_REF_NAME
#
Send deployment url to Github (dev):
<<: *send_url_to_github_stage
before_script:
- HASH_BRANCH_NAME=$(printf "$CI_COMMIT_REF_NAME" | sha1sum | cut -c1-5)
- export HASH_BRANCH_NAME=$HASH_BRANCH_NAME
- export URL=http://$HASH_BRANCH_NAME-wif-bo.${CI_ENVIRONMENT_NAME}.social.gouv.fr/
environment:
name: $DEV_ENVIRONMENT_NAME
except:
- master
Send deployment url to Github (prod):
<<: *send_url_to_github_stage
before_script:
- export URL=http://wif-bo.${CI_ENVIRONMENT_NAME}.social.gouv.fr/
environment:
name: $PROD_ENVIRONMENT_NAME
only:
- master
###########################################
### DELETE USELESS K8S OBJECTS ###
###########################################
Delete useless k8s objects:
stage: "Delete K8S objects"
image:
name: $CI_REGISTRY/$IMAGE_INFRA_BASE_NAME/docker-kube:latest
entrypoint: [""]
before_script:
- /apps/create-kubeconfig.sh
script:
- python3 k8s/scripts/delete-k8s-objects.py
environment:
name: $DEV_ENVIRONMENT_NAME
only:
- master