forked from teamdigitale/webanalytics-onboarding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.xml
366 lines (354 loc) · 18.8 KB
/
build.xml
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?xml version="1.0" encoding="UTF-8"?>
<project name="Web Analytics Italia" default="build">
<!-- Loads properties from the environment with the 'env' as prefix -->
<property environment="env"/>
<!-- Property APP_ENV defaults to "local" -->
<if>
<not><isset property="env.APP_ENV"/></not>
<then>
<property name="env.APP_ENV" value="local"/>
</then>
</if>
<!-- Load properties from file -->
<if>
<available file="env/build.properties" type="file" property="ignored"/>
<then>
<property file="env/build.properties"/>
</then>
</if>
<!-- Target: clear -->
<target name="clear" depends="clean">
</target>
<!-- Target: clean -->
<target name="clean">
<echo msg="Cleaning environment..."/>
<!-- Set Docker .env file according to current environment -->
<echo message="Setting containers environment..."/>
<copy file="env/env-containers-${env.APP_ENV}" tofile="containers/.env" overwrite="true"/>
<echo msg="Deleting containers..."/>
<!-- Stopping and deleting running containers -->
<if>
<available file="containers" type="dir" property="ignored"/>
<then>
<echo msg="Stopping Docker containers currently running..."/>
<exec command="docker-compose down -v" dir="containers" passthru="true" checkreturn="true"/>
</then>
</if>
<!-- Delete containers data directory -->
<echo msg="Deleting containers data directory...."/>
<delete dir="containers/data" includeemptydirs="true" quiet="true"/>
<delete dir="containers/application/data" includeemptydirs="true" quiet="true"/>
<!-- Delete testing db file -->
<echo msg="Deleting testing database file...."/>
<delete file="database/testing.sqlite" quiet="true"/>
<!-- Delete containers logs directory -->
<echo msg="Deleting containers logs directory...."/>
<delete quiet="true">
<fileset dir="containers/logs">
<include name="**/*.log"/>
<include name="*.log"/>
<include name="*"/>
</fileset>
</delete>
<!-- Delete node_modules directory -->
<echo msg="Deleting node_modules directory...."/>
<delete dir="node_modules" includeemptydirs="true" quiet="true"/>
<!-- Delete environment files -->
<echo msg="Deleting environment files...."/>
<delete file=".env" quiet="true"/>
<delete file="containers/.env" quiet="true"/>
<delete quiet="true">
<fileset dir=".">
<include name=".env.dusk.*"/>
</fileset>
</delete>
</target>
<!-- Target: clean -->
<target name="clean-deep" depends="clean">
<echo msg="Cleaning environment..."/>
<!-- Set Docker .env file according to current environment -->
<echo message="Setting containers environment..."/>
<copy file="env/env-containers-${env.APP_ENV}" tofile="containers/.env" overwrite="true"/>
<echo msg="Deleting images..."/>
<!-- Stopping and deleting running containers and images -->
<if>
<available file="containers" type="dir" property="ignored"/>
<then>
<echo msg="Stopping Docker containers currently running..."/>
<exec command="docker-compose down --rmi 'local' -v" dir="containers" passthru="true" checkreturn="true"/>
</then>
</if>
</target>
<!-- Target: prepare -->
<target name="prepare">
<echo msg="Preparing ${env.APP_ENV} environment..."/>
<!-- Set Docker .env file according to current environment -->
<echo message="Setting containers environment..."/>
<php function="md5" returnProperty="HASHED_MATOMO_ROOT_PASSWORD">
<param value="${MATOMO_ROOT_PASSWORD}"/>
</php>
<php function="password_hash" returnProperty="HASHED_MATOMO_ROOT_PASSWORD">
<param value="${HASHED_MATOMO_ROOT_PASSWORD}"/>
<param value="1"/>
</php>
<copy file="env/env-containers-${env.APP_ENV}" tofile="containers/.env" overwrite="true">
<filterchain>
<replacetokens>
<token key="MARIADB_DATABASE" value="${MARIADB_DATABASE}" />
<token key="MARIADB_USER" value="${MARIADB_USER}" />
<token key="MARIADB_PASSWORD" value="${MARIADB_PASSWORD}" />
<token key="MARIADB_ROOT_PASSWORD" value="${MARIADB_ROOT_PASSWORD}" />
<token key="MATOMO_ROOT_USER" value="${MATOMO_ROOT_USER}" />
<token key="MATOMO_ROOT_PASSWORD" value="${HASHED_MATOMO_ROOT_PASSWORD}" />
<token key="MATOMO_ROOT_APIKEY" value="${MATOMO_ROOT_APIKEY}" />
</replacetokens>
</filterchain>
</copy>
<!-- Set Laravel .env file according to current environment -->
<echo message="Setting Laravel environment..."/>
<copy file="env/env-laravel-${env.APP_ENV}" tofile=".env" overwrite="true">
<filterchain>
<replacetokens>
<token key="HOSTNAME" value="${HOSTNAME}" />
<token key="MARIADB_DATABASE" value="${MARIADB_DATABASE}" />
<token key="MARIADB_USER" value="${MARIADB_USER}" />
<token key="MARIADB_PASSWORD" value="${MARIADB_PASSWORD}" />
<token key="MAIL_HOST" value="${MAIL_HOST}" />
<token key="MAIL_PORT" value="${MAIL_PORT}" />
<token key="MAIL_USERNAME" value="${MAIL_USERNAME}" />
<token key="MAIL_PASSWORD" value="${MAIL_PASSWORD}" />
<token key="MAIL_ENCRYPTION" value="${MAIL_ENCRYPTION}" />
<token key="PEC_HOST" value="${PEC_HOST}" />
<token key="PEC_PORT" value="${PEC_PORT}" />
<token key="PEC_USERNAME" value="${PEC_USERNAME}" />
<token key="PEC_PASSWORD" value="${PEC_PASSWORD}" />
<token key="PEC_ENCRYPTION" value="${PEC_ENCRYPTION}" />
<token key="SPID_SP_PRIVATE_KEY" value="${SPID_SP_PRIVATE_KEY}" />
<token key="SPID_SP_CERTIFICATE" value="${SPID_SP_CERTIFICATE}" />
<token key="ANALYTICS_ADMIN_TOKEN" value="${MATOMO_ROOT_APIKEY}" />
<token key="ANALYTICS_API_BASE_URL" value="${ANALYTICS_API_BASE_URL}" />
<token key="ANALYTICS_API_SSL_VERIFY" value="${ANALYTICS_API_SSL_VERIFY}" />
<token key="ANALYTICS_PUBLIC_URL" value="${ANALYTICS_PUBLIC_URL}" />
</replacetokens>
</filterchain>
</copy>
<!-- Create a copy of xdebug.ini -->
<echo message="Copying xdebug.ini files..."/>
<copy file="containers/php-fpm/xdebug.template.ini" tofile="containers/php-fpm/xdebug.ini" overwrite="true"/>
<copy file="containers/workspace/xdebug.template.ini" tofile="containers/workspace/xdebug.ini" overwrite="true"/>
<if>
<or>
<equals arg1="${env.APP_ENV}" arg2="local"/>
<equals arg1="${env.APP_ENV}" arg2="testing"/>
</or>
<then>
<!-- Set value of xdebug ide key to configured xdebug ide key -->
<echo message="Setting XDEBUG IDE KEY to ${XDEBUG_IDEKEY} ..."/>
<replaceregexp file="containers/php-fpm/xdebug.ini" match="xdebug.idekey=(\S*)" replace="xdebug.idekey=${XDEBUG_IDEKEY}"/>
<replaceregexp file="containers/workspace/xdebug.ini" match="xdebug.idekey=(\S*)" replace="xdebug.idekey=${XDEBUG_IDEKEY}"/>
<!-- Set XDEBUG remote host -->
<echo message="Setting XDEBUG remote host to ${XDEBUG_REMOTE_HOST} ..."/>
<replaceregexp file="containers/php-fpm/xdebug.ini" match="xdebug.remote_host=(\S*)" replace="xdebug.remote_host=${XDEBUG_REMOTE_HOST}"/>
<replaceregexp file="containers/workspace/xdebug.ini" match="xdebug.remote_host=(\S*)" replace="xdebug.remote_host=${XDEBUG_REMOTE_HOST}"/>
<!-- Set dusk .env file according to current environment -->
<echo msg="Setting Laravel Dusk .env file according to current environment..."/>
<copy file="env/env-dusk-${env.APP_ENV}" tofile=".env.dusk.${env.APP_ENV}" overwrite="true">
<filterchain>
<replacetokens>
<token key="MAIL_HOST" value="${MAIL_HOST}" />
<token key="MAIL_PORT" value="${MAIL_PORT}" />
<token key="MAIL_USERNAME" value="${MAIL_USERNAME}" />
<token key="MAIL_PASSWORD" value="${MAIL_PASSWORD}" />
<token key="MAIL_ENCRYPTION" value="${MAIL_ENCRYPTION}" />
<token key="PEC_HOST" value="${PEC_HOST}" />
<token key="PEC_PORT" value="${PEC_PORT}" />
<token key="PEC_USERNAME" value="${PEC_USERNAME}" />
<token key="PEC_PASSWORD" value="${PEC_PASSWORD}" />
<token key="PEC_ENCRYPTION" value="${PEC_ENCRYPTION}" />
<token key="ANALYTICS_ADMIN_TOKEN" value="${MATOMO_ROOT_APIKEY}" />
<token key="ANALYTICS_API_BASE_URL" value="${ANALYTICS_API_BASE_URL}" />
<token key="ANALYTICS_API_SSL_VERIFY" value="${ANALYTICS_API_SSL_VERIFY}" />
<token key="ANALYTICS_PUBLIC_URL" value="${ANALYTICS_PUBLIC_URL}" />
</replacetokens>
</filterchain>
</copy>
</then>
</if>
<!-- Set a new application key -->
<echo msg="Setting a new application secret key..."/>
<exec command="php artisan key:generate" checkreturn="true"/>
<!-- Install node modules -->
<echo msg="Installing node modules..."/>
<exec command="npm install" passthru="true" checkreturn="true"/>
<!-- Run laravel mix -->
<echo msg="Compiling assets with Laravel Mix..."/>
<if>
<or>
<equals arg1="${env.APP_ENV}" arg2="local"/>
<equals arg1="${env.APP_ENV}" arg2="testing"/>
</or>
<then>
<exec command="npm run dev" passthru="true" checkreturn="true"/>
</then>
<else>
<exec command="npm run prod" passthru="true" checkreturn="true"/>
</else>
</if>
<!--
If in testing environment copy application files in a location for
being included in the application container. This is necessary because
CircleCI doesn't support volume mount from docker host.
-->
<if>
<equals arg1="${env.APP_ENV}" arg2="testing"/>
<then>
<echo msg="Copying application files..."/>
<copy todir="containers/application/data">
<fileset dir=".">
<exclude name="containers/**"/>
</fileset>
</copy>
<copy todir="containers/application/logs">
<fileset dir="containers/logs">
</fileset>
</copy>
<property name="MARIADB_DATABASE" value="default"/>
<property name="MARIADB_ROOT_PASSWORD" value="root"/>
</then>
</if>
</target>
<!-- Target: build (default) -->
<target name="build" depends="prepare, stop">
<echo msg="Building..."/>
<!-- Start containers -->
<echo msg="Starting Docker containers..."/>
<exec command="docker-compose up -d --build nginx mariadb redis mailhog workspace matomo queue-worker" dir="containers" passthru="true" checkreturn="true"/>
<!-- Wait for mariadb container to start gracefully -->
<echo msg="Waiting for database container to start gracefully..."/>
<retry retryDelay="5" retryCount="12">
<exec command="docker-compose exec mariadb mysql -u root -p${MARIADB_ROOT_PASSWORD} ${MARIADB_DATABASE} -e 'select 1;'" dir="containers" checkreturn="true"/>
</retry>
<!-- Install Matomo database -->
<exec command="docker-compose exec mariadb mysql -u root -p${MARIADB_ROOT_PASSWORD} matomo -e 'select 1;'" dir="containers" returnProperty="checkMatomo"/>
<if>
<not><equals arg1="${checkMatomo}" arg2="0"/></not>
<then>
<echo msg="Installing Matomo database..."/>
<exec command="docker-compose exec mariadb bash /opt/install_matomo_db.sh" dir="containers" checkreturn="true"/>
<exec command="docker-compose exec php-fpm chown -R www-data:www-data /opt/piwik" dir="containers" passthru="true" checkreturn="true"/>
</then>
</if>
<!-- Run migrations -->
<echo msg="Running database migrations..."/>
<exec command="docker-compose exec workspace php artisan migrate" dir="containers" passthru="true" checkreturn="true"/>
<!-- Create roles -->
<exec command="docker-compose exec mariadb mysql -u root -p${MARIADB_ROOT_PASSWORD} ${MARIADB_DATABASE} -e 'select * from abilities where id=1'" dir="containers" outputProperty="checkRoles"/>
<if>
<equals arg1="${checkRoles}" arg2=""/>
<then>
<echo msg="Creating application roles..."/>
<exec command="docker-compose exec workspace php artisan app:create-roles" dir="containers" passthru="true" checkreturn="true"/>
</then>
</if>
<!-- Database seed -->
<exec command="docker-compose exec mariadb mysql -u root -p${MARIADB_ROOT_PASSWORD} ${MARIADB_DATABASE} -e 'select * from users where id=1'" dir="containers" outputProperty="checkDBseed"/>
<if>
<equals arg1="${checkDBseed}" arg2=""/>
<then>
<echo msg="Seeding database..."/>
<exec command="docker-compose exec workspace php artisan db:seed" dir="containers" passthru="true" checkreturn="true"/>
</then>
</if>
<!-- Clear sessions and caches -->
<echo msg="Clearing sessions and caches..."/>
<exec command="docker-compose exec workspace php artisan view:clear" dir="containers" passthru="true" checkreturn="true"/>
<exec command="docker-compose exec workspace php artisan route:clear" dir="containers" passthru="true" checkreturn="true"/>
<exec command="docker-compose exec workspace php artisan config:clear" dir="containers" passthru="true" checkreturn="true"/>
<exec command="docker-compose exec workspace php artisan cache:clear" dir="containers" passthru="true" checkreturn="true"/>
<exec command="docker-compose exec workspace php artisan clear-compiled" dir="containers" passthru="true" checkreturn="true"/>
<!-- Populate IPA list -->
<echo msg="Populating IPA list..."/>
<exec command="docker-compose exec workspace php artisan app:update-ipa" dir="containers" passthru="true" checkreturn="true"/>
<echo msg="Deleting sessions..."/>
<delete>
<fileset dir="storage/framework/sessions">
<include name="*"/>
</fileset>
</delete>
<echo msg="Web Analytics Italia up and running!"/>
</target>
<!-- Target: test -->
<target name="test">
<echo msg="Testing..."/>
<!-- Check if we are in the right environment -->
<if>
<not>
<or>
<equals arg1="${env.APP_ENV}" arg2="local"/>
<equals arg1="${env.APP_ENV}" arg2="testing"/>
</or>
</not>
<then>
<fail message="Testing not allowed in environments other than local or testing"/>
</then>
</if>
<!-- Set permissions -->
<echo msg="Setting permissions..."/>
<exec command="docker-compose exec workspace bash -c 'rm -f database/testing.sqlite; touch database/testing.sqlite'" dir="containers"/>
<exec command="docker-compose exec workspace bash -c 'touch storage/logs/testing.log'" dir="containers"/>
<exec command="docker-compose exec php-fpm chown -R www-data:www-data storage" dir="containers" passthru="true" checkreturn="true"/>
<exec command="docker-compose exec php-fpm chown -R www-data:www-data database" dir="containers" passthru="true" checkreturn="true"/>
<!-- Execute phpunit to run unit tests -->
<echo msg="Running unit tests [PHPUnit]..."/>
<exec command="docker-compose exec workspace bin/phpunit" dir="containers" passthru="true" checkreturn="true"/>
<!-- Execute dusk to run browser tests -->
<echo msg="Running browser tests [Laravel Dusk]..."/>
<exec command="docker-compose exec workspace php artisan dusk" dir="containers" passthru="true" checkreturn="true"/>
</target>
<!-- Target: stop -->
<target name="stop">
<echo msg="Stopping..."/>
<!-- Stop running containers -->
<if>
<available file="containers" type="dir" property="ignored"/>
<then>
<echo msg="Stopping Docker containers currently running..."/>
<exec command="docker-compose stop" dir="containers" passthru="true" checkreturn="true"/>
<echo msg="Web Analytics Italia stopped!"/>
</then>
</if>
</target>
<!-- Target: start -->
<target name="start">
<echo msg="Starting..."/>
<!-- Start containers -->
<if>
<available file="containers" type="dir" property="ignored"/>
<then>
<echo msg="Starting Docker containers..."/>
<exec command="docker-compose up -d nginx mariadb redis mailhog workspace queue-worker" dir="containers" passthru="true" checkreturn="true"/>
<echo msg="Web Analytics Italia up and running!"/>
</then>
</if>
</target>
<!-- Target: enter workspace -->
<target name="ws">
<if>
<available file="containers" type="dir" property="ignored"/>
<then>
<echo msg="Entering into the workspace..."/>
<exec command="docker-compose exec workspace bash" dir="containers" passthru="true"/>
</then>
</if>
</target>
<!-- Target: start phpMyAdmin -->
<target name="pma">
<if>
<available file="containers" type="dir" property="ignored"/>
<then>
<echo msg="Running phpMyAdmin..."/>
<exec command="docker-compose up -d phpmyadmin" dir="containers" passthru="true"/>
</then>
</if>
</target>
</project>