-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoboFile.php
385 lines (364 loc) · 12.1 KB
/
RoboFile.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
use DrupalFinder\DrupalFinder;
use Robo\Contract\VerbosityThresholdInterface;
use Robo\Tasks;
use Robo\Result;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Finder\Finder;
/**
* This is project's console commands configuration for Robo task runner.
*
* @see http://robo.li/
*/
class RoboFile extends Tasks {
/**
* Drupal root path.
*
* @var string
*/
protected $drupalRoot;
/**
* Project root path.
*
* @var string
*/
protected $projectRoot;
/**
* Builds separate artifact and pushes to remote defined in $DEPLOY_TARGET.
*
* If the tag parameter is set, deploy:tag is called, otherwise deploy:branch.
*
* @param array $options
* Options from the command line.
*
* @aliases deploy
*
* @throws \Exception
* Throws an exception if the deployment fails.
*/
public function deployBuild(array $options = [
'branch' => NULL,
'tag' => NULL,
'commit-msg' => NULL,
'remote-branch' => NULL,
'remote' => NULL,
'build_id' => NULL,
'date-tag' => FALSE,
]) {
$result = $this->deployBranch($options);
if ($result instanceof Result && $result->wasSuccessful()) {
$this->io()
->success('Deployment succeeded.');
}
else {
throw new \Exception('Deployment failed.');
}
}
/**
* Builds separate artifact and pushes to remote as a branch.
*
* @param array $options
* Options from the command line or internal call.
*
* @return \Robo\Result
* The result of the final push.
*/
public function deployBranch(array $options = [
'branch' => InputOption::VALUE_REQUIRED,
'commit-msg' => InputOption::VALUE_REQUIRED,
'remote-branch' => NULL,
'remote' => NULL,
'build_id' => NULL,
]) {
$this->setDeploymentOptions($options);
$this->say('Deploying to branch ' . $options['branch']);
$this->setDeploymentVersionControl($options);
$this->getSanitizedBuild();
$this->setDeploymentCommit($options);
$this->setCleanMerge($options);
return $this->getPushResult($options);
}
/**
* Insures that appropriate options are present.
*
* @param array $options
* Options passed from the command.
*/
protected function setDeploymentOptions(array &$options) {
$options['remote'] = $options['remote'] ?? getenv('DEPLOY_TARGET');
$options['branch'] = $options['branch'] ?? getenv('CI_BRANCH');
$options['remote-branch'] = $options['remote-branch'] ?? $options['branch'];
$options['deploy-branch'] = $options['remote-branch'] . '-deploy';
$options['build_id'] = $options['build_id'] ?? substr(getenv('CI_COMMIT_ID'), 0, 7);
// A target is required.
if (empty($options['remote'])) {
// We need a repository target.
throw new \UnexpectedValueException('Environment variable $DEPLOY_TARGET must be set before deployment or the --remote argument passed.');
}
// As is a branch.
if (empty($options['branch'])) {
throw new \UnexpectedValueException('A branch must be specified.');
}
// There should also be a commit-message.
if (empty($options['commit-msg'])) {
$options['commit-msg'] = 'Deployment built for Pantheon on' . date(DATE_RFC2822);
if (!empty($options['build_id'])) {
$options['commit-msg'] .= ' Build ID: ' . $options['build_id'];
}
}
// Store for later use.
$options['remote_name'] = 'pantheon';
}
/**
* Adds the deployment target as a remote.
*
* @param array $options
* Options passed from the command.
*
* @throws \Exception
* Throws an exception if the git tasks fail so that deployment will abort.
*/
protected function setDeploymentVersionControl(array $options) {
$remote_url = $options['remote'];
$remote_name = $options['remote_name'];
$this->say("Will push to git remote $remote_name at $remote_url");
// Collect all the needed tasks and run them.
$collection = $this->collectionBuilder();
$collection->addTask(
$this->taskExec("git remote add $remote_name $remote_url")
->dir($this->getProjectRoot())
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG)
);
$shallowCheck = $this->taskExec('git rev-parse --is-shallow-repository --no-revs HEAD')
->interactive(FALSE)
->run();
if (trim($shallowCheck->getMessage()) == 'true') {
$collection->addTask(
$this->taskExec('git fetch --unshallow')
);
}
$result = $collection->run();
if ($result instanceof Result && !$result->wasSuccessful()) {
throw new \Exception('Git configuration failed.');
}
}
/**
* Removes files and directories that should not be deployed.
*
* Also replaces .gitignore with deployment specific versions.
*
* @throws \Exception
* Throws an exception if the files are unable to be modified.
*/
protected function getSanitizedBuild() {
$this->say('Sanitizing artifact...');
$this->say('Finding .git subdirectories...');
$git = new Finder();
$git
->in([
$this->getProjectRoot() . '/vendor',
$this->getProjectRoot() . '/web',
])
->directories()
->ignoreDotFiles(FALSE)
->ignoreVCS(FALSE)
->name('/^\.git$/');
$this->say($git->count() . ' .git directories found');
$this->say('Finding any CHANGELOG.txt');
$changelog = new Finder();
$changelog
->in($this->getProjectRoot())
->files()
->name('CHANGELOG.txt');
$this->say($changelog->count() . ' CHANGELOG.txt files found');
$this->say('Finding .gitignore in themes');
$theme_ignores = new Finder();
$theme_ignores
->in([$this->getDrupalRoot() . '/themes'])
->ignoreDotFiles(FALSE)
->depth('< 2')
->files()
->name('/\.gitignore$/');
$this->say($theme_ignores->count() . ' .gitignore files found in themes');
$files = $git
->append($changelog)
->append($theme_ignores);
$taskFilesystemStack = $this->taskFilesystemStack();
foreach ($files->getIterator() as $item) {
$taskFilesystemStack
->remove($item->getRealPath());
}
$collection = $this->collectionBuilder();
$collection->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG);
$taskFilesystemStack->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG);
$collection->addTask($taskFilesystemStack);
$collection->addTask(
$this->taskFilesystemStack()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG)
->copy($this->getProjectRoot() . '/setup/deploy-gitignore',
$this->getProjectRoot() . '/.gitignore', TRUE)
);
$result = $collection->run();
if ($result instanceof Result && !$result->wasSuccessful()) {
throw new \Exception('Unable to sanizize the build.');
}
}
/**
* Commits the result of the deployment build.
*
* @param array $options
* Options passed from the command.
*
* @throws \Exception
* Throws an exception if the git tasks fail so that deployment will abort.
*/
protected function setDeploymentCommit(array $options) {
$gitJobs = $this->taskGitStack()
->stopOnFail()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG)
->checkout($options['branch'])
->add('-A')
->commit($options['commit-msg']);
$result = $gitJobs->run();
if ($result instanceof Result && !$result->wasSuccessful()) {
throw new \Exception('Git commit failed.');
}
}
/**
* Cleanly merges the current branch into the remote branch of the same name.
*
* Uses a simulated "theirs" strategy to prefer any changes or updates in
* the current branch over that in the remote.
*
* @param array $options
* Options passed from the command.
*
* @throws \Exception
* Throws an exception if the branch checkout or merge fails.
*
* @see https://stackoverflow.com/questions/173919/is-there-a-theirs-version-of-git-merge-s-ours/4969679#4969679
*/
protected function setCleanMerge(array $options) {
// We need some simple variables for expansion in a string.
$remote = $options['remote_name'];
$target = $remote . '/' . $options['remote-branch'];
$message = 'Merge to remote: ' . $options['commit-msg'];
$local = $options['deploy-branch'];
$this->say('Move to a new branch that tracks the target repo.');
$gitJobs = $this->taskGitStack()
->stopOnFail()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG)
->exec("fetch $remote")
->checkout("-b $local $target");
$result = $gitJobs->run();
if ($result instanceof Result && !$result->wasSuccessful()) {
// The remote branch may not exist. Just make the deploy branch.
$gitJobs = $this->taskGitStack()
->stopOnFail()
->checkout("-b $local");
$result = $gitJobs->run();
if ($result instanceof Result && !$result->wasSuccessful()) {
throw new \Exception('Unable to checkout or create a deployment branch.');
}
}
// Now cleanly merge $options['branch'] into $options['deploy-branch']
// with preference to $options['branch'] for any conflicts.
$this->say('Merging changes into remote tracking branch');
$merge = $this->taskGitStack()
->stopOnFail()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG)
->exec('merge -s ours ' . $options['branch'] . " -m \"$message\"")
->exec('branch branch-temp')
->exec('reset --hard ' . $options['branch'])
->exec('reset --soft branch-temp')
->exec('commit --amend -C HEAD')
->exec('branch -D branch-temp');
if ($options['tag']) {
$merge->tag($options['tag']);
}
$result = $merge->run();
if ($result instanceof Result && !$result->wasSuccessful()) {
throw new \Exception('Failed to merge deployment into the remote branch.');
}
$this->say('Code is ready to push');
}
/**
* Attempts to push the build and returns the final result.
*
* @param array $options
* Options passed from the command.
*
* @return \Robo\Result
* The final task result.
*/
protected function getPushResult(array $options) {
$gitJobs = $this->taskGitStack()
->stopOnFail()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG)
->push($options['remote_name'], $options['deploy-branch'] . ':' . $options['remote-branch']);
if (!empty($options['tag'])) {
$gitJobs->push($options['remote_name'], $options['tag']);
}
return $gitJobs->run();
}
/**
* Utility function to insure root paths are set.
*/
protected function setRoots() {
// Values are set here so one empty is a suffient flag.
if (empty($this->drupalRoot)) {
$drupalFinder = new DrupalFinder();
$drupalFinder->locateRoot(getcwd());
$this->drupalRoot = $drupalFinder->getDrupalRoot();
$this->projectRoot = $drupalFinder->getComposerRoot();
}
}
/**
* Get the path to the Drupal root directory.
*
* @return string
* The drupal root path.
*/
public function getDrupalRoot() {
if (empty($this->drupalRoot)) {
$this->setRoots();
}
return $this->drupalRoot;
}
/**
* Get the path to the project root directory.
*
* @return string
* The project root path.
*/
public function getProjectRoot() {
if (empty($this->projectRoot)) {
$this->setRoots();
}
return $this->projectRoot;
}
/**
* Prep a key to be one line and append to env file.
*
* @param string $key
* The key.
*/
public function keyPrep($key) {
$root = $this->getProjectRoot();
$key_contents = file_get_contents("$root/$key");
$one_line = str_replace(["\r", "\n"], '\\n',
$key_contents);
$result = $this->taskWriteToFile("$root/env")
->append(TRUE)
->line("SSH_PRIVATE_KEY=$one_line")
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG)
->run();
if ($result instanceof Result && $result->wasSuccessful()) {
$this->io()
->success("The key has been processed and appended to the env file.");
}
else {
$this->io()->error('Error message: ' . $result->getMessage());
}
}
}