-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
138 lines (127 loc) · 4.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
cache:
untracked: true
key: $CI_COMMIT_REF_NAME
paths:
- .yarn-cache/
variables:
# From the docs: https://docs.gitlab.com/ee/ci/yaml/#git-strategy
# „fetch is faster as it re-uses the project workspace (falling back
# to clone if it doesn't exist). git clean is used to undo any changes
# made by the last job, and git fetch is used to retrieve commits
# made since the last job ran.“
GIT_STRATEGY: fetch
# The 'latest' image. Used as a base to cache from
# when building the $BUILD_IMAGE.
SERVICE_NAME: claim-brk
stages:
- setup
- test
- build
- deploy
- stop-review
before_script:
- eval export REGISTRY_URL="eu.gcr.io/sonorous-cacao-185213/${SERVICE_NAME}"
- eval export SHORT_GIT_SHA=$(git rev-parse --short HEAD)
- eval export SANITIZED_BRANCH_NAME=$(echo $CI_COMMIT_REF_NAME | sed -e "s/\//-/g")
# The image tag of this build
- eval export BUILD_IMAGE=${REGISTRY_URL}:${SANITIZED_BRANCH_NAME}-${SHORT_GIT_SHA}
- eval export CACHE_TAG=${REGISTRY_URL}:cache
# The fqdn of the gitlab server is not available in the standard env vars so we cut it out of this URL
- eval export GITLAB_HOSTNAME=$(echo $CI_PROJECT_URL | awk -F/ '{print $3}')
setup:
stage: setup
artifacts:
expire_in: 3d
paths:
- node_modules
script:
- eval `ssh-agent -s`
- ssh-add <(echo "$GITLAB_BOT_SSH_KEY")
- yarn install --pure-lockfile --cache-folder .yarn-cache
build-and-push-docker-image:
stage: build
# This is OR logic. The commit should be in master OR be tagged
cache:
policy: pull
script:
- gcloud docker --authorize-only
# Using `--pull` makes sure that Docker fetches any changes to the $RELEASE_IMAGE before building
# just in case our cache is stale. It takes slightly longer, but means we don’t get stuck without
# potential security patches to base images.
#
# Using `--cache-from $RELEASE_IMAGE` can speed up building the $BUILD_IMAGE
# https://medium.com/@gajus/making-docker-in-docker-builds-x2-faster-using-docker-cache-from-option-c01febd8ef84
- docker build --pull --cache-from $CACHE_TAG --tag $BUILD_IMAGE --build-arg GITLAB_BOT_SSH_KEY="$GITLAB_BOT_SSH_KEY" .
- docker push $BUILD_IMAGE
- docker tag $BUILD_IMAGE $CACHE_TAG
- docker push $CACHE_TAG
deploy-review:
stage: deploy
environment:
name: rev-${SERVICE_NAME}--$CI_BUILD_REF_NAME
url: https://rev-${SERVICE_NAME}--$CI_COMMIT_REF_SLUG.brickblock-cloud.io
on_stop: stop-review
only:
- branches
except:
- master
tags:
- google
variables:
MYSQL_INSTANCE: sonorous-cacao-185213:europe-west1:staging-claim-brk
MYSQL_DATABASE: rev-${SERVICE_NAME}--$CI_COMMIT_REF_SLUG
script:
- docker login $CI_REGISTRY -u gitlab-ci-token -p $CI_JOB_TOKEN
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@${GITLAB_HOSTNAME}/infrastructure/infra.git
- /bin/bash $(pwd)/infra/k8s-db/deploy.sh rev-clm-brk--$CI_COMMIT_REF_SLUG
stop-review:
stage: stop-review
environment:
name: rev-${SERVICE_NAME}--$CI_BUILD_REF_NAME
action: stop
when: manual
tags:
- google
script:
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@${GITLAB_HOSTNAME}/infrastructure/infra.git
- /bin/bash $(pwd)/infra/k8s-db/purge.sh rev-clm-brk--$CI_COMMIT_REF_SLUG
only:
- branches
except:
- master
deploy-staging:
stage: deploy
environment:
name: staging-${SERVICE_NAME}
url: https://staging-${SERVICE_NAME}.brickblock-cloud.io
only:
- master
tags:
- google
variables:
MYSQL_DATABASE: staging_claim_brk
MYSQL_INSTANCE: sonorous-cacao-185213:europe-west1:staging-claim-brk
script:
- docker login $CI_REGISTRY -u gitlab-ci-token -p $CI_JOB_TOKEN
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@${GITLAB_HOSTNAME}/infrastructure/infra.git
- /bin/bash $(pwd)/infra/k8s-db/deploy.sh staging-${SERVICE_NAME}
deploy-production:
stage: deploy
environment:
name: production-${SERVICE_NAME}
url: https://production-${SERVICE_NAME}.brickblock-cloud.io
script:
tags:
- google
variables:
MYSQL_DATABASE: production_claim_brk
MYSQL_INSTANCE: sonorous-cacao-185213:europe-west1:production-claim-brk
script:
- docker login $CI_REGISTRY -u gitlab-ci-token -p $CI_JOB_TOKEN
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@${GITLAB_HOSTNAME}/infrastructure/infra.git
- /bin/bash $(pwd)/infra/k8s-db/deploy.sh production-${SERVICE_NAME}
# These two 'onlys' give us AND logic. The commit should both be in master AND be tagged. NB. this is git tagging. not 'gitlab' tagging.
only:
- master
only:
- tags