forked from finmars-platform/finmars-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.gitlab-ci.yml
More file actions
178 lines (160 loc) · 5.46 KB
/
Copy path.gitlab-ci.yml
File metadata and controls
178 lines (160 loc) · 5.46 KB
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
# GitLab CI/CD pipeline for building, testing, and releasing the Finmars Backend Docker image.
# The pipeline consists of 5 stages:
# 1. test - Runs unit tests and generates code coverage reports.
# 2. versioning - Generates a unique version tag for the Docker image.
# 3. build - Builds the Docker image with the generated version tag.
# 4. release - Tags and pushes the Docker image to the registry.
# 5. push_version - Pushes the version information to the license server.
stages:
- test
- versioning
- build
- release
- push_version
default:
image: docker:27
services:
- name: docker:27-dind
alias: docker
command: [ "--host=tcp://0.0.0.0:2375", "--host=unix:///var/run/docker.sock", "--tls=false" ]
before_script:
- docker info
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ""
# Required to initialize postgres & rabbitmq services for testing
POSTGRES_DB: test_finmars
POSTGRES_USER: test_finmars_user
POSTGRES_PASSWORD: test_finmars_password
RABBITMQ_DEFAULT_USER: test_finmars_user
RABBITMQ_DEFAULT_PASS: test_finmars_password
Test:
stage: test
image:
python:3.12-slim-bookworm
services:
- postgres:15-alpine
- rabbitmq:3.13-management-alpine
- redis:7-alpine
variables:
DB_HOST: postgres
DB_NAME: test_finmars
DB_USER: test_finmars_user
DB_PASSWORD: test_finmars_password
DB_PORT: 5432
REPLICA_DB_HOST: postgres
REPLICA_DB_NAME: test_finmars
REPLICA_DB_USER: test_finmars_user
REPLICA_DB_PASSWORD: test_finmars_password
REPLICA_DB_PORT: 5432
RABBITMQ_HOST: rabbitmq
RABBITMQ_USER: test_finmars_user
RABBITMQ_PASSWORD: test_finmars_password
RABBITMQ_PORT: 5672
REDIS_HOST: redis
REDIS_PORT: 6379
before_script:
- pip install --upgrade pip && pip install -r requirements-test.txt
- mkdir -p /var/log/finmars/backend
script:
- coverage run --parallel-mode manage.py test --parallel auto
- coverage combine && coverage report && coverage html
artifacts:
paths:
- ./coverage_html_report
expire_in: 1 day
Versioning:
stage: versioning
image:
python:3.12-slim-bookworm
before_script:
- pip install haikunator
script:
- |
if [[ $CI_COMMIT_REF_NAME == *"-rc" || $CI_COMMIT_REF_NAME == *"-stable" ]]; then
FANCY_NAME=$(python -c 'from haikunator import Haikunator; print(Haikunator().haikunate(token_length=0))')
FULL_VERSION_NAME="$CI_COMMIT_REF_NAME-$CI_PIPELINE_ID-$FANCY_NAME"
echo "FULL_VERSION_NAME=$FULL_VERSION_NAME"
echo "IMAGE_TAG=$(echo $FULL_VERSION_NAME)" >> build.env
fi
artifacts:
reports:
dotenv: build.env
only:
- /^.*-(rc|stable)$/
Push Version:
stage: push_version
image:
debian:bookworm-slim
before_script:
- apt-get update && apt-get install -y curl git
script:
- |
if [[ "$CI_COMMIT_REF_NAME" == *"-rc" ]]; then
CHANNEL="rc"
elif [[ "$CI_COMMIT_REF_NAME" == *"-stable" ]]; then
CHANNEL="stable"
else
CHANNEL="unknown"
fi
# Create JSON payload
NOTES=$(git log -1 --pretty=%f)
JSON_PAYLOAD="{ \"app\": \"$CI_PROJECT_NAME\", \"version\": \"$IMAGE_TAG\", \"build_number\": $CI_PIPELINE_ID, \"notes\": \"$NOTES\", \"channel\": \"$CHANNEL\" }"
echo "JSON_PAYLOAD=$JSON_PAYLOAD"
# Push JSON to license service
status_code=$(curl -o /dev/null -s -w "%{http_code}" -X POST https://license.finmars.com/api/v1/version/ -H "Content-Type: application/json" -d "$JSON_PAYLOAD")
if [[ "$status_code" -ne "201" ]]; then
echo "Push version failed, http status: $status_code"
exit 6
fi
echo "Push version succeeded."
only:
- /^.*-(rc|stable)$/
- tags
Build:
stage: build
before_script:
# to debug runner
# - df
# - cat /etc/resolv.conf
# - cat /etc/hosts
- echo -n $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin git.finmars.com:5050
script:
- IMAGE_TAG=${IMAGE_TAG:-$CI_COMMIT_REF_NAME}
- docker build --pull --no-cache --tag $CI_REGISTRY_IMAGE:$IMAGE_TAG .
- docker push $CI_REGISTRY_IMAGE:$IMAGE_TAG
only:
- /^.*-(rc|stable)$/
Release latest:
stage: release
variables:
# We are just playing with Docker here.
# We do not need GitLab to clone the source code.
GIT_STRATEGY: none
only:
# Only "master" should be tagged "latest"
- master
before_script:
- echo -n $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin git.finmars.com:5050
script:
# Because we have no guarantee that this job will be picked up by the same runner
# that built the image in the previous step, we pull it again locally
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
# Then we tag it "latest"
- docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME $CI_REGISTRY_IMAGE:latest
# And we push it.
- docker push $CI_REGISTRY_IMAGE:latest
Release version:
stage: release
variables:
# Again, we do not need the source code here. Just playing with Docker.
GIT_STRATEGY: none
only:
# We want this job to be run on tags only.
- tags
before_script:
- echo -n $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin git.finmars.com:5050
script:
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
- docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME