Skip to content

Commit 61cc78d

Browse files
committed
fix: prevent images publication if not latest Weekly or LTS
1 parent 17ddac1 commit 61cc78d

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed

.ci/publish.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set -eu -o pipefail
1212
: "${DOCKERHUB_ORGANISATION:=jenkins}"
1313
: "${DOCKERHUB_REPO:=jenkins}"
1414
: "${BAKE_TARGET:=linux}"
15+
: "${BYPASS_ONLY_LATEST_PUBLICATION:=false}"
1516

1617
export JENKINS_REPO="${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}"
1718

@@ -76,6 +77,16 @@ else
7677
LATEST_LTS="false"
7778
fi
7879

80+
if [[ "${LATEST_WEEKLY}" == "false" && "${LATEST_LTS}" == "false" ]]; then
81+
if [[ "${BYPASS_ONLY_LATEST_PUBLICATION}" == "false" ]]; then
82+
echo "ERROR: ${JENKINS_VERSION} is neither the lastest Weekly nor the latest LTS version, not publishing any image"
83+
exit 1
84+
else
85+
echo "WARNING: ${JENKINS_VERSION} is neither the lastest Weekly nor the latest LTS version"
86+
echo 'As BYPASS_ONLY_LATEST_PUBLICATION has been set to "true", still proceeding to its publication'
87+
fi
88+
fi
89+
7990
build_opts=("--pull")
8091
metadata_suffix="publish"
8192
if test "${dry_run}" == "true"; then
@@ -100,6 +111,9 @@ Using the following settings:
100111
* COMMIT_SHA: ${COMMIT_SHA}
101112
* LATEST_WEEKLY: ${LATEST_WEEKLY}
102113
* LATEST_LTS: ${LATEST_LTS}
114+
* latest_weekly_version: ${latest_weekly_version}
115+
* latest_lts_version: ${latest_lts_version}
116+
* BYPASS_ONLY_LATEST_PUBLICATION: ${BYPASS_ONLY_LATEST_PUBLICATION}
103117
* BUILD_METADATA_PATH: ${BUILD_METADATA_PATH}
104118
* BAKE_TARGET: ${BAKE_TARGET}
105119
* BAKE OPTIONS:

make.ps1

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
6146
Function 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

125156
function Initialize-DockerComposeFile {
126157
param (
@@ -166,6 +197,24 @@ Test-CommandExists 'yq'
166197
# Sanity check
167198
yq --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

240289
if ($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

Comments
 (0)