@@ -42,21 +42,6 @@ $env:DOCKERHUB_REPO = "$Repository"
4242$env: JENKINS_VERSION = " $JenkinsVersion "
4343$env: COMMIT_SHA = git rev- parse HEAD
4444
45- # Add 'lts-' prefix to LTS tags not including Jenkins version
46- # Compared to weekly releases, LTS releases include an additional build number in their version
47- # Note: the ':' separator is included as trying to set an environment variable to empty on Windows unset it.
48- $env: SEPARATOR_LTS_PREFIX = ' :'
49- $releaseLine = ' war'
50- if ($JenkinsVersion.Split (' .' ).Count -eq 3 ) {
51- $env: SEPARATOR_LTS_PREFIX = ' :lts-'
52- $releaseLine = ' war-stable'
53- }
54-
55- # If there is no WAR_URL set, using get.jenkins.io URL depending on the release line
56- if ([String ]::IsNullOrWhiteSpace($env: WAR_URL )) {
57- $env: WAR_URL = ' https://get.jenkins.io/{0}/{1}/jenkins.war' -f $releaseLine , $env: JENKINS_VERSION
58- }
59-
6045# Check for required commands
6146Function Test-CommandExists {
6247 Param (
@@ -121,6 +106,52 @@ function Test-Image {
121106
122107 return $failed
123108}
109+ function Test-IsLatestJenkinsRelease {
110+ param (
111+ [String ] $Version
112+ )
113+
114+ Write-Host " = PREPARE: Checking if $env: JENKINS_VERSION is latest Weekly or LTS..."
115+
116+ $metadataUrl = " https://repo.jenkins-ci.org/releases/org/jenkins-ci/main/jenkins-war/maven-metadata.xml"
117+ try {
118+ [xml ]$metadata = Invoke-WebRequest $metadataUrl - UseBasicParsing
119+ }
120+ catch {
121+ Write-Error " Failed to retrieve Jenkins versions from Artifactory"
122+ exit 1
123+ }
124+ $allVersions = $metadata.metadata.versioning.versions.version
125+
126+ # Weekly
127+ $weeklyVersions = $allVersions |
128+ Where-Object { $_ -match ' ^\d+\.\d+$' } |
129+ ForEach-Object { [version ]$_ } |
130+ Sort-Object
131+
132+ # LTS
133+ $ltsVersions = $allVersions |
134+ Where-Object { $_ -match ' ^\d+\.\d+\.\d+$' } |
135+ ForEach-Object { [version ]$_ } |
136+ Sort-Object
137+
138+ $latestWeeklyVersion = $weeklyVersions [-1 ]
139+ Write-Host " latest Weekly version: $latestWeeklyVersion "
140+ $latestLTSVersion = $ltsVersions [-1 ]
141+ Write-Host " latest LTS version: $latestLTSVersion "
142+
143+ $latest = $false
144+ if ($Version -eq $latestWeeklyVersion ) {
145+ $latest = $true
146+ }
147+ if ($Version -eq $latestLTSVersion ) {
148+ $latest = $true
149+ }
150+ if (! $latest ) {
151+ Write-Host " WARNING: $JenkinsVersion is neither the lastest Weekly nor the latest LTS version"
152+ }
153+ return $latest
154+ }
124155
125156function Initialize-DockerComposeFile {
126157 param (
@@ -166,6 +197,24 @@ Test-CommandExists 'yq'
166197# Sanity check
167198yq -- version
168199
200+ # Add 'lts-' prefix to LTS tags not including Jenkins version
201+ # Compared to weekly releases, LTS releases include an additional build number in their version
202+ $releaseLine = ' war'
203+ # Determine if the current JENKINS_VERSION corresponds to the latest Weekly or LTS version from Artifactory
204+ $isJenkinsVersionLatest = Test-IsLatestJenkinsRelease - Version $JenkinsVersion
205+
206+ if ($JenkinsVersion.Split (' .' ).Count -eq 3 ) {
207+ $releaseLine = ' war-stable'
208+ $env: LATEST_LTS = $isJenkinsVersionLatest
209+ } else {
210+ $env: LATEST_WEEKLY = $isJenkinsVersionLatest
211+ }
212+
213+ # If there is no WAR_URL set, using get.jenkins.io URL depending on the release line
214+ if ([String ]::IsNullOrWhiteSpace($env: WAR_URL )) {
215+ $env: WAR_URL = ' https://get.jenkins.io/{0}/{1}/jenkins.war' -f $releaseLine , $JenkinsVersion
216+ }
217+
169218$dockerComposeFile = ' build-windows_{0}.yaml' -f $ImageType
170219$baseDockerCmd = ' docker-compose --file={0}' -f $dockerComposeFile
171220$baseDockerBuildCmd = ' {0} build --parallel --pull' -f $baseDockerCmd
@@ -239,6 +288,13 @@ if ($target -eq 'test') {
239288
240289if ($target -eq ' publish' ) {
241290 Write-Host ' = PUBLISH: push all images and tags'
291+
292+ # Prevent publication if JENKINS_VERSION is not the latest Weekly or LTS version
293+ if (! $isJenkinsVersionLatest -and ! $env: BYPASS_ONLY_LATEST_PUBLICATION ) {
294+ Write-Host " ERROR: $JenkinsVersion is neither the lastest Weekly nor the latest LTS version"
295+ exit 1
296+ }
297+
242298 switch ($DryRun ) {
243299 $true { Write-Host " (dry-run) $baseDockerCmd push" }
244300 $false { Invoke-Expression " $baseDockerCmd push" }
0 commit comments