This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
.gitlab-ci.yml
147 lines (135 loc) · 4.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
image: "ueberdosis/build-tools"
variables:
BRANCH_NAME: $CI_COMMIT_REF_SLUG
IMAGE: $CI_REGISTRY/$CI_PROJECT_PATH
COMMIT_HASH: $CI_COMMIT_SHA
DOCKER_BUILDKIT: 1
stages:
- build
- test
- release
- deploy
build_nginx:
stage: build
script:
- docker image build -f docker/nginx/Dockerfile -t $IMAGE/nginx:$COMMIT_HASH .
- docker image save -o image-nginx.tar $IMAGE/nginx:$COMMIT_HASH
artifacts:
expire_in: "1800"
paths:
- image-*.tar
build_php:
stage: build
script:
- docker image build -f docker/php/Dockerfile -t $IMAGE/php:$COMMIT_HASH .
- docker image save -o image-php.tar $IMAGE/php:$COMMIT_HASH
artifacts:
expire_in: "1800"
paths:
- image-*.tar
build_mysql:
stage: build
script:
- docker image build -f docker/mysql/Dockerfile -t $IMAGE/mysql:$COMMIT_HASH .
- docker image save -o image-mysql.tar $IMAGE/mysql:$COMMIT_HASH
artifacts:
expire_in: "1800"
paths:
- image-*.tar
.phpunit:
stage: test
variables:
COMPOSE_FILE: "docker-compose.test.yml"
COMPOSE_PROJECT_NAME: "test-$COMMIT_HASH"
before_script:
- docker image load -i image-nginx.tar
- docker image load -i image-php.tar
- docker image load -i image-mysql.tar
script:
- docker-compose pull redis
- docker-compose up -d
- sleep 10
- docker-compose exec -T php sh -c "wait-for --timeout=240 mysql_test:3306"
- docker-compose exec -T php php artisan migrate:fresh --env=testing --seed --quiet
- docker-compose exec -T php phpunit
after_script:
- docker-compose ps
- docker-compose logs nginx php mysql mysql_test redis
- docker-compose down
release_nginx:
stage: release
only:
- main
before_script:
- docker image load -i image-nginx.tar
script:
- echo "$CI_BUILD_TOKEN" | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
- docker image tag $IMAGE/nginx:$COMMIT_HASH $IMAGE/nginx:$BRANCH_NAME
- docker image push $IMAGE/nginx:$BRANCH_NAME
release_php:
stage: release
only:
- main
before_script:
- docker image load -i image-php.tar
script:
- echo "$CI_BUILD_TOKEN" | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
- docker image tag $IMAGE/php:$COMMIT_HASH $IMAGE/php:$BRANCH_NAME
- docker image push $IMAGE/php:$BRANCH_NAME
release_mysql:
stage: release
only:
- main
before_script:
- docker image load -i image-mysql.tar
script:
- echo "$CI_BUILD_TOKEN" | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
- docker image tag $IMAGE/mysql:$COMMIT_HASH $IMAGE/mysql:$BRANCH_NAME
- docker image push $IMAGE/mysql:$BRANCH_NAME
.deploy_template: &deploy_definition
stage: deploy
before_script:
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh && chmod 700 ~/.ssh
- echo "$SSH_FINGERPRINT" > ~/.ssh/known_hosts && chmod 644 ~/.ssh/known_hosts
# Create env vars with hash of configs and secrets
- |
export env_vars=( "LARAVEL_ENV" "MYSQL_ROOT_PASSWORD" "MYSQL_DATABASE" "MYSQL_USER" "MYSQL_PASSWORD" )
for env_var in "${env_vars[@]}"; do
echo "${!env_var}" > "$env_var.txt"
export "${env_var}_HASH"="$(sha512sum $env_var.txt | awk "{print $1}" | cut -c1-16)"
done
script:
- echo "$CI_BUILD_TOKEN" | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
- docker stack deploy -c $COMPOSE_FILE --with-registry-auth --prune $DOCKER_STACK_NAME
.deploy_dev:
only:
- development
variables:
COMPOSE_FILE: "docker-compose.development.yml"
DOCKER_HOST: "$DEV_DOCKER_HOST"
DOCKER_STACK_NAME: "alldocs"
LARAVEL_ENV: "$DEV_LARAVEL_ENV"
MYSQL_DATABASE: "$DEV_MYSQL_DATABASE"
MYSQL_USER: "$DEV_MYSQL_USER"
MYSQL_PASSWORD: "$DEV_MYSQL_PASSWORD"
MYSQL_ROOT_PASSWORD: "$DEV_MYSQL_ROOT_PASSWORD"
SSH_PRIVATE_KEY: "$DEV_SSH_PRIVATE_KEY"
SSH_FINGERPRINT: "$DEV_SSH_FINGERPRINT"
<<: *deploy_definition
deploy_prod:
only:
- main
variables:
COMPOSE_FILE: "docker-compose.production.yml"
DOCKER_HOST: "$PROD_DOCKER_HOST"
DOCKER_STACK_NAME: "alldocs"
LARAVEL_ENV: "$PROD_LARAVEL_ENV"
MYSQL_DATABASE: "$PROD_MYSQL_DATABASE"
MYSQL_USER: "$PROD_MYSQL_USER"
MYSQL_PASSWORD: "$PROD_MYSQL_PASSWORD"
MYSQL_ROOT_PASSWORD: "$PROD_MYSQL_ROOT_PASSWORD"
SSH_PRIVATE_KEY: "$PROD_SSH_PRIVATE_KEY"
SSH_FINGERPRINT: "$PROD_SSH_FINGERPRINT"
<<: *deploy_definition