-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
163 lines (151 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
stages:
- docs
- build and publish
- test
- deploy to lab
# DOCKER_REGISTRY is defined as CI/CD variables in GitLab for the whole
# tnc-service-team group, and is automatically set for every project in
# the group; it points to Harbor in lab on thin.thenetcircle.lab:5000
variables:
IMAGE_URL: $DOCKER_REGISTRY/library/eventbus3
IMAGE_LATEST: $IMAGE_URL:latest
COMM_LIST: "popp,8899 feti,9035 gays,9032 happ,9045 kauf,9001"
# update the handbook repo
build docs:
tags:
# decided which gitlab runner will build the docs, this has to be `thin-runner`
# since the script will update the handbook repo from this machine where it's
# cloned
- thin-runner
stage: docs
# adjust the `wget` and `git add` commands with the correct location and filename
script:
- >
cd /usr/local/tncdata/source/handbook &&
git clean --force -d -x &&
git reset --hard &&
git checkout master &&
git fetch &&
git pull &&
wget -O /usr/local/tncdata/source/handbook/docs/eventbus3.md http://gitlab.thenetcircle.lab/tnc-service-team/eventbus3/-/raw/main/README.md &&
cd /usr/local/tncdata/source/handbook &&
git add docs/eventbus3.md &&
(git diff --quiet && git diff --staged --quiet || git commit -m 'updated eventbus3 doc') &&
git push
# build the image and push to Harbor in lab
build:
# use the specific docker version, `:latest` has been broken for a long time
image: $DOCKER_REGISTRY/dockerhub-replica/docker:20.10.12
stage: build and publish
# this makes sure we build from within another docker container (dind)
services:
- name: $DOCKER_REGISTRY/dockerhub-replica/docker:dind
command:
[
"--insecure-registry=thin.thenetcircle.lab:5000 --insecure-registry=http://thin.thenetcircle.lab:5000",
]
tags:
# either `docker` or `overlay2`
- docker
script:
- DOCKER_BUILDKIT=1 docker build -f Dockerfile -t $IMAGE_URL:$CI_COMMIT_SHORT_SHA .
- echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin $DOCKER_REGISTRY
- docker push $IMAGE_URL:$CI_COMMIT_SHORT_SHA
- docker tag $IMAGE_URL:$CI_COMMIT_SHORT_SHA $IMAGE_LATEST
- docker push $IMAGE_LATEST
# another stage for running tests; here we need to replace the entrypoint
# when running, otherwise uvicorn will start from the default entrypoint
coverage:
# no point in trying to run the tests if the previous build failed
when: on_success
# we run the image from inside another docker container to make sure
# the gitlab runner environment doesn't interfere
image: $DOCKER_REGISTRY/dockerhub-replica/docker:20.10.12
stage: test
variables:
# don't need to clone the repo for this stage, since we're pulling the
# image from Harbor
GIT_STRATEGY: none
environment:
name: lab
deployment_tier: testing
services:
- name: $DOCKER_REGISTRY/dockerhub-replica/docker:dind
command:
[
"--insecure-registry=thin.thenetcircle.lab:5000 --insecure-registry=http://thin.thenetcircle.lab:5000",
]
tags:
# use either `docker` or `overlay2`
- docker
script:
# remove the test container if it's already running
- docker container rm -f eventbus3-test || true
- docker pull $IMAGE_LATEST
# here we're overwriting the entrypoint with the test script
- >
docker run --name eventbus3-test -e "EB_CONF_FILE=/app/configs/test.yml" $IMAGE_LATEST coverage
# this line is needed for gitlab to be able to display a test coverage
# percentage badge on the repository page; it's a regex to catch the final
# output from `coverage`
coverage: "/TOTAL.+ ([0-9]{1,3}%)/"
# deploy from inside an alpine docker container, to make sure we don't fail
# because of changes to the runner machine
deploy:
when: on_success
# 'environment' lets us choose which values to use for SERVER_USER/IP and ID_RSA
environment:
name: lab
deployment_tier: development
image: $DOCKER_REGISTRY/dockerhub-replica/alpine:latest # 3.18.3 when manually uploaded to harbor; china blocking docker hub
stage: deploy to lab
only:
# either `main` or `master`, the name of the main branch in the repo
- main
tags:
- docker
# SERVER_USER/IP is defined in GitLab as CI/CD variables, one for each
# environment (only lab currently) and is for now gitlab@fat; ID_RSA
# is a generated private key for the gitlab user on fat, also a CI/CD
# variable
#
# also use the chinese mirror, otherwise apk will timeout sometimes,
# breaking the build
script:
- chmod og= $ID_RSA
- echo http://mirrors.bfsu.edu.cn/alpine/v3.15/main > /etc/apk/repositories;
echo http://mirrors.bfsu.edu.cn/alpine/v3.15/community >> /etc/apk/repositories
- apk update && apk add openssh
- ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP
"echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin $DOCKER_REGISTRY"
# get the latest image from harbor, which is the one we built earlier
- ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP
"docker pull $IMAGE_LATEST"
# remove any existing deployment of this image
- ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP
"IFS=','; for item in ${COMM_LIST}; do set -- \$item;
docker container rm -f eventbus3-\${1}-consumer eventbus3-\${1}-producer || true;
done"
- ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP
"IFS=','; for item in ${COMM_LIST}; do set -- \$item;
docker run -d
-p \${2}:8000
-e \"EB_COMM=\${1}\"
-e \"EB_CONF_FILE=/app/configs/lab.yml\"
--restart unless-stopped
--log-opt max-size=50m
--log-opt max-file=1
--name eventbus3-\${1}-producer
$IMAGE_LATEST producer;
done"
- ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP
"IFS=','; for item in ${COMM_LIST}; do set -- \$item;
docker run -d
-e \"EB_COMM=\${1}\"
-e \"EB_CONF_FILE=/app/configs/lab.yml\"
--restart unless-stopped
--log-opt max-size=50m
--log-opt max-file=1
--name eventbus3-\${1}-consumer
$IMAGE_LATEST consumer;
done"