-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-environment
executable file
·88 lines (74 loc) · 2.29 KB
/
get-environment
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
#!/usr/bin/env php
<?php
// Count args
if ($argc < 4 || $argc > 5) {
echo "Expected 3 or 4 arguments\n";
echo "Usage: {$argv[0]} [--json | --github] {branch} {hash} {tag}\n";
exit(1);
}
// Get paths
$paths = require dirname(realpath(__FILE__)) . '/file-finder.php';
// Move to root
chdir($paths['root']);
// Get args
$type = 'github';
if ($argc === 5) {
$flag = array_splice($argv, 1, 1)[0];
if (!preg_match('/^--(json|github)$/', $flag, $matches)) {
echo "Invalid flag, use --json or --github";
exit(1);
}
$type = $matches[1];
}
$branch = preg_match('/([a-z][-_a-z0-9]+)$/', "{$argv[1]}", $matches) ? $matches[1] : "{$argv[1]}";
$tag = preg_match('/([a-z][-_a-z0-9]+)$/', "{$argv[3]}", $matches) ? $matches[1] : "{$argv[3]}";
$hash = $argv[2] ?: null;
// Get hash if missing
$hash ??= trim(`git log -1 --format='%H'`);
// Get tag
$isTag = isset($tag) && !empty($tag) && preg_match('/^v\d+\.\d+\.\d+$/', $tag, $matches);
$tag = $isTag ? $tag : null;
// If a tag is set, find it's hash
if ($isTag) {
$tagRef = escapeshellarg("refs/tags/{$tag}");
$hash = trim(`git log -1 --format='%H' {$tagRef}`);
}
// Get environments
$environments = json_decode(file_get_contents($paths['config']['environment']), true, 16, JSON_THROW_ON_ERROR);
// Get proper config
if ($isTag && empty($environments['_tagged'])) {
throw new UnderflowException('System is not configured for tagged depoyments');
} elseif ($isTag) {
$environment = $environments['_tagged'];
} else {
if (!preg_match('/^([a-z][a-z0-9-_]+\/)?[a-z][a-z0-9-_]+$/i', $branch)) {
throw new RuntimeException('Branch seems insecure');
}
$environment = $environments[$branch] ?? null;
if (!$environment) {
throw new UnderflowException("System is not configured to deploy [$branch]");
}
}
// Echo as JSON if requested
$result = array_merge($environment, [
'branch' => $branch,
'hash' => $hash,
'tag' => $tag
]);
// Sort for goodness sake
ksort($result);
// JSON
if ($type === 'json') {
echo json_encode($result, JSON_PRETTY_PRINT) . PHP_EOL;
exit(0);
}
// Github envs
if ($type === 'github') {
foreach ($result as $key => $value) {
printf("::set-output name=%s::%s\n", $key, $value);
}
exit(0);
}
// Fail
printf("Unkown output format %s.\n", $type);
exit(1);