1
1
[CmdletBinding ()]
2
2
Param (
3
3
[Parameter (Position = 1 )]
4
- [String ] $Target = " build" ,
5
- [String ] $JenkinsVersion = ' ' ,
4
+ [String ] $Target = ' build' ,
5
+ [String ] $JenkinsVersion = ' 2.431 ' ,
6
6
[switch ] $DryRun = $false
7
7
)
8
8
9
+ $ErrorActionPreference = ' Stop'
10
+ $ProgressPreference = ' SilentlyContinue' # Disable Progress bar for faster downloads
11
+
9
12
$Repository = ' jenkins'
10
- $Organization = ' jenkins4eval'
13
+ $Organisation = ' jenkins4eval'
14
+ $ImageType = ' windowsservercore-ltsc2019' # <WINDOWS_FLAVOR>-<WINDOWS_VERSION>
11
15
12
16
if (! [String ]::IsNullOrWhiteSpace($env: DOCKERHUB_REPO )) {
13
17
$Repository = $env: DOCKERHUB_REPO
14
18
}
15
19
16
20
if (! [String ]::IsNullOrWhiteSpace($env: DOCKERHUB_ORGANISATION )) {
17
- $Organization = $env: DOCKERHUB_ORGANISATION
21
+ $Organisation = $env: DOCKERHUB_ORGANISATION
18
22
}
19
23
20
- # this is the jdk version that will be used for the 'bare tag' images, e.g., jdk17-windowsservercore-1809 -> windowsserver-1809
21
- $defaultBuild = ' 17'
22
- $defaultJvm = ' hotspot'
23
- $builds = @ {}
24
+ if (! [String ]::IsNullOrWhiteSpace($env: JENKINS_VERSION )) {
25
+ $JenkinsVersion = $env: JENKINS_VERSION
26
+ }
24
27
25
- Get-ChildItem - Recurse - Include windows - Directory | ForEach-Object {
26
- Get-ChildItem - Recurse - Directory - Path $_ | Where-Object { Test-Path (Join-Path $_.FullName " Dockerfile" ) } | ForEach-Object {
27
- $dir = $_.FullName.Replace ((Get-Location ), " " ).TrimStart([IO.Path ]::DirectorySeparatorChar)
28
- $items = $dir.Split ([IO.Path ]::DirectorySeparatorChar)
29
- $jdkVersion = $items [0 ]
30
- $baseImage = $items [2 ]
31
- $jvmType = $items [3 ]
32
- $basicTag = " jdk${jdkVersion} -${jvmType} -${baseImage} "
33
- $tags = @ ( $basicTag )
34
- if (($jdkVersion -eq $defaultBuild ) -and ($jvmType -eq $defaultJvm )) {
35
- $tags += $baseImage
36
- }
28
+ if (! [String ]::IsNullOrWhiteSpace($env: IMAGE_TYPE )) {
29
+ $ImageType = $env: IMAGE_TYPE
30
+ }
37
31
38
- $builds [$basicTag ] = @ {
39
- ' Folder' = $dir ;
40
- ' Tags' = $tags ;
41
- }
42
- }
32
+ $env: DOCKERHUB_ORGANISATION = " $Organisation "
33
+ $env: DOCKERHUB_REPO = " $Repository "
34
+ $env: JENKINS_VERSION = " $JenkinsVersion "
35
+
36
+ $items = $ImageType.Split (' -' )
37
+ $env: WINDOWS_FLAVOR = $items [0 ]
38
+ $env: WINDOWS_VERSION = $items [1 ]
39
+ $env: TOOLS_WINDOWS_VERSION = $items [1 ]
40
+ if ($items [1 ] -eq ' ltsc2019' ) {
41
+ # There are no eclipse-temurin:*-ltsc2019 or mcr.microsoft.com/powershell:*-ltsc2019 docker images unfortunately, only "1809" ones
42
+ $env: TOOLS_WINDOWS_VERSION = ' 1809'
43
43
}
44
44
45
- Write-Host " = PREPARE: List of $Organization /$Repository images and tags to be processed:"
46
- ConvertTo-Json $builds
47
-
48
- foreach ($b in $builds.Keys ) {
49
- foreach ($tag in $builds [$b ][' Tags' ]) {
50
- Write-Host " Building $b => tag=$tag "
51
- $cmd = " docker build -t {0}/{1}:{2} {3}" -f $Organization , $Repository , $tag , $builds [$b ][' Folder' ]
52
- switch ($DryRun ) {
53
- $true { Write-Host " (dry-run) $cmd " }
54
- $false {
55
- Copy-Item - Path ' jenkins.ps1' - Destination (Join-Path $builds [$b ][' Folder' ] ' jenkins.ps1' ) - Force
56
- Copy-Item - Path ' jenkins-support.psm1' - Destination (Join-Path $builds [$b ][' Folder' ] ' jenkins-support.psm1' ) - Force
57
- Copy-Item - Path ' jenkins-plugin-cli.ps1' - Destination (Join-Path $builds [$b ][' Folder' ] ' jenkins-plugin-cli.ps1' ) - Force
58
- Invoke-Expression $cmd
59
- }
60
- }
61
- }
45
+ # Retrieve the sha256 corresponding to the JENKINS_VERSION
46
+ $jenkinsShaURL = ' https://repo.jenkins-ci.org/releases/org/jenkins-ci/main/jenkins-war/{0}/jenkins-war-{0}.war.sha256' -f $env: JENKINS_VERSION
47
+ $webClient = New-Object System.Net.WebClient
48
+ $env: JENKINS_SHA = $webClient.DownloadString ($jenkinsShaURL ).ToUpper()
49
+
50
+ $env: COMMIT_SHA = $ (git rev- parse HEAD)
51
+
52
+ $baseDockerCmd = ' docker-compose --file=build-windows.yaml'
53
+ $baseDockerBuildCmd = ' {0} build --parallel --pull' -f $baseDockerCmd
54
+
55
+ $builds = @ {}
56
+ $compose = Invoke-Expression " $baseDockerCmd config --format=json" 2> $null | ConvertFrom-Json
57
+ foreach ($service in $compose.services.PSObject.Properties ) {
58
+ $tags = @ ($service.Value.image )
59
+ $tags += $service.Value.build.tags
60
+ $builds [$service.Value.image ] = @ {
61
+ ' Tags' = $tags ;
62
+ }
63
+ }
64
+
65
+ Write-Host " = PREPARE: List of $Organisation /$Repository images and tags to be processed:"
66
+ Invoke-Expression " $baseDockerCmd config"
67
+
68
+ Write-Host ' = BUILD: Building all images...'
69
+ switch ($DryRun ) {
70
+ $true { Write-Host " (dry-run) $baseDockerBuildCmd " }
71
+ $false { Invoke-Expression $baseDockerBuildCmd }
62
72
}
73
+ Write-Host ' = BUILD: Finished building all images.'
63
74
64
75
if ($lastExitCode -ne 0 -and ! $DryRun ) {
65
76
exit $lastExitCode
66
77
}
67
78
68
- if ($target -eq " test" ) {
79
+ function Test-Image {
80
+ param (
81
+ $ImageName
82
+ )
83
+
84
+ Write-Host " = TEST: Testing image ${ImageName} :"
85
+
86
+ $env: CONTROLLER_IMAGE = $ImageName
87
+ $env: DOCKERFILE = ' windows/{0}/hotspot/Dockerfile' -f $env: WINDOWS_FLAVOR
88
+
89
+ if (Test-Path " .\target\$ImageName " ) {
90
+ Remove-Item - Recurse - Force " .\target\$ImageName "
91
+ }
92
+ New-Item - Path " .\target\$ImageName " - Type Directory | Out-Null
93
+ $configuration.TestResult.OutputPath = " .\target\$ImageName \junit-results.xml"
94
+
95
+ $TestResults = Invoke-Pester - Configuration $configuration
96
+ if ($TestResults.FailedCount -gt 0 ) {
97
+ Write-Host " There were $ ( $TestResults.FailedCount ) failed tests in $ImageName "
98
+ $testFailed = $true
99
+ } else {
100
+ Write-Host " There were $ ( $TestResults.PassedCount ) passed tests out of $ ( $TestResults.TotalCount ) in $ImageName "
101
+ }
102
+
103
+ Remove-Item env:\CONTROLLER_IMAGE
104
+ Remove-Item env:\DOCKERFILE
105
+ }
106
+
107
+ if ($target -eq ' test' ) {
69
108
if ($DryRun ) {
70
- Write-Host " (dry-run) test"
109
+ Write-Host ' (dry-run) test'
71
110
} else {
72
111
# Only fail the run afterwards in case of any test failures
73
112
$testFailed = $false
74
- $mod = Get-InstalledModule - Name Pester - MinimumVersion 4.9 .0 - MaximumVersion 4.99 . 99 - ErrorAction SilentlyContinue
113
+ $mod = Get-InstalledModule - Name Pester - MinimumVersion 5.3 .0 - MaximumVersion 5.3 . 3 - ErrorAction SilentlyContinue
75
114
if ($null -eq $mod ) {
76
- $module = " c:\Program Files\WindowsPowerShell\Modules\Pester"
115
+ $module = ' c:\Program Files\WindowsPowerShell\Modules\Pester'
77
116
if (Test-Path $module ) {
78
117
takeown / F $module / A / R
79
118
icacls $module / reset
80
119
icacls $module / grant Administrators:' F' / inheritance:d / T
81
120
Remove-Item - Path $module - Recurse - Force - Confirm:$false
82
121
}
83
- Install-Module - Force - Name Pester - MaximumVersion 4.99 . 99
122
+ Install-Module - Force - Name Pester - Verbose - MaximumVersion 5.3 . 3
84
123
}
85
124
86
- foreach ($b in $builds.Keys ) {
87
- $folder = $builds [$b ][' Folder' ]
88
- $env: FOLDER = $folder
89
- if (Test-Path " .\target\$folder " ) {
90
- Remove-Item - Force - Recurse " .\target\$folder "
91
- }
92
- New-Item - Path " .\target\$folder " - Type Directory | Out-Null
93
- $TestResults = Invoke-Pester - Path tests - PassThru - OutputFile " .\target\$folder \junit-results.xml" - OutputFormat JUnitXml
94
- if ($TestResults.FailedCount -gt 0 ) {
95
- Write-Host " There were $ ( $TestResults.FailedCount ) failed tests in $b "
96
- $testFailed = $true
97
- } else {
98
- Write-Host " There were $ ( $TestResults.PassedCount ) passed tests out of $ ( $TestResults.TotalCount ) in $b "
99
- }
100
- Remove-Item - Force env:\FOLDER
125
+ Import-Module - Verbose Pester
126
+ Write-Host ' = TEST: Setting up Pester environment...'
127
+ $configuration = [PesterConfiguration ]::Default
128
+ $configuration.Run.PassThru = $true
129
+ $configuration.Run.Path = ' .\tests'
130
+ $configuration.Run.Exit = $true
131
+ $configuration.TestResult.Enabled = $true
132
+ $configuration.TestResult.OutputFormat = ' JUnitXml'
133
+ $configuration.Output.Verbosity = ' Diagnostic'
134
+ $configuration.CodeCoverage.Enabled = $false
135
+
136
+ Write-Host ' = TEST: Testing all images...'
137
+ foreach ($image in $builds.Keys ) {
138
+ Test-Image $image.split (' :' )[1 ]
101
139
}
102
140
103
141
# Fail if any test failures
104
142
if ($testFailed -ne $false ) {
105
- Write-Error " Test stage failed!"
143
+ Write-Error ' Test stage failed!'
106
144
exit 1
107
145
} else {
108
- Write-Host " Test stage passed!"
146
+ Write-Host ' Test stage passed!'
109
147
}
110
148
}
111
149
}
112
150
113
- if ($target -eq " publish" ) {
151
+ if ($target -eq ' publish' ) {
114
152
# Only fail the run afterwards in case of any issues when publishing the docker images
115
153
$publishFailed = 0
116
154
foreach ($b in $builds.Keys ) {
117
- foreach ($tag in $Builds [$b ][' Tags' ]) {
118
- Write-Host " Publishing $b => tag=$tag "
119
- $cmd = " docker push {0}/{1}:{2} " -f $Organization , $Repository , $tag
155
+ foreach ($taggedImage in $Builds [$b ][' Tags' ]) {
156
+ Write-Host " Publishing $b => tag=$taggedImage "
157
+ $cmd = ' docker push {0}' -f $taggedImage
120
158
switch ($DryRun ) {
121
159
$true { Write-Host " (dry-run) $cmd " }
122
160
$false { Invoke-Expression $cmd }
123
- }
161
+ }
124
162
if ($lastExitCode -ne 0 ) {
125
163
$publishFailed = 1
126
164
}
@@ -129,14 +167,14 @@ if($target -eq "publish") {
129
167
130
168
# Fail if any issues when publising the docker images
131
169
if ($publishFailed -ne 0 -and ! $DryRun ) {
132
- Write-Error " Publish failed!"
170
+ Write-Error ' Publish failed!'
133
171
exit 1
134
172
}
135
173
}
136
174
137
175
if ($lastExitCode -ne 0 -and ! $DryRun ) {
138
- Write-Error " Build failed!"
176
+ Write-Error ' Build failed!'
139
177
} else {
140
- Write-Host " Build finished successfully"
178
+ Write-Host ' Build finished successfully'
141
179
}
142
180
exit $lastExitCode
0 commit comments