-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-matrix-strategy.php
More file actions
executable file
·82 lines (65 loc) · 2.37 KB
/
generate-matrix-strategy.php
File metadata and controls
executable file
·82 lines (65 loc) · 2.37 KB
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
#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/common.php';
if (!file_exists(VERSIONS_FILE)) {
exit_cli(sprintf("%s file does not exist", VERSIONS_FILE));
}
$versions = json_decode(file_get_contents(VERSIONS_FILE), true);
$repository = getenv('DOCKER_REPOSITORY');
if (false === $repository) {
exit_cli("DOCKER_REPOSITORY environment variable is not set\n");
}
$include = [];
foreach ($versions as $majorVersion => $versionData) {
$version = $versionData['version'];
$isLatest = $versionData['latest'];
$platforms = implode(',', $versionData['platforms']);
foreach ($versionData['variants'] as $variant) {
$tags = getVersionTags($repository, $version, $variant, $isLatest);
$dir = sprintf('./%s/%s', $majorVersion, $variant);
$include[] = [
'name' => sprintf('%s-%s', $version, $variant),
'os' => 'ubuntu-latest',
'tags' => $tags,
'runs' => [
'build-and-push' => getBuildAndPushCommand($tags, $dir, platforms: $platforms, latest: $isLatest),
],
];
}
}
$strategy = [
'fail-fast' => true,
'matrix' => [
'include' => $include,
],
];
exit_cli(sprintf("%s\n", json_encode($strategy, JSON_PRETTY_PRINT)), STATUS_SUCCESS);
function getVersionTags(string $repository, string $version, string $variant, bool $latest = false): array
{
$versionParts = explode('.', $version);
$tags = [];
$levels = $latest ? 1 : 2;
for ($i = count($versionParts); $i > 0; $i--) {
$tagVer = implode('.', array_slice($versionParts, 0, $i));
$tags[] = sprintf('%s:%s-%s', $repository, $tagVer, $variant);
if ($i <= $levels) {
break;
}
}
if ($latest) {
$tags[] = sprintf('%s:%s', $repository, $variant);
}
return $tags;
}
function getBuildAndPushCommand(array $tags, string $dir, string $platforms = 'linux/amd64', bool $latest = false): string
{
$tagArgs = implode(' ', array_map(static fn(string $tag) => '--tag ' . $tag, $tags));
$cacheTags = $tags;
array_shift($cacheTags);
if ($latest) {
array_pop($cacheTags);
}
$cacheFromArgs = implode(' ', array_map(static fn(string $tag) => '--cache-from ' . $tag, $cacheTags));
return sprintf('docker buildx build --push --platform %s %s %s %s', $platforms, $cacheFromArgs, $tagArgs, $dir);
}