-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.php
executable file
·178 lines (153 loc) · 4.23 KB
/
build.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
#!/usr/bin/env php
<?php
use function Clippy\clippy;
use function Clippy\plugins;
require_once __DIR__ . '/vendor/autoload.php';
$c = clippy()->register(plugins());
$c['app']->main('[--dry-run] [--step] [--image-prefix=] [--image-filter=] [--php-version=] [--download-url=] [--skip-push] [--no-cache]', function(
Clippy\Taskr $taskr,
$imagePrefix,
$imageFilter,
$phpVersion,
$downloadUrl,
$skipPush,
$noCache
) {
// Create an array of all potential build arguments
$args = [];
// CiviCRM version
$civiVersion =
$args['CIVICRM_VERSION'] =
file_get_contents('https://latest.civicrm.org/stable.php');
// If a PHP version is supplied build for that version, else build all
// recommended PHP versions.
if ($phpVersion) {
$phpVersions = [$phpVersion];
}
else {
$phpVersions = ['8.1', '8.2', '8.3'];
}
$defaults = ['CIVICRM_VERSION' => $civiVersion, 'PHP_VERSION' => 'php8.3'];
// The default image prefix is the official one.
$imagePrefix ??= 'civicrm';
// Make sure we have the latest base image before we get started.
foreach ($phpVersions as $phpVersion) {
$taskr->passthru('docker pull php:{{0}}-apache-bookworm', [
$phpVersion,
]);
}
$args['IMAGE_PREFIX'] = $imagePrefix;
if ($downloadUrl) {
$args['CIVICRM_DOWNLOAD_URL'] = $downloadUrl;
}
// Some extra flags to pass to the build command.
$extraFlags = [];
if ($noCache) {
$extraFlags[] = '--no-cache';
}
if (!$skipPush) {
$extraFlags[] = '--push';
}
// This associative array defines all the images that we build.
// - 'dir' is a directory in `build` that contains a Docker context
// - 'args' specifies the build args that are valid for this image
$images = [
[
'dir' => 'common-base',
'args' => [
'PHP_VERSION',
],
'tags' => [
'PHP_VERSION',
],
],
[
'dir' => 'civicrm-base',
'args' => [
'PHP_VERSION',
'IMAGE_PREFIX',
],
'tags' => [
'PHP_VERSION',
],
],
[
'dir' => 'civicrm',
'args' => [
'PHP_VERSION',
'IMAGE_PREFIX',
'CIVICRM_VERSION',
'CIVICRM_DOWNLOAD_URL',
],
'tags' => [
'PHP_VERSION',
'CIVICRM_VERSION',
],
],
];
if ($imageFilter) {
$filteredImages = [];
$imageFilters = explode(',', $imageFilter);
foreach ($images as $k => $image) {
if (in_array($image['dir'], $imageFilters)) {
$filteredImages[] = $image;
}
}
$images = $filteredImages;
}
// Build each image.
foreach ($images as $image) {
foreach ($phpVersions as $phpVersion) {
$args['PHP_VERSION'] = $phpVersion;
$parts = array_intersect_key(['CIVICRM_VERSION' => $civiVersion, 'PHP_VERSION' => 'php' . $phpVersion], array_flip($image['tags']));
$buildArgs = getBuildArgs($args, $image);
$tagFlags = getTagFlags("{$imagePrefix}/{$image['dir']}", $parts, $defaults);
$taskr->passthru('docker build build/{{0}} {{1|@}} {{2|@}} {{3|@}}', [
$image['dir'],
$buildArgs,
$tagFlags,
$extraFlags,
]);
}
}
});
function getBuildArgs($args, $image) {
$buildArgs = array_map(
fn($e) => isset($args[$e])
? "--build-arg {$e}={$args[$e]}"
: NULL, $image['args']
);
return array_filter($buildArgs);
}
function getTagFlags($name, $parts, $defaults) {
// The first 'definitive' tag.
$tags = [$parts];
// Add defaults
foreach ($parts as $key => $value) {
foreach ($tags as $tag) {
if ($defaults[$key] == $value) {
$tags[] = [$key => FALSE] + $tag;
}
}
}
// Add Civi version aliases
if (isset($parts['CIVICRM_VERSION'])) {
$versionParts = explode('.', $parts['CIVICRM_VERSION']);
$major = $versionParts[0];
$minor = "$versionParts[0].$versionParts[1]";
}
foreach ($tags as $tag) {
if (!empty($tag['CIVICRM_VERSION'])) {
$tags[] = ['CIVICRM_VERSION' => $major] + $tag;
$tags[] = ['CIVICRM_VERSION' => $minor] + $tag;
}
}
// Format into tag flags
$tagFlags = [];
foreach ($tags as $tag) {
$tagFlag = implode('-', array_filter($tag));
$tagFlag = !empty($tagFlag) ? $tagFlag : 'latest';
$tagFlags[] = "-t {$name}:{$tagFlag}";
}
return $tagFlags;
}