-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
103 lines (88 loc) · 2.71 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
image: docker:latest
variables:
APPLICATION_URL: "https://${APPLICATION_HOST}"
DOCKER_TLS_CERTDIR: "/certs"
BUILDER_IMAGE_REF: "${CI_REGISTRY_IMAGE}/builder:${CI_COMMIT_SHORT_SHA}"
BUILDER_IMAGE_TAG: "${CI_REGISTRY_IMAGE}/builder:${CI_COMMIT_REF_SLUG}"
DOCKER_IMAGE_REF: "${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHORT_SHA}"
DOCKER_IMAGE_TAG: "${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_SLUG}"
DOCKER_IMAGE_LATEST: "${CI_REGISTRY_IMAGE}:latest"
services:
- docker:dind
stages:
- build
- release
- deploy
before_script:
- docker login "${CI_REGISTRY}" -u "${CI_REGISTRY_USER}" -p "${CI_REGISTRY_PASSWORD}"
build:
stage: build
script:
- |-
if docker pull "${BUILDER_IMAGE_REF}" &> /dev/null
then
docker tag "${BUILDER_IMAGE_REF}" "${BUILDER_IMAGE_TAG}"
else
docker pull "${BUILDER_IMAGE_TAG}" &> /dev/null || true
fi
- docker build --target "builder"
--cache-from "${BUILDER_IMAGE_TAG}"
--tag "${BUILDER_IMAGE_REF}"
--tag "${BUILDER_IMAGE_TAG}"
--build-arg BACKEND_URL="${APPLICATION_URL}"
--build-arg HCAPTCHA_SITE_KEY="${HCAPTCHA_SITE_KEY}"
.
- docker push "${BUILDER_IMAGE_REF}"
- docker push "${BUILDER_IMAGE_TAG}"
- |-
if docker pull "${DOCKER_IMAGE_REF}" &> /dev/null
then
docker tag "${DOCKER_IMAGE_REF}" "${DOCKER_IMAGE_TAG}"
else
docker pull "${DOCKER_IMAGE_TAG}" &> /dev/null || true
fi
- docker build
--cache-from "${BUILDER_IMAGE_TAG}"
--cache-from "${DOCKER_IMAGE_TAG}"
--tag "${DOCKER_IMAGE_REF}"
--tag "${DOCKER_IMAGE_TAG}"
--build-arg BACKEND_URL="${APPLICATION_URL}"
--build-arg HCAPTCHA_SITE_KEY="${HCAPTCHA_SITE_KEY}"
--build-arg AUTHOR="${GITLAB_USER_NAME}"
--build-arg COMMIT_REF="${CI_COMMIT_REF}"
--build-arg COMMIT_SHA="${CI_COMMIT_SHA}"
--build-arg CREATE_DATE="${CI_PIPELINE_CREATED_AT}"
.
- docker push "${DOCKER_IMAGE_REF}"
- docker push "${DOCKER_IMAGE_TAG}"
rules:
- exists:
- Dockerfile
- if: '"${CI_COMMIT_BRANCH}" =~ /^(master|production)$/'
when: on_success
- when: manual
release:
stage: release
needs:
- build
script:
- docker pull "${DOCKER_IMAGE_REF}"
- docker tag "${DOCKER_IMAGE_REF}" "${DOCKER_IMAGE_LATEST}"
- docker push "${DOCKER_IMAGE_LATEST}"
only:
- production
deploy:
stage: deploy
image: "${ANSIBLE_IMAGE}"
needs:
- release
before_script:
- chmod 400 "${DEPLOY_KEY}"
script:
- ansible-playbook ansible.yml
--extra-vars "project_dir=${PROJECT_DIR}"
--inventory "${ANSIBLE_HOSTS}"
--key-file "${DEPLOY_KEY}"
--limit "${DEPLOY_SERVER}"
only:
- production