-
Notifications
You must be signed in to change notification settings - Fork 2
/
.gitlab-ci.yml
180 lines (167 loc) · 5.41 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
# YAML objects named with dot is anchors, which are not recognized as jobs
.run_bot: &run_bot
- docker rm -f ${CONTAINER_NAME} || true
- docker pull ${CONTAINER_RELEASE_IMAGE}
# Add new envs here. Don't forget to add them in exmaple.env and docker-compose files.
- docker run
-d
--name ${CONTAINER_NAME}
--restart always
--label traefik.http.routers.${BOT_PROJECT_NAME}.rule="Host(\`${BOT_URL}\`)"
--label traefik.enable=true
--label traefik.http.services.${BOT_PROJECT_NAME}.loadbalancer.server.port="8000"
--log-opt max-size=10m
--log-opt max-file=5
-e POSTGRES_DSN="${POSTGRES_DSN}"
-e REDIS_DSN="${REDIS_DSN}"
-e BOT_CREDENTIALS="${BOT_CREDENTIALS}"
-e DEBUG="${DEBUG:-false}"
$CONTAINER_RELEASE_IMAGE
.create_db: &create_db
- psql -c "create user \"${POSTGRES_USER}\"" postgres || true
- psql -c "alter user \"${POSTGRES_USER}\" with password '${POSTGRES_PASSWORD}'" postgres
- psql -c "create database \"${POSTGRES_DB}\" with owner \"${POSTGRES_USER}\"" postgres || true
.install_dependencies: &install_dependencies
- echo -e "machine ${GIT_HOST}\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
- pip install -q poetry
- poetry config virtualenvs.in-project true
- poetry install
.cache_dependencies: &cache_dependencies
key:
files:
- poetry.lock
prefix: "venv"
paths:
- .cache/pip
- .venv
.postgres_envs: &postgres_envs
- POSTGRES_USER=${CONTAINER_NAME}
- POSTGRES_DB=${CONTAINER_NAME}
- POSTGRES_PASSWORD=$(openssl rand -hex 16)
- POSTGRES_HOST=${PROD_POSTGRES_HOST}
- POSTGRES_DSN=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST/$POSTGRES_DB
# Jobs
variables:
GIT_DEPTH: 1 # Fetch only latest commit
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
stages:
- check
- build
- deploy
default:
interruptible: true
lint:
image: python:3.10
stage: check
tags:
- docker
cache: *cache_dependencies
before_script:
- *install_dependencies
script:
- poetry run ./scripts/lint
test:
image: python:3.10
stage: check
tags:
- docker
services:
- postgres:11.4-alpine
- redis:6.0.1-alpine
cache: *cache_dependencies
variables:
BOT_CREDENTIALS: cts.example.com@secret@123e4567-e89b-12d3-a456-426655440000
POSTGRES_DSN: postgres://postgres:postgres@postgres/postgres
REDIS_DSN: redis://redis/0
before_script:
- *install_dependencies
script:
- poetry run pytest --cov-config=setup.cfg
coverage: '/Total coverage: \d\d\d.\d\d%/'
build:
image: docker:latest
stage: build
tags:
- docker
before_script:
- docker info
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
- CONTAINER_RELEASE_IMAGE="$CI_REGISTRY_IMAGE:${CI_COMMIT_TAG:-$CI_COMMIT_REF_SLUG}"
script:
- docker pull $CONTAINER_RELEASE_IMAGE || true
- docker build
--cache-from $CONTAINER_RELEASE_IMAGE
--build-arg GIT_HOST=$GIT_HOST
--build-arg CI_JOB_TOKEN=$CI_JOB_TOKEN
--build-arg CI_COMMIT_SHA=$CI_COMMIT_SHA
--force-rm
-t $CONTAINER_RELEASE_IMAGE .
- docker push $CONTAINER_RELEASE_IMAGE
- docker rmi $CONTAINER_RELEASE_IMAGE
deploy.botstest:
image: docker:latest
stage: deploy
tags:
- bots-test
only:
- branches
when: manual
environment:
name: test
on_stop: deploy.botstest.stop
variables:
# https://docs.gitlab.com/ee/ci/runners/configure_runners.html#git-strategy
GIT_STRATEGY: none
before_script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
- if [ -z ${BOT_PROJECT_NAME:-} ]; then BOT_PROJECT_NAME=${CI_PROJECT_PATH_SLUG#"$CI_PROJECT_NAMESPACE-"}; fi
- CONTAINER_NAME=$BOT_PROJECT_NAME-`echo $CI_COMMIT_REF_NAME | sed 's/^.*\///'`
- CONTAINER_RELEASE_IMAGE="$CI_REGISTRY_IMAGE:${CI_COMMIT_TAG:-$CI_COMMIT_REF_SLUG}"
- BOT_URL="${BOT_PROJECT_NAME}.${DEV_SERVER_HOST}"
- BOT_CREDENTIALS=$DEV_BOT_CREDENTIALS
- *postgres_envs
- REDIS_DSN=redis://${PROD_REDIS_HOST}/1
script:
- echo "Use URL 'https://${BOT_URL}/' in your cts admin site"
- echo "Using credentials ${BOT_CREDENTIALS}"
- echo "Deploing Docker container ${CONTAINER_NAME}"
- *create_db
- *run_bot
deploy.botstest.stop:
when: manual
environment:
name: test
action: stop
extends: deploy.botstest
script:
- docker rm -f ${CONTAINER_NAME} || true
- psql -c "drop database \"${POSTGRES_DB}\"" postgres || true
- psql -c "drop user \"${POSTGRES_USER}\"" postgres || true
deploy.botsprod:
stage: deploy
image: docker:latest
tags:
- bots-prod
only:
# Note the bots-prod worker requires branch to be protected
- master
when: manual
environment:
name: production
variables:
# https://docs.gitlab.com/ee/ci/runners/configure_runners.html#git-strategy
GIT_STRATEGY: none
before_script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
- if [ -z ${BOT_PROJECT_NAME:-} ]; then BOT_PROJECT_NAME=${CI_PROJECT_PATH_SLUG#"$CI_PROJECT_NAMESPACE-"}; fi
- CONTAINER_NAME=$BOT_PROJECT_NAME
- CONTAINER_RELEASE_IMAGE="$CI_REGISTRY_IMAGE:${CI_COMMIT_TAG:-$CI_COMMIT_REF_SLUG}"
- BOT_URL="${BOT_PROJECT_NAME}.${PROD_SERVER_HOST}"
- *postgres_envs
- REDIS_DSN=redis://${PROD_REDIS_HOST}/1
script:
- echo "Use URL 'https://${BOT_URL}/' in your cts admin site"
- echo "Using credentials ${BOT_CREDENTIALS}"
- echo "Deploing Docker container ${CONTAINER_NAME}"
- *create_db
- *run_bot