-
Notifications
You must be signed in to change notification settings - Fork 1
/
.gitlab-ci.yml
355 lines (319 loc) · 12.9 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
image: docker:20.10.22
# When using dind, it's wise to use the overlayfs driver for
# improved performance.
variables:
GIT_SUBMODULE_STRATEGY: normal
GIT_SUBMODULES: recursive
# This is used to connect the docker-client intalled into 'php:latest' image, to docker daemon
DOCKER_HOST: tcp://docker:2375/
# https://gitlab.rm.ingv.it/help/ci/pipelines/settings.md
GIT_STRATEGY: clone
# BUG!!! - Remove this (DOCKER_TLS_CERTDIR: "") when solved!!! - https://gitlab.com/gitlab-org/gitlab-runner/issues/4501
DOCKER_TLS_CERTDIR: ""
#
OAS_BASEDIR: "public/api/0.0.2"
OAS_FILENAME: "openapi.yaml"
#
DOCKER_BUILDKIT: 1
COMPOSE_DOCKER_CLI_BUILD: 1
#
DOCKER_IMAGE_TAG_TMP: gitlab-ci-${CI_COMMIT_REF_NAME}
services:
# Remeber to set 'privileged = true' into the runner 'config.toml' file (es on hgp5: /srv/gitlab-runner/config/config.toml)
#- docker:dind
- docker:20.10.12-dind
- command:["--registry-mirror", "https://mirror.gcr.io"]
stages:
- dockerhub
- pre_check
- build
- test
- rebuild_without_dev
- push
- clean_dockerhub
- all_stages
- pages
- deploy
include:
- remote: 'https://raw.githubusercontent.com/vlauciani/gitlabci-include-for-dockerhub-rate-limit/main/dockerhub-rate-limit.yml'
- remote: 'https://raw.githubusercontent.com/vlauciani/gitlabci-include-for-api-oas-checker/main/api-oas-checker.yml'
before_script:
# Print project name
- echo ${CI_PROJECT_NAME}
# DockerHub login
- echo ${CI_DOCKERHUB_REGISTRY_TOKEN} | docker login -u ${CI_DOCKERHUB_REGISTRY_USER} --password-stdin
#
- date
- uname -a
- echo ${OAS_DIR}
# Update repo
- apk update
- apk upgrade
#
- docker -v
- ALPINE_VERSION=$( cat /etc/os-release | grep "VERSION_ID" | awk -F"=" '{print $2}' )
- >
if [[ "${CI_COMMIT_REF_NAME}" == "develop" ]]; then
FILE_DOCKER_COMPOSE="docker-compose.dev.yml";
else
FILE_DOCKER_COMPOSE="docker-compose.prod.yml";
fi
- echo FILE_DOCKER_COMPOSE=${FILE_DOCKER_COMPOSE}
#
- echo CI_COMMIT_REF_NAME=${CI_COMMIT_REF_NAME}
- CI_COMMIT_REF_NAME_WITHOUT_V=$( echo ${CI_COMMIT_REF_NAME} | sed "s/^v//" )
#
- echo DOCKER_IMAGE_TAG_TMP=${DOCKER_IMAGE_TAG_TMP}
- export APOLLO_VERSION=${DOCKER_IMAGE_TAG_TMP}
- echo APOLLO_VERSION=${APOLLO_VERSION}
# From: https://www.docker.com/blog/checking-your-current-docker-pull-rate-limits-and-status/
dockerhub-rate-limit:
stage: dockerhub
variables:
INCLUDE_DOCKERHUB_REGISTRY_USER: ${CI_DOCKERHUB_REGISTRY_USER}
INCLUDE_DOCKERHUB_REGISTRY_PSW: ${CI_DOCKERHUB_REGISTRY_TOKEN}
api-oas-checker:
stage: pre_check
variables:
INCLUDE_OAS_BASEDIR: ${OAS_BASEDIR}
INCLUDE_OAS_FILENAME: ${OAS_FILENAME}
validate_oa3:
stage: pre_check
script:
- ls -l $(pwd)/public/api/0.0.2 # to be removed when https://github.com/italia/api-oas-checker/issues/60 is solved
# This command should work, but the command 'spectral lint --version' doesn't work: https://github.com/stoplightio/spectral/issues/1483
# then I wrote a workaround
#- docker run --rm -v $(pwd)/public/api/0.0.3:/tmp stoplight/spectral lint -r https://italia.github.io/api-oas-checker/spectral.yml "/tmp/openapi.yaml"
# This is the workaround
- docker run --rm --entrypoint=sh -v $(pwd)/public/api/0.0.2:/tmp stoplight/spectral:5.9.1 -c "spectral lint -r https://italia.github.io/api-oas-checker/spectral.yml /tmp/openapi.yaml"
except:
refs:
- develop
changes:
- HISTORY
validate_publiccode:
stage: pre_check
script:
- docker run --rm -v $(pwd)/publiccode.yml:/publiccode_folder/publiccode.yml italia/publiccode-parser-go /publiccode_folder/publiccode.yml
validate_version_number_and_date:
stage: pre_check
script:
- apk add --no-cache bash git
- ./${CI_PROJECT_NAME}/scripts/validate_version_number_and_date.sh
Build:
stage: build
#tags:
# Runners:
# 1) 'laradock' runner is working on 'albus.int.ingv.it' valentino's MAC.
# 2) 'test' runner is working on 'docker.int.ingv.it' server.
# 3) 'hgp5_runner' runner is working on 'hgp5.int.ingv.it' server.
# 4) 'sqlx2-srvr_runner' runner is working on 'sqlx2-srvr.int.ingv.it' server.
#- sqlx2-srvr_runner
# !!! Attenzione !!!, per sfruttare la cache il runner deve essere lo stessi in quanto la cache e' 'local' e quindi e' sull'host.
#cache:
# key: ${CI_COMMIT_REF_SLUG}-composer
# paths:
# - vendor/
# - .env
script:
# Configure Laravel ENV
- cp ./.env.example ./.env
- cat ./.env
# Build and up
- docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} build --no-cache --pull --progress=plain
- docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} push
- docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} down -v
- docker images
phpunit-test:
stage: test
#tags:
# Runners:
# 1) 'laradock' runner is working on 'albus.int.ingv.it' valentino's MAC.
# 2) 'test' runner is working on 'docker.int.ingv.it' server.
# 3) 'hgp5_runner' runner is working on 'hgp5.int.ingv.it' server.
# 4) 'sqlx2-srvr_runner' runner is working on 'sqlx2-srvr.int.ingv.it' server.
#- sqlx2-srvr_runner
# !!! Attenzione !!!, per sfruttare la cache il runner deve essere lo stessi in quanto la cache e' 'local' e quindi e' sull'host.
#cache:
# key: ${CI_COMMIT_REF_SLUG}-composer
# paths:
# - vendor/
# - .env
script:
# Configure Laravel ENV
- cp ./.env.example ./.env
- cat ./.env
- docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} up -d --remove-orphans
- docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} exec -T apollo composer install
# Check 'hyp2000' is started
- |
COUNT=1
COUNT_LIMIT=30
while ! $( docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} exec -T apollo curl --output /dev/null --silent --head --fail "http://hyp2000:8080" ); do
echo " ${COUNT}/${COUNT_LIMIT} - waiting hyp2000 starts...";
if [ "${COUNT}" -ge "${COUNT_LIMIT}" ]; then
exit 1
fi
sleep 5;
COUNT=$(( ${COUNT} + 1 ));
done
- docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} exec -T apollo curl --output /dev/null --head --fail "http://hyp2000:8080"
# Check 'pyml' is started
- |
COUNT=1
COUNT_LIMIT=30
while ! $( docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} exec -T apollo curl --output /dev/null --silent --head --fail "http://pyml:8080" ); do
echo " ${COUNT}/${COUNT_LIMIT} - waiting pyml starts...";
if [ "${COUNT}" -ge "${COUNT_LIMIT}" ]; then
exit 1
fi
sleep 5;
COUNT=$(( ${COUNT} + 1 ));
done
- docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} exec -T apollo curl --output /dev/null --head --fail "http://pyml:8080"
# Run Tests
- docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} exec -T apollo php artisan route:list
- docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} exec -T apollo php artisan test --stop-on-failure
- docker compose -f docker-compose.yml -f ${FILE_DOCKER_COMPOSE} exec -T apollo vendor/bin/phpstan
after_script:
# ATTENTION!!! Set the correct container name!
- docker cp apollo-apollo-1:/app/storage/. /builds/caravel/apollo/storage/
artifacts:
when: always
paths:
- storage
expire_in: 1 day
# Push only 'develop' and 'tag' image. For 'tag', a 'latest' tag will be created
push final image to DockerHub:
stage: push
script:
- docker compose pull
- docker images
- >
if [[ "${CI_COMMIT_REF_NAME}" == "develop" ]]; then
export APOLLO_VERSION_FOR_PUSH=${CI_COMMIT_REF_NAME};
else
export APOLLO_VERSION_FOR_PUSH=${CI_COMMIT_REF_NAME_WITHOUT_V};
fi
- echo APOLLO_VERSION_FOR_PUSH=${APOLLO_VERSION_FOR_PUSH}
#
- docker tag vlauciani/caravel-apollo:${APOLLO_VERSION} vlauciani/caravel-apollo:${APOLLO_VERSION_FOR_PUSH}
- >
if [[ "${CI_COMMIT_REF_NAME}" != "develop" ]]; then
docker tag vlauciani/caravel-apollo:${APOLLO_VERSION} vlauciani/caravel-apollo:latest;
fi
#
- docker images
#
- docker push vlauciani/caravel-apollo:${APOLLO_VERSION_FOR_PUSH}
- >
if [[ "${CI_COMMIT_REF_NAME}" != "develop" ]]; then
docker push vlauciani/caravel-apollo:latest
fi
except:
- schedule
only:
- develop
- tags
clean DockerHub:
stage: clean_dockerhub
script:
- docker images
- docker run --rm lumir/remove-dockerhub-tag --user ${CI_DOCKERHUB_REGISTRY_USER} --password ${CI_DOCKERHUB_REGISTRY_TOKEN} vlauciani/caravel-apollo:${DOCKER_IMAGE_TAG_TMP}
pages:
stage: pages
only:
- main
- develop
script:
- apk add --no-cache git rsync && rm -rf /var/cache/apk/*
- export NOW=$(date +%Y%m%dx%H%M%S)
- mkdir pages_tmp
# Create 'openapi-generator' config file
# option: 'disallowAdditionalPropertiesIfNotPresent: false' -> https://github.com/OpenAPITools/openapi-generator/issues/12550
# |
# |-> https://openapi-generator.tech/docs/generators/java/
- |
cat > pages_tmp/config.yaml << EOF
disallowAdditionalPropertiesIfNotPresent: false
EOF
- cat pages_tmp/config.yaml
- git fetch
- cp pages/index.html.template public/index.html
# from: openapitools/openapi-generator-cli
- for BRANCH in main ; do
git checkout ${BRANCH} ;
for CLIENT_LANGUAGE in java python html2 ; do
FILE_NAME="apollo-${CLIENT_LANGUAGE}-client-v$(cat VERSION)" ;
BRANCH_STRUPPER=$( echo "${BRANCH}" | awk '{print toupper($0)}' ) ;
CLIENT_LANGUAGE_STRUPPER=$( echo "${CLIENT_LANGUAGE}" | awk '{print toupper($0)}' ) ;
echo "****** FILE_NAME=${FILE_NAME} ******" ;
echo "openapi-generator-cli version:" ;
docker run --rm openapitools/openapi-generator-cli:v6.6.0 version ;
echo "" ;
echo "" ;
echo "openapi-generator-cli validate openapi.yaml:" ;
docker run --rm -v $(pwd)/public/api/0.0.2:/input openapitools/openapi-generator-cli:v6.6.0 validate -i /input/openapi.yaml ;
echo "" ;
echo "" ;
docker run
--rm
-v $(pwd)/pages_tmp:/output
-v $(pwd)/public/api/0.0.2:/input
openapitools/openapi-generator-cli
generate
-i /input/openapi.yaml
--artifact-id ${CLIENT_LANGUAGE}-openapi-generator-cli-apollo
--api-package org.ingv.apollo.api
--model-package org.ingv.apollo.model
--group-id org.ingv.apollo
--artifact-version $(cat VERSION)
-g ${CLIENT_LANGUAGE}
-o /output/${FILE_NAME} &&
cd pages_tmp/ ;
if [[ "${CLIENT_LANGUAGE}" == "html2" ]] ; then
cp -R ${FILE_NAME} ../public/ ;
else
tar -czf ../public/${FILE_NAME}.tar.gz ${FILE_NAME} ;
if [[ "${CLIENT_LANGUAGE}" == "python" ]] && [ ! -d ${CLIENT_LANGUAGE} ]; then
git clone https://gitlab+deploy-token-118:[email protected]/caravel/codegen/apollo-${CLIENT_LANGUAGE}.git &&
git config --global user.name "GitLab CI" &&
git config --global user.email "[email protected]" ;
elif [[ "${CLIENT_LANGUAGE}" == "java" ]] && [ ! -d ${CLIENT_LANGUAGE} ]; then
git clone https://gitlab+deploy-token-117:[email protected]/caravel/codegen/apollo-${CLIENT_LANGUAGE}.git &&
git config --global user.name "GitLab CI" &&
git config --global user.email "[email protected]" ;
fi ;
cd apollo-${CLIENT_LANGUAGE}/ &&
git switch ${BRANCH} &&
cd .. &&
echo "---1---" &&
ls -al ${FILE_NAME}/ &&
echo "---2---" &&
ls -al apollo-${CLIENT_LANGUAGE}/ &&
echo "---3---" &&
rsync -avh --delete --exclude '.git/' --exclude '.gitlab-ci.yml' ${FILE_NAME}/ apollo-${CLIENT_LANGUAGE}/ &&
echo "---4---" &&
cd apollo-${CLIENT_LANGUAGE}/ &&
echo "---5---" &&
git status &&
echo "---6---" &&
git add . &&
echo "---7---" &&
git commit -m "Update" &&
echo "---8---" &&
git branch -avv &&
echo "---9---" &&
git push origin ${BRANCH} ;
cd .. ;
fi ;
cd .. ;
sed -i'' -e "s/__LINK_${BRANCH_STRUPPER}_${CLIENT_LANGUAGE_STRUPPER}__/${FILE_NAME}/g" public/*.html ;
done ;
done
- sed -i'' -e "s/__NOW__/${NOW}/g" public/*.html
#- cp pages/* public/
artifacts:
expire_in: 1 week
paths:
- public/