forked from covex-nn/docker-workflow-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
214 lines (196 loc) · 6 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
image: docker:latest
variables:
CI_APP_IMAGE_WITH_TAG: "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"
CI_NGINX_IMAGE_WITH_TAG: "$CI_REGISTRY_IMAGE/nginx:$CI_COMMIT_REF_NAME"
stages:
- deps
- test
- build
- deploy
.template-suffix-vars: &suffix-vars
before_script:
- cat .env | grep ENV_ > .build-env
- sed -i 's/^/export /' .build-env
- for name in `env | awk -F= '{if($1 ~ /'"$ENV_SUFFIX"'$/) print $1}'`; do
echo 'export '`echo $name|awk -F''"$ENV_SUFFIX"'$' '{print $1}'`'='`printenv $name`'' >> .build-env;
done
.template-docker-nginx-image: &docker-nginx-image
stage: build
image: docker:latest
<<: *suffix-vars
script:
- eval $(cat .build-env)
- docker build --tag $CI_NGINX_IMAGE_WITH_TAG --build-arg server_name=$SERVER_NAME --build-arg server_upstream=prod --build-arg app_php=app ./docker/nginx
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
- docker push $CI_NGINX_IMAGE_WITH_TAG
- docker logout $CI_REGISTRY
tags:
- executor-docker
- docker-in-docker
.template-docker-app-image: &docker-app-image
stage: build
image: docker:latest
<<: *suffix-vars
script:
- eval $(cat .build-env)
- echo '{"github-oauth":{"github.com":"'"$COMPOSER_GITHUB_TOKEN"'"}}' > ./auth.json
- docker build --tag $CI_APP_IMAGE_WITH_TAG .
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
- docker push $CI_APP_IMAGE_WITH_TAG
- docker logout $CI_REGISTRY
tags:
- executor-docker
- docker-in-docker
.template-docker-compose: &docker-compose
stage: build
image: covex/docker-compose:1.0
<<: *suffix-vars
script:
- eval $(cat .build-env)
- mkdir build
- docker-compose -f docker-compose-deploy.yml config > build/docker-compose.yml
- sed -i 's/\/builds\/'"$CI_PROJECT_NAMESPACE"'\/'"$CI_PROJECT_NAME"'/\./g' build/docker-compose.yml
artifacts:
untracked: true
name: "$CI_COMMIT_REF_NAME"
paths:
- build/
tags:
- executor-docker
dependencies: []
.template-secure-copy: &secure-copy
stage: deploy
image: covex/alpine-git:1.0
before_script:
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
script:
- eval $(cat .build-env)
- ssh -p 22 $DEPLOY_USER@$DEPLOY_HOST 'set -e ;
rm -rf '"$DEPLOY_DIRECTORY"'_tmp ;
mkdir -p '"$DEPLOY_DIRECTORY"'_tmp'
- scp -P 22 -r build/* ''"$DEPLOY_USER"'@'"$DEPLOY_HOST"':'"$DEPLOY_DIRECTORY"'_tmp'
- ssh -p 22 $DEPLOY_USER@$DEPLOY_HOST 'set -e ;
if [ -d '"$DEPLOY_DIRECTORY"' ]; then rm -rf '"$DEPLOY_DIRECTORY"'; fi ;
mv '"$DEPLOY_DIRECTORY"'_tmp '"$DEPLOY_DIRECTORY"' ;
cd '"$DEPLOY_DIRECTORY"' ;
docker login -u gitlab-ci-token -p '"$CI_JOB_TOKEN"' '"$CI_REGISTRY"' ;
docker-compose pull ;
docker-compose up -d --no-recreate ;
docker-compose up -d --force-recreate --no-deps spare ;
docker-compose exec -T spare sh -c "cd /srv && rm -rf b/* && cp -a web/. b/ && rm -rf a/* && cp -a web/. a/" ;
docker-compose exec -T spare phing storage-prepare database-deploy ;
docker-compose up -d --force-recreate --no-deps php ;
docker-compose exec -T php crond -b'
- ssh -p 22 $DEPLOY_USER@$DEPLOY_HOST 'set -e ;
cd '"$DEPLOY_DIRECTORY"' ;
echo "[$(date -R)] web-server is down" ;
docker-compose stop nginx ;
docker-compose up -d nginx ;
echo "[$(date -R)] web-server is up"'
tags:
- executor-docker
deps:php-composer:
stage: deps
image: covex/php7.1-fpm:1.0
script:
- echo '{"github-oauth":{"github.com":"'"$COMPOSER_GITHUB_TOKEN"'"}}' > ./auth.json
- composer install --prefer-dist --no-scripts --no-autoloader --no-interaction
tags:
- executor-docker
test:phpunit:
stage: test
image: covex/php7.1-fpm:1.0
<<: *suffix-vars
variables:
ENV_SUFFIX: "_TEST"
script:
- eval $(cat .build-env)
- echo '{"github-oauth":{"github.com":"'"$COMPOSER_GITHUB_TOKEN"'"}}' > ./auth.json
- composer require phpunit/phpunit:* --dev
- phpunit
tags:
- executor-docker
build:docker-nginx-image-master:
<<: *docker-nginx-image
variables:
ENV_SUFFIX: "_MASTER"
only:
- master
except:
- tags
build:docker-nginx-image-production:
<<: *docker-nginx-image
variables:
ENV_SUFFIX: "_PRODUCTION"
only:
- /^release-.*$/
except:
- branches
build:docker-app-image-master:
<<: *docker-app-image
variables:
ENV_SUFFIX: "_MASTER"
only:
- master
except:
- tags
build:docker-app-image-production:
<<: *docker-app-image
variables:
ENV_SUFFIX: "_PRODUCTION"
only:
- /^release-.*$/
except:
- branches
build:docker-compose-master:
<<: *docker-compose
variables:
ENV_SUFFIX: "_MASTER"
only:
- master
except:
- tags
build:docker-compose-production:
<<: *docker-compose
variables:
ENV_SUFFIX: "_PRODUCTION"
only:
- /^release-.*$/
except:
- branches
deploy:secure-copy-master:
<<: *secure-copy
only:
- master
except:
- tags
environment:
name: staging
dependencies:
- build:docker-compose-master
deploy:secure-copy-production:
<<: *secure-copy
only:
- /^release-.*$/
except:
- branches
environment:
name: production
dependencies:
- build:docker-compose-production
deploy:sync-project-forks:
stage: deploy
image: covex/alpine-git:1.0
before_script:
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
script:
- git branch -d stable || true
- git checkout -b stable
- for FORK in $PROJECT_FORKS; do git remote add fork $FORK && git push fork stable && git remote rm fork; done
only:
- master
dependencies: []
tags:
- executor-docker