-
Notifications
You must be signed in to change notification settings - Fork 1
/
.gitlab-ci.yml
77 lines (71 loc) · 2.04 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
image: docker:27.1.1
stages:
- build
- push
- deploy
default:
before_script:
- echo -n $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
services:
- docker:27.1.1-dind
build:
stage: build
variables:
DOCKER_DRIVER: overlay2
rules:
- if: $CI_COMMIT_BRANCH == 'main'
environment:
name: production
script:
- docker pull $CI_REGISTRY_IMAGE:compile-stage || true
- docker pull $CI_REGISTRY_IMAGE:latest || true
- >
docker build
--target compile-image
--cache-from $CI_REGISTRY_IMAGE:compile-stage
--tag $CI_REGISTRY_IMAGE:compile-stage
--file ./docker/app/Dockerfile
.
- >
docker build
--target runtime-image
--cache-from $CI_REGISTRY_IMAGE:compile-stage
--cache-from $CI_REGISTRY_IMAGE:latest
--tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
--tag $CI_REGISTRY_IMAGE:latest
--file ./docker/app/Dockerfile
.
- docker push $CI_REGISTRY_IMAGE:compile-stage
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
push:
stage: push
variables:
GIT_STRATEGY: none
only:
- main
script:
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
- docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest
- docker push $CI_REGISTRY_IMAGE:latest
deploy:
stage: deploy
image: docker:27.1.1
variables:
DOCKER_DRIVER: overlay2
services:
- docker:27.1.1-dind
rules:
- if: $CI_COMMIT_BRANCH == 'main'
environment:
name: production
before_script:
- echo -n $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
- 'which ssh-agent || (apt-get update -y && apt-get install openssh-client -y)'
- eval $(ssh-agent -s)
- cat "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
script:
- DOCKER_HOST="ssh://$SERVER_USER@$SERVER_HOSTNAME:$SSH_PORT" docker stack deploy --with-registry-auth -c docker-compose.prod.yml $SWARM_NAME