-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
.gitlab-ci.yml
97 lines (85 loc) · 2.82 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
image: "php:7.3-fpm-stretch"
stages:
- test
- build
- deploy
variables:
MYSQL_DATABASE: testing
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: testing
IMAGE_TAG_APPLICATION: registry.gitlab.com/guillaumebriday/todolist-backend-laravel/application:${CI_COMMIT_REF_SLUG}
IMAGE_TAG_NGINX: registry.gitlab.com/guillaumebriday/todolist-backend-laravel/nginx:${CI_COMMIT_REF_SLUG}
REGISTRY_URL: registry.gitlab.com
cache:
paths:
- vendor/
.setup_test_env: &setup_test_env
before_script:
# Install node and some other deps
- apt-get update -yqq
- apt-get install zip unzip git libjpeg-dev libpng-dev libfreetype6-dev -yqq --no-install-recommends
- docker-php-ext-install gd pdo_mysql
- curl -sS https://getcomposer.org/installer | php
- php composer.phar install --no-progress --no-interaction --optimize-autoloader
- cp .env.gitlab .env.testing
# Stage: Test
phpunit:
<<: *setup_test_env
services:
- mysql:5.7
stage: test
script:
- vendor/bin/phpunit --testdox
php-cs-fixer:
<<: *setup_test_env
stage: test
script:
- vendor/bin/php-cs-fixer fix --config=.php_cs --verbose --dry-run --diff
# Stage: Build
.setup_build_env: &setup_build_env
stage: build
image: docker:stable
services:
- docker:dind
only:
- master
build_application_image:
<<: *setup_build_env
script:
- docker build -f .cloud/docker/Dockerfile.prod --target application -t ${IMAGE_TAG_APPLICATION} .
- docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
- docker push ${IMAGE_TAG_APPLICATION}
build_nginx_image:
<<: *setup_build_env
script:
- docker build -f .cloud/docker/Dockerfile.prod --target nginx -t ${IMAGE_TAG_NGINX} .
- docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
- docker push ${IMAGE_TAG_NGINX}
# Stage: deploy
.setup_deploy_env: &setup_deploy_env
image: kroniak/ssh-client
before_script:
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan -H '51.15.244.250' >> ~/.ssh/known_hosts
deploy_prod:
<<: *setup_deploy_env
stage: deploy
script:
# log into Docker registry
- ssh root@51.15.244.250 "
docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${REGISTRY_URL} &&
docker pull ${IMAGE_TAG_APPLICATION} &&
docker pull ${IMAGE_TAG_NGINX} &&
docker stop todolist-nginx || true &&
docker stop todolist-server || true &&
docker run --restart unless-stopped --name todolist-server --link todolist-db --env-file /var/www/todolist-backend/.env -d ${IMAGE_TAG_APPLICATION} &&
docker run --restart unless-stopped -d --name todolist-nginx --link todolist-server ${IMAGE_TAG_NGINX}"
only:
- master
when: manual
environment:
name: production
url: https://todolist-api.guillaumebriday.me/