-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
make.ps1
173 lines (144 loc) · 5.94 KB
/
make.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
[CmdletBinding()]
Param(
[Parameter(Position=1)]
[String] $Target = 'build',
[String] $JenkinsVersion = '2.479',
[switch] $DryRun = $false
)
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue' # Disable Progress bar for faster downloads
$Repository = 'jenkins'
$Organisation = 'jenkins4eval'
$ImageType = 'windowsservercore-ltsc2019' # <WINDOWS_FLAVOR>-<WINDOWS_VERSION>
if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO)) {
$Repository = $env:DOCKERHUB_REPO
}
if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_ORGANISATION)) {
$Organisation = $env:DOCKERHUB_ORGANISATION
}
if(![String]::IsNullOrWhiteSpace($env:JENKINS_VERSION)) {
$JenkinsVersion = $env:JENKINS_VERSION
}
if(![String]::IsNullOrWhiteSpace($env:IMAGE_TYPE)) {
$ImageType = $env:IMAGE_TYPE
}
$env:DOCKERHUB_ORGANISATION = "$Organisation"
$env:DOCKERHUB_REPO = "$Repository"
$env:JENKINS_VERSION = "$JenkinsVersion"
# Add 'lts-' prefix to LTS tags not including Jenkins version
# Compared to weekly releases, LTS releases include an additional build number in their version
# Note: the ':' separator is included as trying to set an environment variable to empty on Windows unset it.
$env:SEPARATOR_LTS_PREFIX = ':'
if ($JenkinsVersion.Split('.').Count -eq 3) {
$env:SEPARATOR_LTS_PREFIX = ':lts-'
}
$items = $ImageType.Split('-')
$env:WINDOWS_FLAVOR = $items[0]
$env:WINDOWS_VERSION = $items[1]
$env:TOOLS_WINDOWS_VERSION = $items[1]
if ($items[1] -eq 'ltsc2019') {
# There are no eclipse-temurin:*-ltsc2019 or mcr.microsoft.com/powershell:*-ltsc2019 docker images unfortunately, only "1809" ones
$env:TOOLS_WINDOWS_VERSION = '1809'
}
# Retrieve the sha256 corresponding to the JENKINS_VERSION
$jenkinsShaURL = 'https://repo.jenkins-ci.org/releases/org/jenkins-ci/main/jenkins-war/{0}/jenkins-war-{0}.war.sha256' -f $env:JENKINS_VERSION
$webClient = New-Object System.Net.WebClient
$env:JENKINS_SHA = $webClient.DownloadString($jenkinsShaURL).ToUpper()
$env:COMMIT_SHA=$(git rev-parse HEAD)
$baseDockerCmd = 'docker-compose --file=build-windows.yaml'
$baseDockerBuildCmd = '{0} build --parallel --pull' -f $baseDockerCmd
Write-Host "= PREPARE: List of $Organisation/$Repository images and tags to be processed:"
Invoke-Expression "$baseDockerCmd config"
Write-Host '= BUILD: Building all images...'
switch ($DryRun) {
$true { Write-Host "(dry-run) $baseDockerBuildCmd" }
$false { Invoke-Expression $baseDockerBuildCmd }
}
Write-Host '= BUILD: Finished building all images.'
if($lastExitCode -ne 0 -and !$DryRun) {
exit $lastExitCode
}
function Test-Image {
param (
$ImageName
)
Write-Host "= TEST: Testing image ${ImageName}:"
$env:CONTROLLER_IMAGE = $ImageName
$env:DOCKERFILE = 'windows/{0}/hotspot/Dockerfile' -f $env:WINDOWS_FLAVOR
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
$failed = $false
if ($TestResults.FailedCount -gt 0) {
Write-Host "There were $($TestResults.FailedCount) failed tests in $ImageName"
$failed = $true
} else {
Write-Host "There were $($TestResults.PassedCount) passed tests out of $($TestResults.TotalCount) in $ImageName"
}
Remove-Item env:\CONTROLLER_IMAGE
Remove-Item env:\DOCKERFILE
return $failed
}
if($target -eq 'test') {
if ($DryRun) {
Write-Host '(dry-run) test'
} else {
# 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) {
$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 -Verbose -MaximumVersion 5.3.3
}
Import-Module -Verbose 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
Write-Host '= TEST: Testing all images...'
# Only fail the run afterwards in case of any test failures
$testFailed = $false
Invoke-Expression "$baseDockerCmd config" | yq '.services[].image' | ForEach-Object {
$testFailed = $testFailed -or (Test-Image $_.split(':')[1])
}
# Fail if any test failures
if($testFailed -ne $false) {
Write-Error 'Test stage failed!'
exit 1
} else {
Write-Host 'Test stage passed!'
}
}
}
if($target -eq "publish") {
Write-Host "= PUBLISH: push all images and tags"
switch($DryRun) {
$true { Write-Host "(dry-run) $baseDockerCmd push" }
$false { Invoke-Expression "$baseDockerCmd push" }
}
# Fail if any issues when publising the docker images
if($lastExitCode -ne 0) {
Write-Error "= PUBLISH: failed!"
exit 1
}
}
if($lastExitCode -ne 0 -and !$DryRun) {
Write-Error 'Build failed!'
} else {
Write-Host 'Build finished successfully'
}
exit $lastExitCode