-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.gitlab-ci.yml
262 lines (238 loc) · 8.56 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
include:
- local: .gitlab/ci/base_register_stage.yml
variables:
# CI variables
DOCKER_DRIVER: overlay2
DOCKER_HOST: tcp://localhost:2375
DOCKER_TLS_CERTDIR: ''
GIT_STRATEGY: fetch
GIT_DEPTH: 5
# Globals
DOCKER_VERSION: '18'
PYTHON_VERSION: '3.6-alpine'
stages:
- 'Prepare'
- 'Build Image'
- 'Registration'
- 'Deploy'
.prepare_stage:
stage: 'Prepare'
dependencies: []
.base_yarn_workspace_install:
extends: .prepare_stage
image: node:13-alpine
interruptible: true
cache:
key: '$CI_JOB_NAME-$CI_COMMIT_REF_SLUG'
paths:
- $CI_PROJECT_DIR/.cache
before_script:
- '[[ type jq ]] || apk add jq=~1'
- find packages -maxdepth 1 -type d -not -name ${CONTEXT} | tail -n +2 |
xargs -I {} sh -c "[[ -f "{}/package.json" ]] || exit 0 ; {
rm {}/package.json ;
jq 'del(.dependencies) | del(.devDependencies)' > {}/package.json
; } < {}/package.json"
script:
- '[[ type jq ]] || apk add jq=~1'
- "{ rm package.json; jq 'del(.dependencies) | del(.devDependencies)' > package.json; } < package.json"
- yarn config set cache-folder $CI_PROJECT_DIR/.cache/yarn
- yarn --frozen-lockfile --prefer-offline
artifacts:
expire_in: 1 day
paths:
- node_modules
- packages/${CONTEXT}/node_modules
################################################################################################
################################### ##############################################
################################### PREPARE ##############################################
################################### ##############################################
################################################################################################
Install app:
extends: .base_yarn_workspace_install
variables:
CONTEXT: app
Install env:
extends: .base_yarn_workspace_install
variables:
CONTEXT: env
################################################################################################
################################### #########################################
################################### BUILD IMAGE #########################################
################################### #########################################
################################################################################################
Build @gameofblocks/app:
stage: 'Build Image'
variables:
INVALID_GIT_FILES_CHANGES: 'true'
dependencies:
- Install app
- Install env
needs:
- Install app
- Install env
image: node:12-alpine
script:
- yarn workspace @gameofblocks/app build
artifacts:
expire_in: 1 day
paths:
- packages/app/.next
- packages/app/dist
################################################################################################
################################### #########################################
################################### REGISTRATION #########################################
################################### #########################################
################################################################################################
#
# docker image registration
# these base jobs can be reused to build and publish your docker images
#
.base_docker_image_stage:
image: docker:19
services:
- docker:19-dind
.base_stage:
except:
variables:
# Don't run when running e2e tests
- $E2E_TEST
# Don't run when deploying in production an existing image
- $PRODUCTION
# Don't run when tagging a commit
- $RELEASE
.register_stage:
extends: .base_stage
stage: 'Registration'
interruptible: true
dependencies: []
Register_app_image:
extends:
- .base_register_stage
- .register_stage
dependencies:
- Install app
- Install env
- Build @gameofblocks/app
needs:
- Install app
- Install env
- Build @gameofblocks/app
before_script:
- cp yarn.lock packages/app/yarn.lock
- cp lerna.json packages/app/lerna.json
- mkdir packages/app/packages
- cp -r packages/env packages/app/packages/env
variables:
CONTEXT: packages/app
IMAGE_NAME: $CI_REGISTRY_IMAGE/app
# HASURA
Register_hasura_image:
extends:
- .base_register_stage
- .register_stage
variables:
CONTEXT: packages/hasura
IMAGE_NAME: $CI_REGISTRY_IMAGE/hasura
################################################################################################
################################### #########################################
################################### DEPLOY #########################################
################################### #########################################
################################################################################################
############### Deploy APP ###############################
deploy_review_app:
image:
name: lachlanevenson/k8s-kubectl:latest
entrypoint: ['/bin/sh', '-c']
stage: 'Deploy'
only:
- branches
except:
- tags
environment:
on_stop: stop_review
name: review/$CI_BUILD_REF_NAME-app
url: https://$CI_ENVIRONMENT_SLUG.dev.gameofblocks.io
before_script:
- apk add -u gettext
- apk add --update -t deps curl
- kubectl create secret docker-registry regcred --docker-server=$CI_REGISTRY --docker-username="$CI_DEPLOY_USER" --docker-password="$CI_DEPLOY_PASSWORD" --docker-email="$GITLAB_USER_EMAIL" --dry-run=true -o yaml | kubectl apply -f -
script:
- kubectl version
- echo $CI_BUILD_REF_NAME
- echo "$CI_REGISTRY_IMAGE/app:$CI_COMMIT_SHA"
- echo $CI_REGISTRY
- echo $CI_COMMIT_REF_SLUG
- echo https://${CI_ENVIRONMENT_SLUG}.dev.gameofblocks.io
- echo $CI_REGISTRY_IMAGE
- echo $CI_COMMIT_SHA
- echo $DIGITAL_OCEAN_TOKEN
- echo $CURL_HEADERS
- echo $CURL_BODY
- echo $CURL_REQUEST
- cd .k8s/app
- envsubst < deployment.yaml > "/tmp/deployment.yaml"
- envsubst < ingress.yaml > "/tmp/ingress.yaml"
- envsubst < service.yaml > "/tmp/service.yaml"
- kubectl apply -f /tmp/deployment.yaml
- kubectl apply -f /tmp/service.yaml || true
- kubectl apply -f /tmp/ingress.yaml
- kubectl rollout status -f /tmp/deployment.yaml
- kubectl get deploy,svc,ing,pod -l app="$(echo ${CI_PROJECT_NAME} | tr "." "-")",ref="${CI_ENVIRONMENT_SLUG}"
############### Deploy Hasura ###############################
deploy_review_hasura:
image:
name: lachlanevenson/k8s-kubectl:latest
entrypoint: ['/bin/sh', '-c']
stage: 'Deploy'
only:
- branches
except:
- tags
environment:
name: review/$CI_BUILD_REF_NAME-hasura
url: https://hasura-$CI_ENVIRONMENT_SLUG.dev.gameofblocks.io
before_script:
- apk add -u gettext
- apk add --update -t deps curl
- kubectl create secret docker-registry regcred --docker-server=$CI_REGISTRY --docker-username="$CI_DEPLOY_USER" --docker-password="$CI_DEPLOY_PASSWORD" --docker-email="$GITLAB_USER_EMAIL" --dry-run=true -o yaml | kubectl apply -f -
- |-
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer ${DIGITAL_OCEAN_TOKEN}" -d '{"name": "'"$CI_COMMIT_REF_SLUG"'"}' "https://api.digitalocean.com/v2/databases/${DATA_BASE_ID}/dbs"
script:
- kubectl version
- echo $CI_BUILD_REF_NAME
- echo "$CI_REGISTRY_IMAGE/app:$CI_COMMIT_SHA"
- echo $CI_REGISTRY
- echo $CI_COMMIT_REF_SLUG
- echo https://hasura-${CI_ENVIRONMENT_SLUG}.dev.gameofblocks.io
- echo $CI_REGISTRY_IMAGE
- echo $CI_COMMIT_SHA
- echo $DIGITAL_OCEAN_TOKEN
- echo $CURL_HEADERS
- echo $CURL_BODY
- echo $CURL_REQUEST
- cd .k8s/hasura
- envsubst < deployment.yaml > "/tmp/deployment.yaml"
- envsubst < ingress.yaml > "/tmp/ingress.yaml"
- envsubst < service.yaml > "/tmp/service.yaml"
- kubectl apply -f /tmp/deployment.yaml
- kubectl apply -f /tmp/service.yaml || true
- kubectl apply -f /tmp/ingress.yaml
- kubectl rollout status -f /tmp/deployment.yaml
- kubectl get deploy,svc,ing,pod -l app="$(echo ${CI_PROJECT_NAME} | tr "." "-")",ref="${CI_ENVIRONMENT_SLUG}"
############### Stop APP ###############################
stop_review:
image:
name: lachlanevenson/k8s-kubectl:latest
entrypoint: ['/bin/sh', '-c']
stage: 'Deploy'
variables:
GIT_STRATEGY: none
when: manual
environment:
name: review/$CI_BUILD_REF_NAME-app
action: stop
script:
- kubectl version
- kubectl delete ing -l ref=${CI_ENVIRONMENT_SLUG}
- kubectl delete all -l ref=${CI_ENVIRONMENT_SLUG}