Skip to content

Commit

Permalink
feat(Windows): use docker-compose file for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
lemeurherve committed Oct 27, 2023
1 parent 75b5a4c commit c706390
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 61 deletions.
15 changes: 12 additions & 3 deletions build-windows.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: agent
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 +11,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 +24,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 +39,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}"
123 changes: 65 additions & 58 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,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'
$builds = @{}
$env:REMOTING_VERSION = "$RemotingVersion"

Expand All @@ -85,35 +83,24 @@ Test-CommandExists "yq"
$baseDockerCmd = 'docker-compose --file=build-windows.yaml'
$baseDockerBuildCmd = '{0} build --parallel --pull' -f $baseDockerCmd

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"
$compose = Invoke-Expression "$baseDockerCmd config --format=json" 2>$null | ConvertFrom-Json

# Remove the 'jdk' prefix
$jdkMajorVersion = $_.Remove(0,3)
foreach ($service in $compose.services.PSObject.Properties) {
$value = $service.Value

$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;
$builds[$value.image] = @{
'Tags' = $value.build.tags;
}
}

Write-Host "= PREPARE: List of $Organization/$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 All @@ -125,39 +112,40 @@ function Test-Image {
param (
$ImageName
)
if ($DryRun) {
Write-Host "= TEST: (dry-run) Testing image ${ImageName}:"
} else {
Write-Host "= TEST: Testing image ${ImageName}:"

Write-Host "= TEST: Testing image ${ImageName}:"

$env:AGENT_IMAGE = $ImageName
$serviceName = $ImageName.SubString(0, $ImageName.IndexOf('-'))
$env:BUILD_CONTEXT = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${serviceName}.build.context"
$env:VERSION = "$RemotingVersion-$BuildNumber"
$env:AGENT_IMAGE = $ImageName
# $serviceName = $ImageName.SubString(0, $ImageName.IndexOf('-'))
$env:BUILD_CONTEXT = './' # Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${serviceName}.build.context"
$env:VERSION = "$RemotingVersion-$BuildNumber"

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
if ($TestResults.FailedCount -gt 0) {
Write-Host "There were $($TestResults.FailedCount) failed tests in $ImageName"
$testFailed = $true
} else {
Write-Host "There were $($TestResults.PassedCount) passed tests out of $($TestResults.TotalCount) in $ImageName"
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
if ($TestResults.FailedCount -gt 0) {
Write-Host "There were $($TestResults.FailedCount) failed tests in $ImageName"
$testFailed = $true
} else {
Write-Host "There were $($TestResults.PassedCount) passed tests out of $($TestResults.TotalCount) in $ImageName"
}
Remove-Item env:\AGENT_IMAGE
Remove-Item env:\BUILD_CONTEXT
Remove-Item env:\VERSION
}
Remove-Item env:\AGENT_IMAGE
Remove-Item env:\BUILD_CONTEXT
Remove-Item env:\VERSION
}

if($target -eq "test") {
if ($DryRun) {
Write-Host "= TEST: (dry-run) test harness"
Write-Host "= TEST: (dry-run) setup test harness"
} else {
Write-Host "= TEST: Starting test harness"
Write-Host "= TEST: Setup test harness"

# 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) {
Write-Host "= TEST: Pester 5.3.x not found: installing..."
Expand All @@ -181,19 +169,23 @@ if($target -eq "test") {
$configuration.TestResult.OutputFormat = 'JUnitXml'
$configuration.Output.Verbosity = 'Diagnostic'
$configuration.CodeCoverage.Enabled = $false
}

Write-Host "= TEST: Testing all images..."
foreach($image in $builds.Keys) {
Test-Image $image
}
# Only fail the run afterwards in case of any test failures
$testFailed = $false

# Fail if any test failures
if($testFailed -ne $false) {
Write-Error "Test stage failed!"
exit 1
} else {
Write-Host "= TEST: stage passed!"
}
Write-Host "= TEST: Testing all images..."
foreach($image in $builds.Keys) {
$imageParts = $image -split ':'
Test-Image $imageParts[1]
}

# Fail if any test failures
if($testFailed -ne $false) {
Write-Error "Test stage failed!"
exit 1
} else {
Write-Host "= TEST: stage passed!"
}
}

Expand All @@ -219,7 +211,9 @@ if($target -eq "publish") {
$publishFailed = 0
foreach($b in $builds.Keys) {
foreach($tag in $Builds[$b]['Tags']) {
Publish-Image "$b" "${Organization}/${Repository}:${tag}"
$imageParts = $image -split ':'
$imageName = $imageParts[1]
Publish-Image "$imageName" "${Organization}/${Repository}:${tag}"
if($lastExitCode -ne 0) {
$publishFailed = 1
}
Expand All @@ -233,6 +227,19 @@ if($target -eq "publish") {
}
}

if($target -eq "publish-compose") {
Write-Host "= PUBLISH-COMPOSE: pushing everything"
switch($DryRun) {
$true { Write-Host "$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) {
Write-Error "Build failed!"
} else {
Expand Down

0 comments on commit c706390

Please sign in to comment.