-
Notifications
You must be signed in to change notification settings - Fork 6
/
.gitlab-ci.yml
248 lines (233 loc) · 7.77 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
---
# -*- coding: utf-8 -*-
image: docker:latest
variables:
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: "/certs"
DOCKER_IMAGE_TAG: $CI_PIPELINE_ID
DOCKER_BUILDKIT: 1
COMPOSE_DOCKER_CLI_BUILD: 1
services:
- docker:dind
cache:
key: ${CI_COMMIT_REF_SLUG}
stages:
- build
- deploy
- publish
before_script:
- |-
if [ $CI_COMMIT_BRANCH == "main" ] ; then
TAG="main"
REGISTRY=$CI_REGISTRY/$CI_PROJECT_PATH
docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
elif [ $CI_COMMIT_BRANCH == "preprod" ] ; then
TAG="preprod"
REGISTRY=$CI_REGISTRY/$CI_PROJECT_PATH
docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
elif [ $CI_COMMIT_BRANCH == "prod" ] ; then
TAG="prod"
REGISTRY=$CI_REGISTRY/$CI_PROJECT_NAME
docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
else
TAG=$CI_COMMIT_REF_NAME
REGISTRY=$CI_REGISTRY/$CI_PROJECT_PATH
docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
fi
echo $TAG
app_build:
stage: build
cache:
key: ${CI_COMMIT_REF_SLUG}
variables:
IMG: app
script:
- sed -i "s|agentj_version|$TAG|g" $CI_PROJECT_DIR/$IMG/docker/files/.env
- cd $CI_PROJECT_DIR/$IMG && docker build
--target base
--build-arg BUILDKIT_INLINE_CACHE=1
-t $REGISTRY/agentj_$IMG:base .
- cd $CI_PROJECT_DIR/$IMG && docker build
--cache-from $REGISTRY/agentj_$IMG:$TAG
--build-arg BUILDKIT_INLINE_CACHE=1
-t $REGISTRY/agentj_$IMG:$TAG .
- docker push $REGISTRY/agentj_$IMG:$TAG
only:
changes:
- app/*
- app/**/*
amavis_build:
stage: build
variables:
IMG: amavis
script:
- cd $CI_PROJECT_DIR/$IMG && docker build
--cache-from $REGISTRY/agentj_$IMG:$TAG
--build-arg BUILDKIT_INLINE_CACHE=1
-t $REGISTRY/agentj_$IMG:$TAG .
- docker push $REGISTRY/agentj_$IMG:$TAG
only:
changes:
- amavis/*
- amavis/**/*
relay_build:
stage: build
variables:
IMG: relay
script:
- cd $CI_PROJECT_DIR/$IMG && docker build
--cache-from $REGISTRY/agentj_$IMG:$TAG
--build-arg BUILDKIT_INLINE_CACHE=1
-t $REGISTRY/agentj_$IMG:$TAG .
- docker push $REGISTRY/agentj_$IMG:$TAG
only:
changes:
- relay/*
- relay/**/*
smtp_build:
stage: build
variables:
IMG: smtp
script:
- cd $CI_PROJECT_DIR/$IMG && docker build
--cache-from $REGISTRY/agentj_$IMG:$TAG
--build-arg BUILDKIT_INLINE_CACHE=1
-t $REGISTRY/agentj_$IMG:$TAG .
- docker push $REGISTRY/agentj_$IMG:$TAG
only:
changes:
- smtp/*
- smtp/**/*
syslogng_build:
stage: build
variables:
IMG: syslogng
script:
- cd $CI_PROJECT_DIR/$IMG && docker build
--cache-from $REGISTRY/agentj_$IMG:$TAG
--build-arg BUILDKIT_INLINE_CACHE=1
-t $REGISTRY/agentj_$IMG:$TAG .
- docker push $REGISTRY/agentj_$IMG:$TAG
only:
changes:
- syslogng/*
- syslogng/**/*
# This task is manually triggered only
mass_build:
stage: build
script:
- for DIR in amavis app relay smtp syslogng ;
do cd $CI_PROJECT_DIR/$DIR && docker build
--cache-from $REGISTRY/agentj_$DIR:main
--build-arg BUILDKIT_INLINE_CACHE=1
-t $REGISTRY/agentj_$DIR:main . ;
docker push $REGISTRY/agentj_$DIR:main ;
done
when: manual
deploy-dev:
stage: deploy
cache: {}
environment:
name: dev
variables:
DEPLOY_USER: docker
DEPLOY_DIR: "/home/$DEPLOY_USER/agentj-docker"
TAG: "main"
before_script:
- apk add --no-cache rsync
- echo $TAG
- REGISTRY="$CI_REGISTRY/$CI_PROJECT_PATH"
- echo $REGISTRY
script:
- mkdir ~/.ssh && ssh-keyscan $DEPLOY_DEV >> ~/.ssh/known_hosts && chmod 700 ~/.ssh
- echo "$SSH_CONFIG" > ~/.ssh/config && chmod 400 ~/.ssh/config
- echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa && chmod 400 ~/.ssh/id_rsa
- export DOCKER_HOST="ssh://${DEPLOY_USER:-$USER}@${DEPLOY_DEV:-$1}"
- sed -i "s|probesys38|$REGISTRY|g" docker-compose.yml
- echo $DEPLOY_DIR
- ssh $DEPLOY_USER@$DEPLOY_DEV -C "[ ! -d $DEPLOY_DIR ] && mkdir -p $DEPLOY_DIR || true"
- ssh $DEPLOY_USER@$DEPLOY_DEV -C "docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY"
- rsync -arv --exclude=".env" ./ $DEPLOY_USER@$DEPLOY_DEV:$DEPLOY_DIR/
- ssh $DEPLOY_USER@$DEPLOY_DEV -C "cd $DEPLOY_DIR/ && docker compose down"
- docker image rm $(docker images -q --filter=reference="*/*/*/*agentj*") || true
- docker volume rm agentj-docker_public || true
- ssh $DEPLOY_USER@$DEPLOY_DEV -C "cd $DEPLOY_DIR/ && docker compose up -d"
except:
- merge_requests
only:
- main
deploy-preprod:
stage: deploy
cache: {}
environment:
name: preprod
variables:
DEPLOY_USER: docker
DEPLOY_DIR: "/home/$DEPLOY_USER/agentj-docker"
TAG: "preprod"
before_script:
- apk add --no-cache rsync
- echo $TAG
- REGISTRY="$CI_REGISTRY/$CI_PROJECT_PATH"
- echo $REGISTRY
script:
- mkdir ~/.ssh
- echo $DEPLOY_PREPROD_HOST_SSHD_PUB_KEY
- echo "$DEPLOY_PREPROD_HOST_SSHD_PUB_KEY" >> ~/.ssh/known_hosts
- chmod 700 ~/.ssh
- echo $SSH_CONFIG_PREPROD
- echo "$SSH_CONFIG_PREPROD" > ~/.ssh/config
- chmod 400 ~/.ssh/config
- echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
- chmod 400 ~/.ssh/id_rsa
- echo $DEPLOY_PREPROD_HOST
- export DOCKER_HOST="ssh://${DEPLOY_USER:-$USER}@${DEPLOY_PREPROD_HOST:-$1}"
- echo $DOCKER_HOST
- sed -i "s|probesys38|$REGISTRY|g" docker-compose.yml
- echo $DEPLOY_DIR
- ssh $DEPLOY_USER@$DEPLOY_PREPROD_HOST -C "[ ! -d $DEPLOY_DIR ] && mkdir -p $DEPLOY_DIR || true"
- ssh $DEPLOY_USER@$DEPLOY_PREPROD_HOST -C "docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY"
- rsync -arv --exclude=".env" ./ $DEPLOY_USER@$DEPLOY_PREPROD_HOST:$DEPLOY_DIR/
- ssh $DEPLOY_USER@$DEPLOY_PREPROD_HOST -C "cd $DEPLOY_DIR/ && docker compose down"
- docker image rm $(docker images -q --filter=reference="*/*/*/*agentj*") || true
- docker volume rm agentj-docker_public || true
- ssh $DEPLOY_USER@$DEPLOY_PREPROD_HOST -C "sed -i "s/VERSION=.*/VERSION=$TAG/" /home/docker/agentj-docker/.env"
- ssh $DEPLOY_USER@$DEPLOY_PREPROD_HOST -C "cd $DEPLOY_DIR/ && docker compose up -d"
except:
- merge_requests
only:
- preprod
deploy-tags:
stage: deploy
cache: {}
environment:
name: preprod
url: https://agentj-$CI_ENVIRONMENT_SLUG.$DOMAIN
variables:
DEPLOY_USER: docker
DEPLOY_DIR: "/home/$DEPLOY_USER/agentj-docker"
TAG: $CI_COMMIT_TAG
before_script:
- apk add --no-cache rsync
- echo $TAG
- REGISTRY="$CI_REGISTRY/$CI_PROJECT_PATH"
- echo $REGISTRY
script:
- mkdir ~/.ssh && ssh-keyscan $DEPLOY_PREPROD >> ~/.ssh/known_hosts && chmod 700 ~/.ssh
- echo "$SSH_CONFIG" > ~/.ssh/config && chmod 400 ~/.ssh/config
- echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa && chmod 400 ~/.ssh/id_rsa
- export DOCKER_HOST="ssh://${DEPLOY_USER:-$USER}@${DEPLOY_PREPROD:-$1}"
- sed -i "s|probesys38|$REGISTRY|g" docker-compose.yml
- sed -i "s|agentj_version|$TAG|g" .env.example
- echo $DEPLOY_DIR
- ssh $DEPLOY_USER@$DEPLOY_PREPROD -C "[ ! -d $DEPLOY_DIR ] && mkdir -p $DEPLOY_DIR || true"
- rsync -arv --exclude=".env" ./ $DEPLOY_USER@$DEPLOY_PREPROD:$DEPLOY_DIR/
- ssh $DEPLOY_USER@$DEPLOY_PREPROD -C "cd $DEPLOY_DIR/ && docker compose down"
- docker image rm $(docker images -q --filter=reference="*/*/*/*agentj*") || true
- docker volume rm agentj-docker_public || true
- ssh $DEPLOY_USER@$DEPLOY_PREPROD -C "cd $DEPLOY_DIR/ && docker compose up -d"
only:
- tags
except:
- branches
- merge_requests