-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfirebird-docker.build.ps1
308 lines (243 loc) · 9.83 KB
/
firebird-docker.build.ps1
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#
# Globals
#
$outputFolder = './generated'
$defaultVariant = 'bookworm'
$blockedVariants = @{'3' = @('noble') } # Ubuntu 24.04 doesn't have libncurses5.
#
# Functions
#
function Expand-Template([Parameter(ValueFromPipeline = $true)]$Template) {
$evaluator = {
$innerTemplate = $args[0].Groups[1].Value
$ExecutionContext.InvokeCommand.ExpandString($innerTemplate)
}
$regex = [regex]"\<\%(.*?)\%\>"
$regex.Replace($Template, $evaluator)
}
function Copy-TemplateItem([string]$Path, [string]$Destination, [switch]$Force) {
if (Test-Path $Destination) {
# File already exists.
if ($Force) {
# With -Force: Overwrite.
$outputFile = Get-Item $Destination
$outputFile | Set-ItemProperty -Name IsReadOnly -Value $false
} else {
# Without -Force: Nothing to do.
return
}
}
if ( (-not $Force) -and (Test-Path $Destination) ) {
# File already exists. Ignore.
return
}
# Add header
$fileExtension = $Destination.Split('.')[-1]
$header = if ($fileExtension -eq 'md') {
@'
[//]: # (This file was auto-generated. Do not edit. See /src.)
'@
} else {
@'
#
# This file was auto-generated. Do not edit. See /src.
#
'@
}
$header | Set-Content $Destination -Encoding UTF8
# Expand template
Get-Content $Path -Raw -Encoding UTF8 |
Expand-Template |
Add-Content $Destination -Encoding UTF8
# Set readonly flag (another reminder to not edit the file)
$outputFile = Get-Item $Destination
$outputFile | Set-ItemProperty -Name IsReadOnly -Value $true
}
function Use-CachedResponse {
param(
[Parameter(Mandatory = $true)]
[string]$JsonFile,
[scriptblock]$ScriptBlock
)
if (Test-Path $JsonFile) {
return Get-Content $JsonFile | ConvertFrom-Json
}
$result = Invoke-Command -ScriptBlock $ScriptBlock
return $result | ConvertTo-Json -Depth 10 | Out-File $JsonFile -Encoding utf8
}
#
# Tasks
#
# Synopsis: Rebuild "assets.json" from GitHub releases.
task Update-Assets {
$tempFolder = [System.IO.Path]::GetTempPath()
$releasesFile = Join-Path $tempFolder 'github-releases.json'
$assetsFolder = Join-Path $tempFolder 'firebird-assets'
New-Item $assetsFolder -ItemType Directory -Force > $null
# All github releases
$releases = Use-CachedResponse -JsonFile $releasesFile { Invoke-RestMethod -Uri "https://api.github.com/repos/FirebirdSQL/firebird/releases" -UseBasicParsing }
# Ignore legacy and prerelease
$currentReleases = $releases | Where-Object { ($_.tag_name -like 'v*') -and (-not $_.prerelease) }
# Select only amd64/arm64 and non-debug assets
$currentAssets = $currentReleases |
Select-Object -Property @{ Name='version'; Expression={ [version]$_.tag_name.TrimStart("v") } },
@{ Name='download_url'; Expression={ $_.assets.browser_download_url | Where-Object { ( $_ -like '*amd64*' -or $_ -like '*linux-x64*' -or $_ -like '*linux-arm64*') -and ($_ -notlike '*debug*') } } } |
Sort-Object -Property version -Descending
# Group by major version
$groupedAssets = $currentAssets |
Select-Object -Property @{ Name='major'; Expression={ $_.version.Major } }, 'version', 'download_url' |
Group-Object -Property 'major' |
Sort-Object -Property Name -Descending
# Get Variants
$dockerFiles = Get-Item './src/Dockerfile.*.template'
$allOtherVariants = $dockerFiles.Name |
Select-String -Pattern 'Dockerfile.(.+).template' |
ForEach-Object { $_.Matches.Groups[1].Value } |
Where-Object { $_ -ne $defaultVariant }
$allVariants = @($defaultVariant) + $otherVariants
# For each asset
$groupedAssets | ForEach-Object -Begin { $groupIndex = 0 } -Process {
# For each major version
$_.Group | ForEach-Object -Begin { $index = 0 } -Process {
$asset = $_
# Remove blocked variants
$otherVariants = $allOtherVariants | Where-Object { $_ -notin $blockedVariants."$($asset.major)" }
$variants = $allVariants | Where-Object { $_ -notin $blockedVariants."$($asset.major)" }
$releases = $asset.download_url | ForEach-Object {
$url = [uri]$_
$assetFileName = $url.Segments[-1]
$assetLocalFile = Join-Path $assetsFolder $assetFileName
if (-not (Test-Path $assetLocalFile)) {
$ProgressPreference = 'SilentlyContinue' # How NOT to implement a progress bar -- https://stackoverflow.com/a/43477248
Invoke-WebRequest $url -OutFile $assetLocalFile
}
$sha256 = (Get-FileHash $assetLocalFile -Algorithm SHA256).Hash.ToLower()
if ($url -like '*arm64*') {
[ordered]@{
arm64 =
[ordered]@{
url = $url
sha256 = $sha256
}
}
} else {
[ordered]@{
amd64 =
[ordered]@{
url = $url
sha256 = $sha256
}
}
}
}
$tags = [ordered]@{}
$tags[$defaultVariant] = @("$($asset.version)")
$otherVariants | ForEach-Object {
$tags[$_] = @("$($asset.version)-$_")
}
if ($index -eq 0) {
# latest of this major version
$tags[$defaultVariant] = @("$($asset.major)") + $tags[$defaultVariant]
$otherVariants | ForEach-Object {
$tags[$_] = @("$($asset.major)-$_") + $tags[$_]
}
}
if (($groupIndex -eq 0) -and ($index -eq 0)) {
# latest of all
$tags[$defaultVariant] += 'latest'
$otherVariants | ForEach-Object {
$tags[$_] = @("$_") + $tags[$_]
}
}
Write-Output ([ordered]@{
'version' = "$($asset.version)"
'releases' = $releases
'tags' = $tags
})
$index++
}
$groupIndex++
} | ConvertTo-Json -Depth 10 | Out-File './assets.json' -Encoding ascii
}
# Synopsis: Rebuild "README.md" from "assets.json".
task Update-Readme {
# For each asset
$assets = Get-Content -Raw -Path '.\assets.json' | ConvertFrom-Json
$TSupportedTags = $assets | ForEach-Object {
$asset = $_
$version = [version]$asset.version
$versionFolder = Join-Path $outputFolder $version
# For each image
$asset.tags | Get-Member -MemberType NoteProperty | ForEach-Object {
$image = $_.Name
$TImageTags = $asset.tags.$image
if ($TImageTags) {
# https://stackoverflow.com/a/73073678
$TImageTags = "``{0}``" -f ($TImageTags -join "``, ``")
}
$variantFolder = (Join-Path $versionFolder $image).Replace('\', '/')
Write-Output "|$TImageTags|[Dockerfile]($variantFolder/Dockerfile)|`n"
}
}
Copy-TemplateItem "./src/README.md.template" './README.md' -Force
}
# Synopsis: Invoke preprocessor to generate images sources from "assets.json".
task Prepare {
# Clear/create output folder
Remove-Item -Path $outputFolder -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory $outputFolder -Force > $null
# For each asset
$assets = Get-Content -Raw -Path '.\assets.json' | ConvertFrom-Json
$assets | ForEach-Object {
$asset = $_
$version = [version]$asset.version
$versionFolder = Join-Path $outputFolder $version
New-Item -ItemType Directory $versionFolder -Force > $null
# For each image
$asset.tags | Get-Member -MemberType NoteProperty | ForEach-Object {
$image = $_.Name
$THasArchARM64 = ($asset.releases.arm64.url -ne $null -and $image -ne 'bullseye' -and $image -ne 'jammy' ?
'$true' : '$false')
$TUrlArchAMD64 = $asset.releases.amd64.url
$TSha256ArchAMD64 = $asset.releases.amd64.sha256
$TUrlArchARM64 = $asset.releases.arm64.url
$TSha256ArchARM64 = $asset.releases.arm64.sha256
$TMajor = $version.Major
$TImageVersion = $version
$TImageTags = $asset.tags.$image
if ($TImageTags) {
# https://stackoverflow.com/a/73073678
$TImageTags = "'{0}'" -f ($TImageTags -join "', '")
}
$variantFolder = Join-Path $versionFolder $image
New-Item -ItemType Directory $variantFolder -Force > $null
Copy-TemplateItem "./src/Dockerfile.$image.template" "$variantFolder/Dockerfile"
Copy-Item './src/entrypoint.sh' $variantFolder
Copy-TemplateItem "./src/image.build.ps1.template" "$variantFolder/image.build.ps1"
Copy-Item './src/image.tests.ps1' $variantFolder
}
}
}
# Synopsis: Build all docker images.
task Build Prepare, {
$builds = Get-ChildItem "$outputFolder/**/image.build.ps1" -Recurse | ForEach-Object {
@{File = $_; Task = 'Build' }
}
Build-Parallel $builds
}
# Synopsis: Run all tests.
task Test {
$builds = Get-ChildItem "$outputFolder/**/image.build.ps1" -Recurse | ForEach-Object {
@{File = $_; Task = 'Test' }
}
Build-Parallel $builds
}
# Synopsis: Publish all images.
task Publish {
$builds = Get-ChildItem "$outputFolder/**/image.build.ps1" -Recurse | ForEach-Object {
@{File = $_; Task = 'Publish' }
}
Build-Parallel $builds
}
# Synopsis: Default task.
task . Build