forked from OpenFuturePlatform/open-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
240 lines (215 loc) · 6.34 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
image: docker:latest
services:
- docker:dind
stages:
- test
- build
- package
- deploy
- certificates
variables:
DOCKER_DRIVER: overlay2
IMAGE_NAME: gcr.io/zinc-computer-205115/apiopenfuture
IMAGE_TAG: ${CI_COMMIT_REF_NAME}-${CI_PIPELINE_ID}
###########################
# Building
###########################
unit-tests:
stage: test
image: openjdk:8-jdk
variables:
POSTGRES_HOST: "postgres"
POSTGRES_DB: "open-api"
POSTGRES_USER: "open-api"
POSTGRES_PASSWORD: "open-api"
GOOGLE_CLIENT_ID: $GOOGLE_CLIENT_ID_DEV
GOOGLE_CLIENT_SECRET: $GOOGLE_CLIENT_SECRET_DEV
NETWORK_URL: $NETWORK_URL_DEV
ETHEREUM_PRIVATE_KEY: $ETHEREUM_PRIVATE_KEY_DEV
OPEN_TOKEN_ADDRESS: $OPEN_TOKEN_ADDRESS_DEV
services:
- name: postgres
alias: $POSTGRES_HOST
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
- apt update && apt install make gcc g++ -y
script:
- ./gradlew check
after_script:
- cat build/reports/jacoco/test/html/index.html
coverage: '/Total.*?([0-9]{1,3})%/'
cache:
paths:
- .gradle/wrapper
- .gradle/caches
- frontend/.gradle/nodejs
artifacts:
when: always
paths:
- build/reports/
expire_in: 1 week
except:
- schedules
build-jar:
stage: build
image: openjdk:8-jdk
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
- apt update && apt install make gcc g++ -y
script:
- ./gradlew assemble
cache:
paths:
- .gradle/wrapper
- .gradle/caches
- frontend/.gradle/nodejs
artifacts:
paths:
- build/libs/*.jar
expire_in: 1 week
only:
- master
- sprint
except:
- schedules
###########################
# Packaging
###########################
.registry-auth-script: ®istry_auth
- docker login -u _json_key -p "${GOOGLE_JSON_TOKEN}" https://gcr.io
package-docker:
stage: package
before_script: *registry_auth
script:
- docker build -t ${IMAGE_NAME}:${IMAGE_TAG} -f docker/Dockerfile .
- docker push ${IMAGE_NAME}:${IMAGE_TAG}
only:
- sprint
- master
except:
- schedules
###########################
# Deployment
###########################
.prepare-key-script: &prepare_key
- apk add --no-cache openssh-client
- eval $(ssh-agent -s)
- echo "$DEPLOY_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan $DEPLOY_HOST >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
.deploy-to-host-script: &deploy_to_host
- ssh ${DEPLOY_USER}@${DEPLOY_HOST} "docker login -u _json_key -p '${GOOGLE_JSON_TOKEN}' https://gcr.io"
- ssh ${DEPLOY_USER}@${DEPLOY_HOST} "docker pull ${IMAGE_NAME}:${IMAGE_TAG}"
- |
ssh ${DEPLOY_USER}@${DEPLOY_HOST} "
docker stop ${CONTAINER_NAME} && \
IMG=\$(docker ps -a --filter='name=^/${CONTAINER_NAME}$' --format='{{.Image}}') && \
docker rm -f ${CONTAINER_NAME} && \
docker rmi \$IMG; \
exit 0
"
- |
ssh ${DEPLOY_USER}@${DEPLOY_HOST} "
docker run -d --name '${CONTAINER_NAME}' --restart=always \
-p 8080:8080 \
-e "POSTGRES_HOST=${POSTGRES_HOST}" \
-e "POSTGRES_DB=${POSTGRES_DB}" \
-e "POSTGRES_USER=${POSTGRES_USER}" \
-e "POSTGRES_PASSWORD=${POSTGRES_PASSWORD}" \
-e "GOOGLE_CLIENT_ID=${GOOGLE_CLIENT_ID}" \
-e "GOOGLE_CLIENT_SECRET=${GOOGLE_CLIENT_SECRET}" \
-e "NETWORK_URL=${NETWORK_URL}" \
-e "ETHEREUM_PRIVATE_KEY=${ETHEREUM_PRIVATE_KEY}" \
-e "OPEN_TOKEN_ADDRESS=${OPEN_TOKEN_ADDRESS}" \
${IMAGE_NAME}:${IMAGE_TAG}
"
deploy-development:
stage: deploy
variables:
DEPLOY_USER: $DEPLOY_USER_DEV
DEPLOY_HOST: $DEPLOY_HOST_DEV
DEPLOY_KEY: $DEPLOY_KEY_DEV
CONTAINER_NAME: open-api
POSTGRES_HOST: $POSTGRES_HOST_DEV
POSTGRES_DB: $POSTGRES_DB_DEV
POSTGRES_USER: $POSTGRES_USER_DEV
POSTGRES_PASSWORD: $POSTGRES_PASSWORD_DEV
GOOGLE_CLIENT_ID: $GOOGLE_CLIENT_ID_DEV
GOOGLE_CLIENT_SECRET: $GOOGLE_CLIENT_SECRET_DEV
NETWORK_URL: $NETWORK_URL_DEV
ETHEREUM_PRIVATE_KEY: $ETHEREUM_PRIVATE_KEY_DEV
OPEN_TOKEN_ADDRESS: $OPEN_TOKEN_ADDRESS_DEV
before_script: *prepare_key
script: *deploy_to_host
only:
- sprint
environment:
name: development
url: http://api.open-platform.zensoft.io
except:
- schedules
deploy-production:
stage: deploy
variables:
DEPLOY_USER: $DEPLOY_USER_PROD
DEPLOY_HOST: $DEPLOY_HOST_PROD
DEPLOY_KEY: $DEPLOY_KEY_PROD
CONTAINER_NAME: open-api
POSTGRES_HOST: $POSTGRES_HOST_PROD
POSTGRES_DB: $POSTGRES_DB_PROD
POSTGRES_USER: $POSTGRES_USER_PROD
POSTGRES_PASSWORD: $POSTGRES_PASSWORD_PROD
GOOGLE_CLIENT_ID: $GOOGLE_CLIENT_ID_PROD
GOOGLE_CLIENT_SECRET: $GOOGLE_CLIENT_SECRET_PROD
NETWORK_URL: $NETWORK_URL_PROD
ETHEREUM_PRIVATE_KEY: $ETHEREUM_PRIVATE_KEY_PROD
OPEN_TOKEN_ADDRESS: $OPEN_TOKEN_ADDRESS_PROD
before_script: *prepare_key
script: *deploy_to_host
only:
- master
environment:
name: production
url: https://api.openfuture.io
except:
- schedules
###########################
# Certificates
###########################
.update-certificates-script: &update_certificates
- |
ssh ${DEPLOY_USER}@${DEPLOY_HOST} "
docker run --rm -t --name certbot \
-v /etc/letsencrypt:/etc/letsencrypt \
-v /var/www/certbot:/var/www/certbot \
-v /var/log/letsencrypt:/var/log/letsencrypt \
certbot/certbot \
certonly --webroot -w /var/www/certbot/ \
-d ${SERVER_NAME} \
--renew-by-default -m [email protected] --eff-email --agree-tos --manual-public-ip-logging-ok
"
- ssh ${DEPLOY_USER}@${DEPLOY_HOST} "sudo service nginx reload"
certificates-development:
stage: certificates
variables:
DEPLOY_USER: $DEPLOY_USER_DEV
DEPLOY_HOST: $DEPLOY_HOST_DEV
DEPLOY_KEY: $DEPLOY_KEY_DEV
SERVER_NAME: "api.open-platform.zensoft.io"
before_script: *prepare_key
script: *update_certificates
only:
- schedules
certificates-production:
stage: certificates
variables:
DEPLOY_USER: $DEPLOY_USER_PROD
DEPLOY_HOST: $DEPLOY_HOST_PROD
DEPLOY_KEY: $DEPLOY_KEY_PROD
SERVER_NAME: "api.openfuture.io"
before_script: *prepare_key
script: *update_certificates
only:
- schedules