Skip to content

Commit

Permalink
feat(Windows): define tags in docker compose file instead of code
Browse files Browse the repository at this point in the history
  • Loading branch information
lemeurherve committed Jan 13, 2024
1 parent 06ade7e commit 414c6fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 55 deletions.
14 changes: 11 additions & 3 deletions build-windows.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
jdk11:
image: jdk11-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
image: ${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:jdk11-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
context: ./
dockerfile: ./windows/${WINDOWS_FLAVOR}/Dockerfile
Expand All @@ -10,8 +10,10 @@ services:
VERSION: ${REMOTING_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
TOOLS_WINDOWS_VERSION: ${TOOLS_WINDOWS_VERSION}
tags:
- "${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:${REMOTING_VERSION}-${BUILD_NUMBER}-jdk11-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}"
jdk17:
image: jdk17-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
image: ${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:jdk17-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
context: ./
dockerfile: ./windows/${WINDOWS_FLAVOR}/Dockerfile
Expand All @@ -21,8 +23,12 @@ services:
VERSION: ${REMOTING_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
TOOLS_WINDOWS_VERSION: ${TOOLS_WINDOWS_VERSION}
tags:
- "${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:${REMOTING_VERSION}-${BUILD_NUMBER}-jdk17-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}"
- "${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:${REMOTING_VERSION}-${BUILD_NUMBER}-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}"
- "${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}"
jdk21:
image: jdk21-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
image: ${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:jdk21-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
context: ./
dockerfile: ./windows/${WINDOWS_FLAVOR}/Dockerfile
Expand All @@ -32,3 +38,5 @@ services:
VERSION: ${REMOTING_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
TOOLS_WINDOWS_VERSION: ${TOOLS_WINDOWS_VERSION}
tags:
- "${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:${REMOTING_VERSION}-${BUILD_NUMBER}-jdk21-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}"
67 changes: 15 additions & 52 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ Function Test-CommandExists {
}
}

# this is the jdk version that will be used for the 'bare tag' images, e.g., jdk17-windowsservercore-1809 -> windowsserver-1809
$defaultJdk = '17'
$env:REMOTING_VERSION = "$RemotingVersion"

$items = $ImageType.Split("-")
Expand Down Expand Up @@ -126,30 +124,14 @@ function Test-Image {
Remove-Item env:\VERSION
}

function Publish-Image {
param (
[String] $Build,
[String] $ImageName
)
if ($DryRun) {
Write-Host "= PUBLISH: (dry-run) docker tag then publish '$Build $ImageName'"
} else {
Write-Host "= PUBLISH: Tagging $Build => full name = $ImageName"
docker tag "$Build" "$ImageName"

Write-Host "= PUBLISH: Publishing $ImageName..."
docker push "$ImageName"
}
}


$originalDockerComposeFile = 'build-windows.yaml'
$finalDockerComposeFile = 'build-windows-current.yaml'
$baseDockerCmd = 'docker-compose --file={0}' -f $finalDockerComposeFile
$baseDockerBuildCmd = '{0} build --parallel --pull' -f $baseDockerCmd

foreach($agentType in $AgentTypes) {
$env:AGENT_TYPE = $agentType
$env:DOCKERHUB_REPO = $agentType

# Temporary docker compose file (git ignored)
Copy-Item -Path $originalDockerComposeFile -Destination $finalDockerComposeFile
Expand All @@ -162,35 +144,21 @@ foreach($agentType in $AgentTypes) {

$builds = @{}

Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object {
$image = '{0}-{1}-{2}' -f $_, $env:WINDOWS_FLAVOR, $env:WINDOWS_VERSION_TAG # Ex: "jdk17-nanoserver-1809"

# Remove the 'jdk' prefix
$jdkMajorVersion = $_.Remove(0,3)

$versionTag = "${RemotingVersion}-${BuildNumber}-${image}"
$tags = @( $image, $versionTag )

# Additional image tag without any 'jdk' prefix for the default JDK
$baseImage = "${env:WINDOWS_FLAVOR}-${env:WINDOWS_VERSION_TAG}"
if($jdkMajorVersion -eq "$defaultJdk") {
$tags += $baseImage
$tags += "${RemotingVersion}-${BuildNumber}-${baseImage}"
}

$builds[$image] = @{
'Tags' = $tags;
$compose = Invoke-Expression "$baseDockerCmd config --format=json" 2>$null | ConvertFrom-Json
foreach ($service in $compose.services.PSObject.Properties) {
$builds[$service.Value.image] = @{
'AdditionalTags' = $service.Value.build.tags;
}
}

Write-Host "= PREPARE: List of $Organisation/$repository images and tags to be processed:"
ConvertTo-Json $builds
Invoke-Expression "$baseDockerCmd config"

Write-Host "= BUILD: Building all images..."
if ($DryRun) {
Write-Host "(dry-run) $baseDockerBuildCmd"
} else {
Invoke-Expression $baseDockerBuildCmd
switch ($DryRun) {
$true { Write-Host "(dry-run) $baseDockerBuildCmd" }
$false { Invoke-Expression $baseDockerBuildCmd }
}
Write-Host "= BUILD: Finished building all images."

Expand Down Expand Up @@ -247,20 +215,15 @@ foreach($agentType in $AgentTypes) {
}

if($target -eq "publish") {
# Only fail the run afterwards in case of any issues when publishing the docker images
$publishFailed = 0
foreach($b in $builds.Keys) {
foreach($tag in $Builds[$b]['Tags']) {
Publish-Image "$b" "${Organisation}/${Repository}:${tag}"
if($lastExitCode -ne 0) {
$publishFailed = 1
}
}
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($publishFailed -ne 0) {
Write-Error "Publish failed for ${Organisation}/${repository}!"
if($lastExitCode -ne 0) {
Write-Error "= PUBLISH: failed!"
exit 1
}
}
Expand Down

0 comments on commit 414c6fc

Please sign in to comment.