-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.php
187 lines (151 loc) · 5.12 KB
/
deploy.php
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
<?php
/**
* @author Adeyemi Olaoye <[email protected]>
* @author Adegoke Obasa <[email protected]>
*/
namespace Deployer;
require 'deploy/vendor/autoload.php';
require 'deploy/vendor/deployer/deployer/recipe/symfony.php';
set('ssh_type', 'native');
serverList('deploy/servers.yml');
set('writable_dirs', ['app/runtime', 'app/logs', 'app/web/assets']);
set('shared_dirs', ['app/runtime', 'app/logs', 'vendor']);
set('repository', '{{REPO_URL}}');
set('composer_options', 'install --verbose --prefer-dist --optimize-autoloader --no-progress --no-interaction');
set('local_path', __DIR__);
/**
* Installing node dependencies.
*/
task('deploy:install_node_dependencies', function () {
run('cd {{release_path}} && npm install');
run('cd {{release_path}} && ./node_modules/.bin/bower install');
run('cd {{release_path}} && ./node_modules/.bin/gulp build --env {{APPLICATION_ENV}}');
})->desc('Installing Node Dependencies');
/** Yii2 composer setup */
task('deploy:yii2_composer_config', function () {
run('composer config -g github-oauth.github.com ' . get('GITHUB_TOKEN'));
run('composer global require "fxp/composer-asset-plugin"');
});
task('deploy:update_staging', function () {
runLocally('cd ' . get('local_path'));
runLocally('git stash');
runLocally('git fetch');
runLocally('git checkout develop');
runLocally('git pull origin develop');
runLocally('git checkout staging');
runLocally('git pull origin staging');
runLocally('git merge develop');
runLocally('git push origin staging');
})->onlyForStage('staging');
/**
* Cleanup old releases.
*/
task('cleanup', function () {
$releases = get('releases_list');
$keep = get('keep_releases');
while ($keep > 0) {
array_shift($releases);
--$keep;
}
foreach ($releases as $release) {
run("sudo rm -rf {{deploy_path}}/releases/$release");
}
run("cd {{deploy_path}} && if [ -e release ]; then rm release; fi");
run("cd {{deploy_path}} && if [ -h release ]; then rm release; fi");
})->desc('Cleaning up old releases');
/**
* Tag a new release on production.
*/
task('release:tag_release', function () {
writeln('Tagging Release... ');
runLocally('cd ' . get('local_path'));
runLocally('git stash');
runLocally('git checkout production');
$releaseVersion = get('RELEASE_VERSION');
$releaseMessage = get('RELEASE_MESSAGE');
runLocally('git tag -a ' . $releaseVersion . ' -m "' . $releaseMessage . '"');
runLocally('git push --tags');
runLocally('git checkout develop');
writeln('Release Tagged Successfully');
})->onlyForStage('production');
/**
* Upload env file
*/
task('deploy:upload_environments_file', function () {
run('mv {{deploy_path}}/.env.{{APPLICATION_ENV}} {{release_path}}/app/env/.env');
});
/**
* Upload programs.conf
*/
task('deploy:upload_programs_config', function () {
run('sudo mv {{deploy_path}}/programs.conf.{{APPLICATION_ENV}} /etc/supervisor/conf.d/crm-queue-worker.conf');
});
task('webserver:restart', function () {
run('sudo service php7.2-fpm restart');
run('sudo service nginx restart');
});
/**
* Start the yii beanstalkd queue
*/
task('restart_tasks', function () {
run('sudo supervisorctl reread');
run('sudo supervisorctl update');
run('sudo supervisorctl restart all');
});
/**
* Main task
*/
task('deploy', [
'deploy:update_staging',
'deploy:prepare',
'deploy:release',
'deploy:update_code',
'deploy:shared',
'deploy:yii2_composer_config',
'deploy:vendors',
'deploy:install_node_dependencies',
'deploy:upload_programs_config',
'deploy:upload_environments_file',
'deploy:run_migrations',
'deploy:symlink',
'deploy:writable',
'cleanup',
'webserver:restart',
'restart_tasks',
'release:tag_release'
])->desc('Deploy Project');
/** Slack Tasks Begin */
task('slack:before_deploy', function () {
postToSlack('Starting deploy on ' . get('server.name') . '...');
});
task('slack:after_deploy', function () {
postToSlack('Deploy to ' . get('server.name') . ' done');
});
task('slack:after_migrate', function () {
postToSlack('Migrations done on ' . get('server.name'));
});
task('slack:before_migrate', function () {
postToSlack('Running migrations on ' . get('server.name') . '...');
});
/** Slack Tasks End */
task('deploy:first_run', function () {
$firstRun = run('if [ ! -d {{deploy_path}} ]; then echo true; fi');
set('first_run', ($firstRun == 'true'));
});
function postToSlack($message)
{
$slackHookUrl = get('SLACK_HOOK_URL');
if (!empty($slackHookUrl)) {
runLocally('curl -s -S -X POST --data-urlencode payload="{\"channel\": \"#' . get('SLACK_CHANNEL_NAME') .
'\", \"username\": \"{{RELEASE_BOT_LABEL}}\", \"text\": \"' . $message . '\"}" ' . get('SLACK_HOOK_URL'));
} else {
write('Configure the SLACK_HOOK_URL to post to slack');
}
}
/**
* Post to slack if the slack hook URL is not empty
*/
before('deploy:run_migrations', 'slack:before_migrate');
after('deploy:run_migrations', 'slack:after_migrate');
before('deploy', 'slack:before_deploy');
after('deploy', 'slack:after_deploy');