forked from sunnysideup/silverstripe-travis-support
-
Notifications
You must be signed in to change notification settings - Fork 0
/
travis_upload_artifacts.php
52 lines (42 loc) · 1.72 KB
/
travis_upload_artifacts.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
#!/usr/bin/env php
<?php
/**
* Creates an index.html with links to all files in a given directory structure, recursively.
* This is useful for Amazon S3 uploads with static file hosting, since it doesn't list files by default.
*
* Assumes to run in a SilverStripe webroot
*/
require_once 'lib.php';
$opts = getopt('', array(
'artifacts-path:',
'target-path:',
'if-env:',
'artifacts-base-url:',
));
// --if-env=BEHAT_TEST means that this script will only be executed if the given environment var is set
if(!checkenv(@$opts['if-env'])) {
echo "Apache skipped; {$opts['if-env']} wasn't set.\n";
exit(0);
}
$artifactsPath = (isset($opts['artifacts-path'])) ? $opts['artifacts-path'] : 'artifacts/';
$targetPath = $opts['target-path'];
$baseUrl = $opts['artifacts-base-url'];
if(!file_exists($artifactsPath)) {
echo "No artifacts found, skipped\n";
exit(0);
}
run("gem install --no-rdoc --no-ri --version 0.8.9 faraday");
run("gem install --no-rdoc --no-ri travis-artifacts");
echo "Creating $artifactsPath/index.html...\n";
$html = '<html><head></head><body><ul>';
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(realpath($artifactsPath)), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
if($object->isDir()) continue;
$relativePath = trim(str_replace(realpath($artifactsPath) . '/', '', $object->getPathName()), '/');
$html .= sprintf('<li><a href="%s">%s</a></li>', $relativePath, $relativePath);
}
$html .= '</ul></body></html>';
file_put_contents("$artifactsPath/index.html", $html);
run("travis-artifacts upload --path $artifactsPath --target-path $targetPath");
$fullPath = str_replace('//', '/', "$baseUrl/$targetPath/index.html");
echo "Uploaded artifacts to $fullPath\n";