-
Notifications
You must be signed in to change notification settings - Fork 13
/
generate_php.php
146 lines (106 loc) · 4.47 KB
/
generate_php.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
<?php
$supportedVersions = ['8.1', '8.2', '8.3'];
$index = [];
$tpl = file_get_contents('Dockerfile.nginx-php.template');
$versionRegex ='/^(?<version>\d\.\d\.\d{1,})/m';
$workflow = <<<YML
version: 2.1
parameters:
build-image:
type: boolean
default: false
jobs:
YML;
$stages = [];
$dockerMerges = [];
foreach ($supportedVersions as $supportedVersion)
{
$apiResponse = json_decode(file_get_contents('https://hub.docker.com/v2/repositories/library/php/tags/?page_size=50&page=1&name=' . $supportedVersion. '.'), true);
if (!is_array($apiResponse)) {
throw new \RuntimeException("invalid api response");
}
$curVersion = null;
$patchVersion = null;
foreach ($apiResponse['results'] as $entry) {
if (strpos($entry['name'], 'RC') !== false) {
continue;
}
preg_match($versionRegex, $entry['name'], $patchVersion);
if (count($patchVersion) > 0) {
break;
}
}
if ($patchVersion === null) {
throw new \RuntimeException('There is no version found for PHP ' . $supportedVersion);
}
$folder = 'nginx/' . $supportedVersion . '/';
if (!file_exists($folder)) {
mkdir($folder, 0777, true);
}
file_put_contents($folder . 'Dockerfile', str_replace('${PHP_VERSION}', $patchVersion['version'], $tpl));
$index[$supportedVersion] = $patchVersion['version'];
$workflowTpl = <<<'TPL'
php${PHP_VERSION_SHORT}-arm64:
machine:
image: ubuntu-2004:current
docker_layer_caching: true
resource_class: arm.medium
steps:
- checkout
- run: echo "$GHCR_PASSWORD" | docker login ghcr.io -u shyim --password-stdin
- run: docker build -t ghcr.io/shyim/shopware-nginx:${PHP_VERSION}-arm64 -t ghcr.io/shyim/shopware-nginx:${PHP_PATCH_VERSION}-arm64 -f nginx/${PHP_VERSION}/Dockerfile .
- run: docker push ghcr.io/shyim/shopware-nginx:${PHP_VERSION}-arm64
- run: docker push ghcr.io/shyim/shopware-nginx:${PHP_PATCH_VERSION}-arm64
php${PHP_VERSION_SHORT}-amd64:
machine:
image: ubuntu-2004:current
docker_layer_caching: true
resource_class: medium
steps:
- checkout
- run: echo "$GHCR_PASSWORD" | docker login ghcr.io -u shyim --password-stdin
- run: docker build -t ghcr.io/shyim/shopware-nginx:${PHP_VERSION}-amd64 -t ghcr.io/shyim/shopware-nginx:${PHP_PATCH_VERSION}-amd64 -f nginx/${PHP_VERSION}/Dockerfile .
- run: docker push ghcr.io/shyim/shopware-nginx:${PHP_VERSION}-amd64
- run: docker push ghcr.io/shyim/shopware-nginx:${PHP_PATCH_VERSION}-amd64
TPL;
$phpShort = str_replace('.', '', $supportedVersion);
$replaces = [
'${PHP_VERSION_SHORT}' => $phpShort,
'${PHP_VERSION}' => $supportedVersion,
'${PHP_PATCH_VERSION}' => $patchVersion['version'],
];
$workflow .= str_replace(array_keys($replaces), array_values($replaces), $workflowTpl);
$dockerMerges[] = 'docker manifest create ghcr.io/shyim/shopware-nginx:' . $supportedVersion . ' --amend ghcr.io/shyim/shopware-nginx:' . $patchVersion['version'] . '-amd64 --amend ghcr.io/shyim/shopware-nginx:' . $patchVersion['version'] . '-arm64';
$dockerMerges[] = 'docker manifest create ghcr.io/shyim/shopware-nginx:' . $patchVersion['version'] . ' --amend ghcr.io/shyim/shopware-nginx:' . $patchVersion['version'] . '-amd64 --amend ghcr.io/shyim/shopware-nginx:' . $patchVersion['version'] . '-arm64';
$dockerMerges[] = 'docker manifest push ghcr.io/shyim/shopware-nginx:' . $supportedVersion;
$dockerMerges[] = 'docker manifest push ghcr.io/shyim/shopware-nginx:' . $patchVersion['version'];
$stages[] = 'php' . $phpShort . '-arm64';
$stages[] = 'php' . $phpShort . '-amd64';
}
$workflow .= '
merge-manifest:
machine:
image: ubuntu-2004:current
docker_layer_caching: true
resource_class: medium
steps:
- run: echo "$GHCR_PASSWORD" | docker login ghcr.io -u shyim --password-stdin
';
foreach ($dockerMerges as $merge) {
$workflow .= " - run: " . $merge . "\n\n";
}
$workflow .= 'workflows:
build-base-image:
when: << pipeline.parameters.build-image >>
jobs:
';
foreach ($stages as $stage) {
$workflow .= ' - ' . $stage . "\n";
}
$workflow .= " - merge-manifest:
requires:\n";
foreach ($stages as $stage) {
$workflow .= ' - ' . $stage . "\n";
}
file_put_contents('.circleci/docker-build.yml', $workflow);
file_put_contents('index_php.json', json_encode($index, true, JSON_PRETTY_PRINT));