This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
build.ps1
258 lines (220 loc) · 8.39 KB
/
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
[CmdletBinding()]
Param(
[Parameter(Position=1)]
[String] $Target = "build",
[String] $Build = '',
[String] $VersionTag = 'NEXT_TAG_VERSION',
[String] $ParentImageVersion = '3206.vb_15dcf73f6a_9-1',
[switch] $PushVersions = $false
)
$ErrorActionPreference = 'Stop'
$Repository = 'inbound-agent'
$Organization = 'jenkins'
$ImageType = 'windowsservercore-ltsc2019'
if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO)) {
$Repository = $env:DOCKERHUB_REPO
}
if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_ORGANISATION)) {
$Organization = $env:DOCKERHUB_ORGANISATION
}
if(![String]::IsNullOrWhiteSpace($env:PARENT_IMAGE_VERSION)) {
$ParentImageVersion = $env:PARENT_IMAGE_VERSION
}
if(![String]::IsNullOrWhiteSpace($env:IMAGE_TYPE)) {
$ImageType = $env:IMAGE_TYPE
}
# Check for required commands
Function Test-CommandExists {
# From https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
Param (
[String] $command
)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
try {
if(Get-Command $command){
Write-Debug "$command exists"
}
}
Catch {
"$command does not exist"
}
Finally {
$ErrorActionPreference=$oldPreference
}
}
# # this is the jdk version that will be used for the 'bare tag' images, e.g., jdk7-windowsservercore-1809 -> windowsserver-1809
$defaultJdk = '17'
$builds = @{}
$env:PARENT_IMAGE_VERSION = "$ParentImageVersion"
$items = $ImageType.Split("-")
$env:WINDOWS_FLAVOR = $items[0]
$env:WINDOWS_VERSION_TAG = $items[1]
# # Uncomment to help debugging when working on this script
# Write-Host "= DEBUG: env vars"
# Get-ChildItem Env: | ForEach-Object { Write-Host "$($_.Name) = $($_.Value)" }
$ProgressPreference = 'SilentlyContinue' # Disable Progress bar for faster downloads
Test-CommandExists "docker"
Test-CommandExists "docker-compose"
Test-CommandExists "yq"
$baseDockerCmd = 'docker-compose --file=build-windows.yaml'
$baseDockerBuildCmd = '{0} build --parallel --pull' -f $baseDockerCmd
Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object {
$image = '{0}-{1}-{2}' -f $_, $env:WINDOWS_FLAVOR, $env:WINDOWS_VERSION_TAG # Ex: "jdk17-windowsservercore-ltsc2019"
# Remove the 'jdk' prefix (3 first characters)
$jdkMajorVersion = $_.Remove(0,3)
$baseImage = "${env:WINDOWS_FLAVOR}-${env:WINDOWS_VERSION_TAG}"
$completeVersionTag = "${VersionTag}-${image}"
$tags = @( $image, $completeVersionTag )
# Additional image tag without any 'jdk' prefix for the default JDK
if($jdkMajorVersion -eq "$defaultJdk") {
$tags += $baseImage
}
$builds[$image] = @{
'Tags' = $tags;
}
}
Write-Host "= PREPARE: List of ${Organization}/${Repository} images and tags to be processed for ${ImageType}:"
ConvertTo-Json $builds
if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) {
Write-Host "= BUILD: Building image ${Build}..."
$dockerBuildCmd = '{0} {1}' -f $baseDockerBuildCmd, $Build
Invoke-Expression $dockerBuildCmd
Write-Host "= BUILD: Finished building image ${Build}"
} else {
Write-Host "= BUILD: Building all images..."
Invoke-Expression $baseDockerBuildCmd
Write-Host "= BUILD: Finished building all image"
}
if($lastExitCode -ne 0) {
exit $lastExitCode
}
function Test-Image {
param (
$ImageName
)
Write-Host "= TEST: Testing image ${ImageName}:" # Ex: jdk17-windowsservercore-ltsc2019
$env:AGENT_IMAGE = $ImageName
$serviceName = $ImageName.SubString(0, $ImageName.IndexOf('-'))
$env:BUILD_CONTEXT = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${serviceName}.build.context"
$env:version = $ParentImageVersion
if(Test-Path ".\target\$ImageName") {
Remove-Item -Recurse -Force ".\target\$ImageName"
}
New-Item -Path ".\target\$ImageName" -Type Directory | Out-Null
$configuration.TestResult.OutputPath = ".\target\$ImageName\junit-results.xml"
$TestResults = Invoke-Pester -Configuration $configuration
if ($TestResults.FailedCount -gt 0) {
Write-Host "There were $($TestResults.FailedCount) failed tests in $ImageName"
$testFailed = $true
} else {
Write-Host "There were $($TestResults.PassedCount) passed tests out of $($TestResults.TotalCount) in $ImageName"
}
Remove-Item env:\AGENT_IMAGE
Remove-Item env:\BUILD_CONTEXT
Remove-Item env:\VERSION
}
if($target -eq "test") {
Write-Host "= TEST: Starting test harness"
# Only fail the run afterwards in case of any test failures
$testFailed = $false
$mod = Get-InstalledModule -Name Pester -MinimumVersion 5.3.0 -MaximumVersion 5.3.3 -ErrorAction SilentlyContinue
if($null -eq $mod) {
Write-Host "= TEST: Pester 5.3.x not found: installing..."
$module = "c:\Program Files\WindowsPowerShell\Modules\Pester"
if(Test-Path $module) {
takeown /F $module /A /R
icacls $module /reset
icacls $module /grant Administrators:'F' /inheritance:d /T
Remove-Item -Path $module -Recurse -Force -Confirm:$false
}
Install-Module -Force -Name Pester -MaximumVersion 5.3.3
}
Import-Module Pester
Write-Host "= TEST: Setting up Pester environment..."
$configuration = [PesterConfiguration]::Default
$configuration.Run.PassThru = $true
$configuration.Run.Path = '.\tests'
$configuration.Run.Exit = $true
$configuration.TestResult.Enabled = $true
$configuration.TestResult.OutputFormat = 'JUnitXml'
$configuration.Output.Verbosity = 'Diagnostic'
$configuration.CodeCoverage.Enabled = $false
if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) {
Test-Image $Build
} else {
Write-Host "= TEST: Testing all images..."
foreach($image in $builds.Keys) {
Test-Image $image
}
}
# Fail if any test failures
if($testFailed -ne $false) {
Write-Error "Test stage failed!"
exit 1
} else {
Write-Host "Test stage passed!"
}
}
function Publish-Image {
param (
[String] $Build,
[String] $ImageName
)
Write-Host "= PUBLISH: Tagging $Build => full name = $ImageName"
docker tag "$Build" "$ImageName"
Write-Host "= PUBLISH: Publishing $ImageName..."
docker push "$ImageName"
}
if($target -eq "publish") {
# Only fail the run afterwards in case of any issues when publishing the docker images
$publishFailed = 0
if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) {
foreach($tag in $Builds[$Build]['Tags']) {
Publish-Image "$Build" "${Organization}/${Repository}:${tag}"
if($lastExitCode -ne 0) {
$publishFailed = 1
}
if($PushVersions) {
$buildTag = "$tag"
if($tag -eq 'latest') {
$buildTag = "$VersionTag"
}
Publish-Image "$b" "${Organization}/${Repository}:${buildTag}"
if($lastExitCode -ne 0) {
$publishFailed = 1
}
}
}
} else {
foreach($b in $builds.Keys) {
foreach($tag in $Builds[$b]['Tags']) {
Publish-Image "$b" "${Organization}/${Repository}:${tag}"
if($lastExitCode -ne 0) {
$publishFailed = 1
}
if($PushVersions) {
$buildTag = "$tag"
if($tag -eq 'latest') {
$buildTag = "$VersionTag"
}
Publish-Image "$b" "${Organization}/${Repository}:${buildTag}"
if($lastExitCode -ne 0) {
$publishFailed = 1
}
}
}
}
}
# Fail if any issues when publising the docker images
if($publishFailed -ne 0) {
Write-Error "Publish failed!"
exit 1
}
}
if($lastExitCode -ne 0) {
Write-Error "Build failed!"
} else {
Write-Host "Build finished successfully"
}
exit $lastExitCode