-
Notifications
You must be signed in to change notification settings - Fork 5
/
.gitlab-ci.yml
226 lines (198 loc) · 10.5 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
# ######################################## #
# This file executes command on merge-requests
# for every installation / server / client
# ######################################## #
# Changelog:
# 2023-02-17 RRO Changed docker-compose recreated check and start without changing directory, as entry point is now /code/front & changeing owner of front directory to gitlab-runner at the beginning, as it is changed later to root
# 2022-01-26 RRO Added GITCLIENTID, GITCLIENTSECRET
# 2022-01-06 RRO Added gunicorn reload for behave and django
# ######################################## #
# include all server configuration
# files from local repository
include:
- 'gitlab-ci-yamls/*.yml'
# pulling content accidentally and
# losing old changes
variables:
#########################################################
# Build Images names
#########################################################
COMETA_FRONT: "cometa/front"
# This images are not build in this pipeline
# Need to add this to pipeline
COMETA_DJANGO: "cometa/django"
COMETA_BEHAVE: "cometa/behave"
COMETA_SOCKET: "cometa/socket"
COMETA_CRONTAB: "cometa/crontab"
COMETA_REDIS: "cometa/redis"
COMETA_NOVNC: "cometa/novnc"
# CI_DEBUG_TRACE: "true" # show verbose information
# GIT_STRATEGY: clone
COMETA_EMAIL_ENABLED: 'True'
# COMETA_REPLACE_FAVICON_IN: "front/src/index.html front/src/manifest.json front/src/welcome.html"
COMETA_REPLACE_FAVICON_IN: "/usr/local/apache2/htdocs/index.html /usr/local/apache2/htdocs/manifest.json /usr/local/apache2/htdocs/welcome.html"
COMETA_TOTAL_BROWSER_VERSION: 10 # number of latest browser version that will be pulled.
CONFIG_FILE: "front/src/assets/config.json"
# set deploy order in which jobs
# will be executed
stages:
- build
- deploy
# main script anchor to avoid code
# redundancy.
.deployment_script: &deployment_script
stage: deploy
script:
#########################################################
# REPLACE
#########################################################
# update docker-compose file depending on server variables
- sed -i 's/<outside_port>/'$DOCKER_HTTP_PORT'/g;s/<server>/'$DOCKER_OPENIDC_CONFIG_EXT'/g' docker-compose.yml
# Replace secrets in openidc
- sed -i 's/@@COMETA_CRYPTO_PASSPHRASE@@/'$COMETA_CRYPTO_PASSPHRASE'/g' front/apache-conf/openidc.conf_${DOCKER_OPENIDC_CONFIG_EXT}
# Replace Stripe client keys
# - sed -i 's/@@COMETA_STRIPE_PUBLIC_LIVE_KEY@@/'$COMETA_STRIPE_PUBLIC_LIVE_KEY/'g' front/src/app/deploy-tokens.ts
# - sed -i 's/@@COMETA_STRIPE_PUBLIC_TEST_KEY@@/'$COMETA_STRIPE_PUBLIC_TEST_KEY'/g' front/src/app/deploy-tokens.ts
#########################################################
# Make gitlab-runner the owner of front directory
# ... needed as later it will be owned by root
#########################################################
# - echo "Setting permissions to gitlab-runner on front directory"
# - echo "$DEPLOY_BACKEND_FOLDER/front"
# - sudo chown -R gitlab-runner $DEPLOY_BACKEND_FOLDER/front
#########################################################
# COPY CODE TO DEPLOY_BACKEND_FOLDER
#########################################################
# make sure the deployment folder exists
- mkdir -p $DEPLOY_BACKEND_FOLDER
# copy all the data from current directory to the deployment directory
- rsync -av --exclude={".git/","*.so"} ./ $DEPLOY_BACKEND_FOLDER/.
# change directory to deployment folder
- cd $DEPLOY_BACKEND_FOLDER
#########################################################
# BACKEND
#########################################################
# spin up dockers if not started yet
# - docker-compose up -d db django behave ws selenoid novnc
# save all environment variables that start with COMETA_ to secret_variables.py
# these variables come from CI/CD variables from gitlab and variables set inside
# gitlab-ci yaml files.
# Commenting below line since from 22-08-2024 secret_variables will be stored in the database
# - env | grep "^COMETA_" | sed -E "s/([A-Z_])=(.*)/\1='\2'/" > backend/secret_variables.py
# Build Selenoid browsers
- echo -e "\e[0Ksection_start:`date +%s`:deploy_selenoid\r\e[0KRunning selenoid deployment script"
- bash backend/selenoid/deploy_selenoid.sh
# - docker exec -it cometa_selenoid sh /etc/selenoid/check_for_custom_background.sh
- echo -e "\e[0Ksection_end:`date +%s`:deploy_selenoid\r\e[0K"
# Install new python packages in django and behave container
- docker exec cometa_django poetry install
- docker exec cometa_behave poetry install
# Detect & Install Django migration
- docker exec cometa_django python manage.py makemigrations
- docker exec cometa_django python manage.py makemigrations security
- docker exec cometa_django python manage.py makemigrations housekeeping
- docker exec cometa_django python manage.py makemigrations configuration
- docker exec cometa_django python manage.py migrate
# Update steps in database
- echo -e "\e[0Ksection_start:`date +%s`:parse_actions\r\e[0KParsing actions to update database"
- docker exec cometa_django bash -c "curl http://localhost:8000/parseActions/ --silent --retry 5 --retry-delay 5"
- echo -e "\e[0Ksection_end:`date +%s`:parse_actions\r\e[0K"
# Update selenoid browsers in database
- echo -e "\e[0Ksection_start:`date +%s`:parse_browsers\r\e[0KParsing selenoid browsers to update database"
- docker exec cometa_django bash -c "curl http://localhost:8000/parseBrowsers/ --silent --retry 5 --retry-delay 5"
- echo -e "\e[0Ksection_end:`date +%s`:parse_browsers\r\e[0K"
#########################################################
# FRONTEND
#########################################################
# start container or if already started then compile else start from zero
- OLD_FRONT_IMAGE_TAG=$(docker inspect cometa_front | jq -r '.[0].Config.Image')
- echo "Old cometa_front image version is $OLD_FONT_IMAGE_TAG"
- echo "Fetching new version from config.yaml file"
- BUILD_VERSION=$(jq -r '.version' "$CONFIG_FILE") # Extract versions
- echo "Pull the cometa_front with version-> $BUILD_VERSION"
# Check if OLD_FRONT_IMAGE_TAG is not equal to BUILD_VERSION or if cometa_front container is not running
- FRONT_CONTAINER_STATUS=$(docker inspect -f '{{.State.Status}}' cometa_front)
- echo "$OLD_FRONT_IMAGE_TAG container status is $FRONT_CONTAINER_STATUS"
- if [ "$OLD_FRONT_IMAGE_TAG" != "$COMETA_FRONT:$BUILD_VERSION" ] || [ "$FRONT_CONTAINER_STATUS" != "running" ]; then
docker pull $COMETA_FRONT:$BUILD_VERSION;
echo "Replace 'cometa/front:latest' with $COMETA_FRONT:$BUILD_VERSION in the docker-compose.yml file";
sed -i "s#cometa/front:latest#"$COMETA_FRONT":"$BUILD_VERSION"#" docker-compose.yml;
echo "Recrating cometa_front";
docker-compose up --force-recreate apache -d;
for FILE in ${COMETA_REPLACE_FAVICON_IN}; do docker exec cometa_front sed -i 's/@@BRANCH@@/'$CI_COMMIT_BRANCH'/g' $FILE; done
else
echo "Front container is up to date, will not do deployment";
fi
- if [ "$OLD_FRONT_IMAGE_TAG" != "$COMETA_FRONT:$BUILD_VERSION" ]; then
echo "Removing old $OLD_FRONT_IMAGE_TAG image from docker";
docker rmi $OLD_FRONT_IMAGE_TAG;
fi
# - echo "Checking if cometa_front is already running else start it..."
# - echo "If there are changes to docker-compose file it will recreate the container..."
# - docker-compose up -d apache 2>&1 | grep -E "up-to-date|cometa_front.*?Running" && docker exec cometa_front bash -c "./start.sh $script_options" || ( timeout 360 docker-compose logs -f --tail=0 apache || true )
#########################################################
# RELOAD GUNICORNS
#########################################################
# Reload project files in gunicorn
- echo -e "\e[0Ksection_start:`date +%s`:reloading_gunicorn\r\e[0KPreparing to reload project in gunicorn"
- echo -e "Now reloading gunicorn for django"
- docker exec cometa_django fuser -k -HUP 8000/tcp
- echo -e "Now reloading gunicorn for behave"
- docker exec cometa_behave fuser -k -HUP 8001/tcp
- docker exec cometa_behave bash -c "supervisorctl restart django-rq:"
- echo -e "\e[0Ksection_end:`date +%s`:reloading_gunicorn\r\e[0K"
# create a deploy job for amvara2 for branch stage
deploy:amvara2.stage:
<<: *deployment_script
extends: .amvara2.stage
# create a deploy job for amvara3 for branch prod
deploy:amvara3.prod:
<<: *deployment_script
extends: .amvara3.prod
# create a deploy job for client01 for branch prod
deploy:client01.int:
<<: *deployment_script
extends: .client01.int
# create a deploy job for client01 for branch prod
deploy:client01.prod:
<<: *deployment_script
extends: .client01.prod
# create a deploy job for client02 for branch prod
# deploy:client02.prod:
# <<: *deployment_script
# extends: .client02.prod
# create a deploy job for client04 for branch prod
deploy:client04.prod:
<<: *deployment_script
extends: .client04.prod
# create a deploy job for client04 for branch build
build:amvara6.build:
stage: build
script:
#########################################################
# FRONTEND
########################################################
- |
# Fetch the JSON response from Docker Hub
RESPONSE=$(curl -s "https://hub.docker.com/v2/repositories/cometa/front/tags/?page_size=2&ordering=last_updated")
# Extract the latest tag using jq
LATEST_TAG=$(echo "$RESPONSE" | jq -r '.results[1].name')
echo "Latest tag from Docker Hub is: $LATEST_TAG"
# Compare the latest tag with the build version
if [ "$LATEST_TAG" == "$BUILD_VERSION" ]; then
echo "The latest version is already $BUILD_VERSION. Skipping build and push."
else
cd front
docker build . -f Dockerfile_angular_build -t $COMETA_FRONT:$BUILD_VERSION
echo "$COMETA_FRONT:$BUILD_VERSION build successfully"
echo "Pushing $COMETA_FRONT:$BUILD_VERSION image to Docker $DOCKER_REGISTRY"
docker push $COMETA_FRONT:$BUILD_VERSION
echo "$COMETA_FRONT:$BUILD_VERSION image pushed"
echo "Converting $COMETA_FRONT:$BUILD_VERSION tag $COMETA_FRONT:latest"
docker tag $COMETA_FRONT:$BUILD_VERSION $COMETA_FRONT:latest
echo "Pushing $COMETA_FRONT:latest image to Docker $DOCKER_REGISTRY"
docker push $COMETA_FRONT:latest
echo "$COMETA_FRONT:latest image pushed"
cd -
fi
extends: .amvara6.build