-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.gitlab-ci.yml
200 lines (175 loc) · 4.67 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
stages:
- build
- test
- prepare
- deploy
- rollback
default:
image: jellybellydev/ci-cd-theory-into-practice:latest
## Template filter
.branch_filter: &branch_filter
only:
- branches
.master_filter: &master_filter
only:
- master
.tags_filter: &tags_filter
only:
- tags
## Stage Build
install-deps:
stage: build
<<: *branch_filter
script:
- composer install
artifacts:
expire_in: 3 hours
paths:
- bin/**
- config/**
- public/**
- src/**
- templates/**
- tests/**
- var/**
- vendor/**
- .env
- .env.test
- .php_cs.dist
- composer.json
- composer.lock
- psalm.xml
- symfony.lock
## Stage Tests
php-cs-fixer:
stage: test
<<: *branch_filter
dependencies:
- install-deps
script:
- vendor/bin/php-cs-fixer fix --verbose --diff --dry-run
lint-yaml-config:
stage: test
<<: *branch_filter
dependencies:
- install-deps
script:
- bin/console lint:yaml config
phpunit:
stage: test
<<: *branch_filter
dependencies:
- install-deps
script:
- XDEBUG_MODE=coverage bin/phpunit --colors=never --coverage-text
psalm:
stage: test
<<: *branch_filter
dependencies:
- install-deps
script:
- vendor/bin/psalm
## Template to prepare artifact deployable
.prepare_template: &prepare_template
script:
- composer install --no-dev --optimize-autoloader
- composer dump-autoload --no-dev --classmap-authoritative
- composer dump-env
- echo "export CI_JOB_ID_ARTIFACTS=\"$(echo "$CI_JOB_ID")\"" > gitlab_variables
artifacts:
expire_in: 3 hours
paths:
- bin/**
- config/**
- public/**
- src/**
- vendor/**
- .env
- .env.local
- .env.local.php
- composer.json
- gitlab_variables
## Stage Prepare
prepare:staging:
stage: prepare
<<: *prepare_template
<<: *master_filter
before_script:
- echo "APP_ENV=staging" >> .env.local
prepare:production:
stage: prepare
<<: *prepare_template
<<: *tags_filter
before_script:
- echo "APP_ENV=prod" >> .env.local
## Template to deploy artifact
.deploy_template: &deploy_template
image: spy86/ansible:latest
before_script:
## Create the SSH directory and give it the right permissions
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
## Install ssh-agent if not already installed, it is required by Docker.
- 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
## Run ssh-agent (inside the build environment)
- eval $(ssh-agent -s)
## Add the SSH key stored in DEPLOY_PRIVATE_KEY variable to the agent store
- echo "$DEPLOY_PRIVATE_KEY" | tr -d '\r' | ssh-add -
## Verifying the SSH host keys
- ssh-keyscan -H $HOST_FOR_SSH_KEYSCAN >> ~/.ssh/known_hosts
- if [[ -f gitlab_variables ]]; then source gitlab_variables; fi
- cd ansible-deploy
- 'echo "gitlab_access_token: \"${GITLAB_ACCESS_TOKEN_API}\"" >> user-vars.yml'
- 'echo "telegram_token: \"${TELEGRAM_TOKEN}\"" >> user-vars.yml'
- 'echo "telegram_chat_id: \"${TELEGRAM_CHAT_ID}\"" >> user-vars.yml'
## Deploy on staging
deploy:staging:
stage: deploy
variables:
HOST_FOR_SSH_KEYSCAN: "161.97.172.133"
DEPLOY_PRIVATE_KEY: $MY_DEPLOY_PRIVATE_KEY
dependencies:
- prepare:staging
<<: *deploy_template
<<: *master_filter
script:
- echo "Deploy of $CI_COMMIT_REF_NAME on staging server"
- ansible-playbook deploy.yml -i inventories/staging -e "app_version=$CI_COMMIT_REF_NAME ci_job_id=$CI_JOB_ID_ARTIFACTS"
rollback:staging:
stage: rollback
variables:
HOST_FOR_SSH_KEYSCAN: "161.97.172.133"
DEPLOY_PRIVATE_KEY: $MY_DEPLOY_PRIVATE_KEY
when: manual
dependencies: []
<<: *deploy_template
<<: *master_filter
script:
- echo "Rollback of $CI_COMMIT_REF_NAME on staging server"
- ansible-playbook rollback.yml -i inventories/staging
## Deploy on production
deploy:production:
stage: deploy
variables:
HOST_FOR_SSH_KEYSCAN: "161.97.172.133"
DEPLOY_PRIVATE_KEY: $MY_DEPLOY_PRIVATE_KEY
when: manual
dependencies:
- prepare:production
<<: *deploy_template
<<: *tags_filter
script:
- echo "Deploy of $CI_COMMIT_REF_NAME on production server"
- ansible-playbook deploy.yml -i inventories/production -e "app_version=$CI_COMMIT_TAG ci_job_id=$CI_JOB_ID_ARTIFACTS"
rollback:production:
stage: rollback
variables:
HOST_FOR_SSH_KEYSCAN: "161.97.172.133"
DEPLOY_PRIVATE_KEY: $MY_DEPLOY_PRIVATE_KEY
when: manual
dependencies: []
<<: *deploy_template
<<: *tags_filter
script:
- echo "Rollback of $CI_COMMIT_TAG on production server"
- ansible-playbook rollback.yml -i inventories/production