forked from deployphp/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal.php
200 lines (157 loc) · 5.86 KB
/
local.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
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/* (c) HAKGER[hakger.pl] Hubert Kowalski <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer;
/**
* Custom bins for local.
* Auto detectors have non-UNIX OS problems, so we are highly recommended
* using your paths instead of it.
*/
set('local_bin/php', function () {
return runLocally('which php')->toString();
});
set('local_bin/git', function () {
return runLocally('which git')->toString();
});
set('local_bin/composer', function () {
$composer = runLocally('which composer')->toString();
if (empty($composer)) {
runLocally("cd {{release_path}} && curl -sS https://getcomposer.org/installer | {{local_bin/php}}");
$composer = '{{local_bin/php}} {{local_release_path}}/composer.phar';
}
return $composer;
});
/**
* Check if we can use local git cache. by default it checks if we're using
* git in version at least 2.3.
* You can override it if You prefer shalow clones or do not use full
* release workflow, that allows You to take advantage of this setting
*/
set('local_git_cache', function() {
$gitVersion = runLocally('{{local_bin/git}} version');
$regs = [];
if (preg_match('/((\d+\.?)+)/', $gitVersion, $regs)) {
$version = $regs[1];
} else {
$version = "1.0.0";
}
return version_compare($version, '2.3', '>=');
});
set('local_deploy_path', '/tmp/deployer');
/**
* Return list of releases on server.
*/
set('local_releases_list', function () {
$list = runLocally('ls {{local_deploy_path}}/releases')->toArray();
rsort($list);
return $list;
});
/**
* Return release path.
*/
set('local_release_path', function () {
return str_replace("\n", '', runLocally("readlink {{local_deploy_path}}/release"));
});
/**
* Return current release path.
*/
set('local_current', function () {
return runLocally("readlink {{local_deploy_path}}/current")->toString();
});
desc('Preparing for local deploy');
task('local:prepare', function () {
runLocally('mkdir -p {{local_deploy_path}}'); //just to make sure everything exists
runLocally('if [ ! -d {{local_deploy_path}} ]; then echo ""; fi');
// Create releases dir.
runLocally("cd {{local_deploy_path}} && if [ ! -d releases ]; then mkdir releases; fi");
// Create shared dir.
runLocally("cd {{local_deploy_path}} && if [ ! -d shared ]; then mkdir shared; fi");
});
desc('Prepare local release');
task('local:release', function () {
$release = date('YmdHis');
$releasePath = "{{local_deploy_path}}/releases/$release";
$i = 0;
while (is_dir(parse($releasePath)) && $i < 42) {
$releasePath .= '.' . ++$i;
}
runLocally("mkdir -p $releasePath");
runLocally("cd {{local_deploy_path}} && if [ -h release ]; then rm release; fi");
runLocally("ln -s $releasePath {{local_deploy_path}}/release");
});
desc('Updating code');
task('local:update_code', function () {
$repository = trim(get('repository'));
$branch = get('branch');
$git = get('local_bin/git');
$gitCache = get('local_git_cache');
$depth = $gitCache ? '' : '--depth 1';
// If option `branch` is set.
if (input()->hasOption('branch')) {
$inputBranch = input()->getOption('branch');
if (!empty($inputBranch)) {
$branch = $inputBranch;
}
}
// Branch may come from option or from configuration.
$at = '';
if (!empty($branch)) {
$at = "-b $branch";
}
// If option `tag` is set
if (input()->hasOption('tag')) {
$tag = input()->getOption('tag');
if (!empty($tag)) {
$at = "-b $tag";
}
}
// If option `tag` is not set and option `revision` is set
if (empty($tag) && input()->hasOption('revision')) {
$revision = input()->getOption('revision');
if (!empty($revision)) {
$depth = '';
}
}
$releases = get('local_releases_list');
if ($gitCache && isset($releases[1])) {
try {
runLocally("$git clone $at --recursive -q --reference {{local_deploy_path}}/releases/{$releases[1]} --dissociate $repository {{local_release_path}} 2>&1");
} catch (\RuntimeException $e) {
// If {{local_deploy_path}}/releases/{$releases[1]} has a failed git clone, is empty, shallow etc, git would throw error and give up. So we're forcing it to act without reference in this situation
runLocally("$git clone $at --recursive -q $repository {{local_release_path}} 2>&1");
}
} else {
// if we're using git cache this would be identical to above code in catch - full clone. If not, it would create shallow clone.
runLocally("$git clone $at $depth --recursive -q $repository {{local_release_path}} 2>&1");
}
});
desc('Installing vendors locally');
task('local:vendors', function () {
runLocally("cd {{local_release_path}} && {{env_vars}} {{local_bin/composer}} {{composer_options}}");
});
desc('Creating symlink to local release');
task('local:symlink', function () {
runLocally("cd {{local_deploy_path}} && ln -sfn {{local_release_path}} current"); // Atomic override symlink.
runLocally("cd {{local_deploy_path}} && rm release"); // Remove release link.
});
desc('Show current local release.');
task('local:current', function () {
writeln('Current local release: ' . basename(get('local_current')));
});
desc('Cleaning up old local releases');
task('local:cleanup', function () {
$releases = get('local_releases_list');
$keep = get('keep_releases');
while ($keep > 0) {
array_shift($releases);
--$keep;
}
foreach ($releases as $release) {
runLocally("rm -rf {{local_deploy_path}}/releases/$release");
}
runLocally("cd {{local_deploy_path}} && if [ -e release ]; then rm release; fi");
runLocally("cd {{local_deploy_path}} && if [ -h release ]; then rm release; fi");
});