-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.php
executable file
·182 lines (143 loc) · 4.17 KB
/
versions.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
#!/usr/bin/env php
<?php
declare(strict_types=1);
require(__DIR__ . '/common.php');
const VERSIONS = [
'8.2',
'8.3',
'8.4',
];
const LATEST = '8.4';
// Must be implemented in Dockerfile.template
const DISTROS = [
'alpine',
];
const VARIANTS = [
'fpm',
'cli',
];
// Must be implemented in Dockerfile.template
const SUBVARIANTS = [
'fpm' => [
'xdebug',
'pcov',
],
'cli' => [
'xdebug',
'pcov',
],
];
$currentVersions = [];
if (file_exists(VERSIONS_FILE)) {
$currentVersions = json_decode(file_get_contents(VERSIONS_FILE), true);
}
$bumps = [];
$latestVersions = [];
foreach (VERSIONS as $version) {
$latestVersion = getLatestVersion($version);
if (!$latestVersion) {
exit_cli(sprintf("Could not get latest version for %s\n", $version));
}
if (!dockerTagExists($latestVersion)) {
continue;
}
if (!isset($currentVersions[$version]) || $currentVersions[$version]['version'] !== $latestVersion) {
$bumps[] = sprintf('%s to %s', $version, $latestVersion);
}
$versionData = [
'version' => $latestVersion,
'latest' => $version == LATEST,
'variants' => [],
];
foreach (VARIANTS as $variant) {
foreach (DISTROS as $distro) {
$tagSuffix = sprintf('%s-%s', $variant, $distro);
$versionData['variants'][] = $tagSuffix;
if (!isset(SUBVARIANTS[$variant])) {
continue;
}
foreach (SUBVARIANTS[$variant] as $subvariant) {
$versionData['variants'][] = sprintf('%s-%s', $tagSuffix, $subvariant);
}
}
}
$latestVersions[$version] = $versionData;
}
writeVersionsToFile($currentVersions, $latestVersions);
exit_cli(getGithubActionsOutputParams($bumps), status: STATUS_SUCCESS);
function dockerTagExists(string $tag): bool
{
$resp = request(sprintf('https://hub.docker.com/v2/repositories/library/php/tags/%s', $tag));
$result = true;
if ($resp->getCurlErrno() > 0) {
logError("cURL error (%d): %s", $resp->getCurlErrno(), $resp->getCurlError());
$result = false;
}
if ($resp->getStatus() >= 400) {
logError("HTTP error: %d", $resp->getStatus());
$result = false;
}
if ($resp->getBody()['errinfo'] ?? false) {
logError("Docker Hub error: %s", $resp->getBody()['message']);
$result = false;
}
return $result;
}
function getLatestVersion(string $version): false|string
{
$resp = request('https://www.php.net/releases/index.php', [
'json' => true,
'max' => 1,
'version' => $version,
]);
if ($resp->getCurlErrno() > 0) {
logError("cURL error (%d): %s", $resp->getCurlErrno(), $resp->getCurlError());
return false;
}
if ($resp->getStatus() >= 400) {
logError("HTTP error: %d", $resp->getStatus());
return false;
}
if (null === $resp->getBody() || isset($resp->getBody()['error'])) {
return false;
}
return array_key_first($resp->getBody());
}
function writeVersionsToFile(array $currentVersions, array $latestVersions): void
{
$currentVersions = array_merge($currentVersions, $latestVersions);
$versions = [];
foreach (VERSIONS as $version) {
if (!isset($currentVersions[$version])) {
continue;
}
$versions[$version] = $currentVersions[$version];
}
$result = file_put_contents(
VERSIONS_FILE,
json_encode($latestVersions, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n",
);
if ($result === false) {
exit_cli(sprintf("Could not write to %s\n", VERSIONS_FILE));
}
}
function getGithubActionsOutputParams(array $bumps): string
{
$bumped = 0;
$message = 'n/a';
if (count($bumps) > 0) {
$bumped = 1;
$message = getCommitMessage($bumps);
}
// Must match the expected
return sprintf("bumped=%d\ncommit_message=%s", $bumped, $message);
}
function getCommitMessage(array $bumps): string
{
$last = array_pop($bumps);
$message = implode(', ', $bumps);
if ($message) {
$message .= ' and ';
}
return sprintf("Bump %s%s", $message, $last);
}