From 5c61d62b57e1058a4f1ea48594601541286e1362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Sat, 8 Jul 2023 19:12:48 +0200 Subject: [PATCH 01/17] Import build.ps1 and build-windows.yaml from docker-agent before recent refactoring --- Jenkinsfile | 167 +++++++++++++---------------- build-windows.yaml | 33 ++++++ build.ps1 | 254 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 357 insertions(+), 97 deletions(-) create mode 100644 build-windows.yaml create mode 100644 build.ps1 diff --git a/Jenkinsfile b/Jenkinsfile index be119b85..6bff728f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,115 +3,88 @@ pipeline { options { buildDiscarder(logRotator(daysToKeepStr: '10')) - timestamps() } stages { - stage('Build') { - parallel { - stage('Windows') { - options { - timeout(time: 60, unit: 'MINUTES') + stage('docker-agent') { + failFast true + matrix { + axes { + axis { + name 'AGENT_TYPE' + values 'linux', 'windows-2019' } - environment { - DOCKERHUB_ORGANISATION = "${infra.isTrusted() ? 'jenkins' : 'jenkins4eval'}" - } - stages { - stage('Build and Test') { - // This stage is the "CI" and should be run on all code changes triggered by a code change - when { - not { buildingTag() } + } + stages { + stage('Main') { + agent { + label env.AGENT_TYPE + } + options { + timeout(time: 30, unit: 'MINUTES') + } + environment { + DOCKERHUB_ORGANISATION = "${infra.isTrusted() ? 'jenkins' : 'jenkins4eval'}" + } + stages { + stage('Prepare Docker') { + when { + environment name: 'AGENT_TYPE', value: 'linux' + } + steps { + sh ''' + docker buildx create --use + docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + ''' + } } - steps { - script { - def parallelBuilds = [:] - def images = ['jdk11-windowsservercore-ltsc2019', 'jdk11-nanoserver-1809', 'jdk17-windowsservercore-ltsc2019', 'jdk17-nanoserver-1809'] - for (unboundImage in images) { - def image = unboundImage // Bind variable before the closure - // Prepare a map of the steps to run in parallel - parallelBuilds[image] = { - // Allocate a node for each image to avoid filling disk - node('docker-windows') { - checkout scm - powershell '& ./make.ps1 -Build ' + image + ' test' - junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/**/junit-results.xml') - } + stage('Build and Test') { + // This stage is the "CI" and should be run on all code changes triggered by a code change + when { + not { buildingTag() } + } + steps { + script { + if(isUnix()) { + sh './build.sh' + sh './build.sh test' + // If the tests are passing for Linux AMD64, then we can build all the CPU architectures + sh 'docker buildx bake --file docker-bake.hcl linux' + } else { + powershell "& ./build.ps1 test" } } - // Peform the parallel execution - parallel parallelBuilds } - } - } - stage('Deploy to DockerHub') { - agent { - label 'docker-windows' - } - // This stage is the "CD" and should only be run when a tag triggered the build - when { - buildingTag() - } - steps { - script { - // This function is defined in the jenkins-infra/pipeline-library - infra.withDockerCredentials { - powershell '& ./make.ps1 -PushVersions -VersionTag $env:TAG_NAME publish' + post { + always { + junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/**/junit-results.xml') } } } - } - } - } - stage('Linux') { - agent { - label "docker&&linux" - } - options { - timeout(time: 30, unit: 'MINUTES') - } - environment { - JENKINS_REPO = "${infra.isTrusted() ? 'jenkins' : 'jenkins4eval'}/inbound-agent" - } - stages { - stage('Prepare Docker') { - steps { - sh ''' - docker buildx create --use - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - ''' - } - } - stage('Build and Test') { - // This stage is the "CI" and should be run on all code changes triggered by a code change - when { - not { buildingTag() } - } - steps { - sh 'make build' - sh 'make test' - // If the tests are passing for Linux AMD64, then we can build all the CPU architectures - sh 'docker buildx bake --file docker-bake.hcl linux' - } - post { - always { - junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/*.xml') + stage('Deploy to DockerHub') { + // This stage is the "CD" and should only be run when a tag triggered the build + when { + buildingTag() } - } - } - stage('Deploy to DockerHub') { - // This stage is the "CD" and should only be run when a tag triggered the build - when { - buildingTag() - } - steps { - script { - // This function is defined in the jenkins-infra/pipeline-library - infra.withDockerCredentials { - sh ''' - export IMAGE_TAG="${TAG_NAME}" - export ON_TAG=true - docker buildx bake --push --file docker-bake.hcl linux - ''' + steps { + script { + def tagItems = env.TAG_NAME.split('-') + if(tagItems.length == 2) { + def remotingVersion = tagItems[0] + def buildNumber = tagItems[1] + // This function is defined in the jenkins-infra/pipeline-library + infra.withDockerCredentials { + if (isUnix()) { + sh """ + docker buildx create --use + docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + ./build.sh -r ${remotingVersion} -b ${buildNumber} -d publish + """ + } else { + powershell "& ./build.ps1 -PushVersions -RemotingVersion $remotingVersion -BuildNumber $buildNumber -DisableEnvProps publish" + } + } + } } } } diff --git a/build-windows.yaml b/build-windows.yaml new file mode 100644 index 00000000..75847993 --- /dev/null +++ b/build-windows.yaml @@ -0,0 +1,33 @@ +services: + jdk11-nanoserver-1809: + image: jdk11-nanoserver-1809 + build: + context: ./windows/nanoserver-1809/ + args: + JAVA_HOME: "C:/openjdk-11" + JAVA_VERSION: "11.0.19_7" + VERSION: ${REMOTING_VERSION} + jdk17-nanoserver-1809: + image: jdk17-nanoserver-1809 + build: + context: ./windows/nanoserver-1809/ + args: + JAVA_HOME: "C:/openjdk-17" + JAVA_VERSION: "17.0.7_7" + VERSION: ${REMOTING_VERSION} + jdk11-windowsservercore-ltsc2019: + image: jdk11-windowsservercore-ltsc2019 + build: + context: ./windows/windowsservercore-ltsc2019/ + args: + JAVA_HOME: "C:/openjdk-11" + JAVA_VERSION: "11.0.19_7" + VERSION: ${REMOTING_VERSION} + jdk17-windowsservercore-ltsc2019: + image: jdk17-windowsservercore-ltsc2019 + build: + context: ./windows/windowsservercore-ltsc2019/ + args: + JAVA_HOME: "C:/openjdk-17" + JAVA_VERSION: "17.0.7_7" + VERSION: ${REMOTING_VERSION} diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 00000000..8db02488 --- /dev/null +++ b/build.ps1 @@ -0,0 +1,254 @@ +[CmdletBinding()] +Param( + [Parameter(Position=1)] + [String] $Target = "build", + [String] $Build = '', + [String] $RemotingVersion = '3131.vf2b_b_798b_ce99', + [String] $BuildNumber = '1', + [switch] $PushVersions = $false, + [switch] $DisableEnvProps = $false +) + +$ErrorActionPreference = "Stop" +$Repository = 'agent' +$Organization = 'jenkins' + +if(!$DisableEnvProps) { + Get-Content env.props | ForEach-Object { + $items = $_.Split("=") + if($items.Length -eq 2) { + $name = $items[0].Trim() + $value = $items[1].Trim() + Set-Item -Path "env:$($name)" -Value $value + } + } +} + +if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO)) { + $Repository = $env:DOCKERHUB_REPO +} + +if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_ORGANISATION)) { + $Organization = $env:DOCKERHUB_ORGANISATION +} + +if(![String]::IsNullOrWhiteSpace($env:REMOTING_VERSION)) { + $RemotingVersion = $env:REMOTING_VERSION +} + +# Check for required commands +Function Test-CommandExists { + # From https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/ + Param ( + [String] $command + ) + + $oldPreference = $ErrorActionPreference + $ErrorActionPreference = 'stop' + try { + if(Get-Command $command){ + Write-Debug "$command exists" + } + } + Catch { + "$command does not exist" + } + Finally { + $ErrorActionPreference=$oldPreference + } +} + +# this is the jdk version that will be used for the 'bare tag' images, e.g., jdk8-windowsservercore-1809 -> windowsserver-1809 +$defaultJdk = '11' +$builds = @{} +$env:REMOTING_VERSION = "$RemotingVersion" +$ProgressPreference = 'SilentlyContinue' # Disable Progress bar for faster downloads + +Test-CommandExists "docker" +Test-CommandExists "docker-compose" +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 = $_ + $items = $image.Split("-") + $jdkMajorVersion = $items[0].Remove(0,3) + $windowsType = $items[1] + $windowsVersion = $items[2] + + $baseImage = "${windowsType}-${windowsVersion}" + $versionTag = "${RemotingVersion}-${BuildNumber}-${image}" + $tags = @( $image, $versionTag ) + if($jdkMajorVersion -eq "$defaultJdk") { + $tags += $baseImage + } + + $builds[$image] = @{ + 'Tags' = $tags; + } +} + +if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) { + Write-Host "= BUILD: Building image ${Build}..." + $dockerBuildCmd = '{0} {1}' -f $baseDockerBuildCmd, $Build + Invoke-Expression $dockerBuildCmd + Write-Host "= BUILD: Finished building image ${Build}" +} else { + Write-Host "= BUILD: Building all images..." + Invoke-Expression $baseDockerBuildCmd + Write-Host "= BUILD: Finished building all image" +} + +if($lastExitCode -ne 0) { + exit $lastExitCode +} + +function Test-Image { + param ( + $ImageName + ) + + Write-Host "= TEST: Testing image ${ImageName}:" + + $env:AGENT_IMAGE = $ImageName + $env:IMAGE_FOLDER = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${ImageName}.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" + } + Remove-Item env:\AGENT_IMAGE + Remove-Item env:\IMAGE_FOLDER + Remove-Item env:\VERSION +} + +if($target -eq "test") { + Write-Host "= TEST: Starting 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..." + $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 -MaximumVersion 5.3.3 + } + + Import-Module 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 + + if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) { + Test-Image $Build + } else { + foreach($image in $builds.Keys) { + Test-Image $image + } + } + + # Fail if any test failures + if($testFailed -ne $false) { + Write-Error "Test stage failed!" + exit 1 + } else { + Write-Host "Test stage passed!" + } +} + +function Publish-Image { + param ( + [String] $Build, + [String] $ImageName + ) + # foreach($tag in $builds[$ImageName]['Tags']) { + # $fullImageName = '{0}/{1}:{2}' -f $Organization, $Repository, $tag + # $cmd = "docker tag {0} {1}" -f $ImageName, $tag + Write-Host "= PUBLISH: Tagging $Build => full name = $ImageName" + docker tag "$Build" "$ImageName" + + Write-Host "= PUBLISH: Publishing $ImageName..." + docker push "$ImageName" +} + + +if($target -eq "publish") { + # Only fail the run afterwards in case of any issues when publishing the docker images + $publishFailed = 0 + if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) { + foreach($tag in $Builds[$Build]['Tags']) { + Publish-Image "$Build" "${Organization}/${Repository}:${tag}" + if($lastExitCode -ne 0) { + $publishFailed = 1 + } + + if($PushVersions) { + $buildTag = "$RemotingVersion-$BuildNumber-$tag" + if($tag -eq 'latest') { + $buildTag = "$RemotingVersion-$BuildNumber" + } + Publish-Image "$Build" "${Organization}/${Repository}:${buildTag}" + if($lastExitCode -ne 0) { + $publishFailed = 1 + } + } + } + } else { + foreach($b in $builds.Keys) { + foreach($tag in $Builds[$b]['Tags']) { + Publish-Image "$b" "${Organization}/${Repository}:${tag}" + if($lastExitCode -ne 0) { + $publishFailed = 1 + } + + if($PushVersions) { + $buildTag = "$RemotingVersion-$BuildNumber-$tag" + if($tag -eq 'latest') { + $buildTag = "$RemotingVersion-$BuildNumber" + } + Publish-Image "$b" "${Organization}/${Repository}:${buildTag}" + if($lastExitCode -ne 0) { + $publishFailed = 1 + } + } + } + } + } + + # Fail if any issues when publising the docker images + if($publishFailed -ne 0) { + Write-Error "Publish failed!" + exit 1 + } +} + +if($lastExitCode -ne 0) { + Write-Error "Build failed!" +} else { + Write-Host "Build finished successfully" +} +exit $lastExitCode From 4cbfbf724fbdb5fd62134322e5abd7341bc533b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Sat, 8 Jul 2023 19:14:30 +0200 Subject: [PATCH 02/17] build Windows images with docker compose like docker-agent and regroup dockerfiles --- Jenkinsfile | 159 ++++++------ build-windows.yaml | 28 +-- build.ps1 | 64 +++-- docker-bake.hcl | 40 +-- alpine/Dockerfile => linux/Dockerfile.alpine | 10 +- debian/Dockerfile => linux/Dockerfile.debian | 10 +- jenkins-agent => linux/jenkins-agent | 0 make.ps1 | 237 ------------------ tests/inboundAgent.Tests.ps1 | 14 +- tests/test_helpers.psm1 | 2 +- tests/tests.bats | 2 +- ...ns-agent-parent.yaml => docker-agent.yaml} | 18 +- .../Dockerfile => Dockerfile.nanoserver} | 8 +- ...ockerfile => Dockerfile.windowsservercore} | 8 +- .../jenkins-agent.ps1 | 0 15 files changed, 192 insertions(+), 408 deletions(-) rename alpine/Dockerfile => linux/Dockerfile.alpine (88%) rename debian/Dockerfile => linux/Dockerfile.debian (63%) rename jenkins-agent => linux/jenkins-agent (100%) delete mode 100644 make.ps1 rename updatecli/updatecli.d/{jenkins-agent-parent.yaml => docker-agent.yaml} (93%) rename windows/{nanoserver-1809/Dockerfile => Dockerfile.nanoserver} (89%) rename windows/{windowsservercore-ltsc2019/Dockerfile => Dockerfile.windowsservercore} (89%) rename jenkins-agent.ps1 => windows/jenkins-agent.ps1 (100%) diff --git a/Jenkinsfile b/Jenkinsfile index 6bff728f..92e0387b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,88 +3,103 @@ pipeline { options { buildDiscarder(logRotator(daysToKeepStr: '10')) + timestamps() } stages { - stage('docker-agent') { - failFast true - matrix { - axes { - axis { - name 'AGENT_TYPE' - values 'linux', 'windows-2019' + stage('Build') { + parallel { + stage('Windows') { + agent { + label "docker-windows" } - } - stages { - stage('Main') { - agent { - label env.AGENT_TYPE - } - options { - timeout(time: 30, unit: 'MINUTES') - } - environment { - DOCKERHUB_ORGANISATION = "${infra.isTrusted() ? 'jenkins' : 'jenkins4eval'}" - } - stages { - stage('Prepare Docker') { - when { - environment name: 'AGENT_TYPE', value: 'linux' - } - steps { - sh ''' - docker buildx create --use - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - ''' - } + options { + timeout(time: 60, unit: 'MINUTES') + } + environment { + DOCKERHUB_ORGANISATION = "${infra.isTrusted() ? 'jenkins' : 'jenkins4eval'}" + } + stages { + stage('Build and Test') { + // This stage is the "CI" and should be run on all code changes triggered by a code change + when { + not { buildingTag() } } - stage('Build and Test') { - // This stage is the "CI" and should be run on all code changes triggered by a code change - when { - not { buildingTag() } - } - steps { - script { - if(isUnix()) { - sh './build.sh' - sh './build.sh test' - // If the tests are passing for Linux AMD64, then we can build all the CPU architectures - sh 'docker buildx bake --file docker-bake.hcl linux' - } else { - powershell "& ./build.ps1 test" - } - } + steps { + powershell '& ./build.ps1 test' + } + post { + always { + junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/**/junit-results.xml') } - post { - always { - junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/**/junit-results.xml') + } + } + stage('Deploy to DockerHub') { + // This stage is the "CD" and should only be run when a tag triggered the build + when { + buildingTag() + } + steps { + script { + infra.withDockerCredentials { + // TODO: check if this function has the same beahvior in build.ps1 vs make.ps1 + powershell '& ./build.ps1 -PushVersions -VersionTag $env:TAG_NAME publish' } } } - stage('Deploy to DockerHub') { - // This stage is the "CD" and should only be run when a tag triggered the build - when { - buildingTag() + } + } + } + stage('Linux') { + agent { + label "docker&&linux" + } + options { + timeout(time: 30, unit: 'MINUTES') + } + environment { + JENKINS_REPO = "${infra.isTrusted() ? 'jenkins' : 'jenkins4eval'}/inbound-agent" + } + stages { + stage('Prepare Docker') { + steps { + sh ''' + docker buildx create --use + docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + ''' + } + } + stage('Build and Test') { + // This stage is the "CI" and should be run on all code changes triggered by a code change + when { + not { buildingTag() } + } + steps { + sh 'make build' + sh 'make test' + // If the tests are passing for Linux AMD64, then we can build all the CPU architectures + sh 'docker buildx bake --file docker-bake.hcl linux' + } + post { + always { + junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/*.xml') } - steps { - script { - def tagItems = env.TAG_NAME.split('-') - if(tagItems.length == 2) { - def remotingVersion = tagItems[0] - def buildNumber = tagItems[1] - // This function is defined in the jenkins-infra/pipeline-library - infra.withDockerCredentials { - if (isUnix()) { - sh """ - docker buildx create --use - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - ./build.sh -r ${remotingVersion} -b ${buildNumber} -d publish - """ - } else { - powershell "& ./build.ps1 -PushVersions -RemotingVersion $remotingVersion -BuildNumber $buildNumber -DisableEnvProps publish" - } - } - } + } + } + stage('Deploy to DockerHub') { + // This stage is the "CD" and should only be run when a tag triggered the build + when { + buildingTag() + } + steps { + script { + // This function is defined in the jenkins-infra/pipeline-library + infra.withDockerCredentials { + sh ''' + export IMAGE_TAG="${TAG_NAME}" + export ON_TAG=true + docker buildx bake --push --file docker-bake.hcl linux + ''' } } } diff --git a/build-windows.yaml b/build-windows.yaml index 75847993..7febdfa5 100644 --- a/build-windows.yaml +++ b/build-windows.yaml @@ -2,32 +2,28 @@ services: jdk11-nanoserver-1809: image: jdk11-nanoserver-1809 build: - context: ./windows/nanoserver-1809/ + context: ./windows/ + dockerfile: Dockerfile.nanoserver args: - JAVA_HOME: "C:/openjdk-11" - JAVA_VERSION: "11.0.19_7" - VERSION: ${REMOTING_VERSION} + VERSION: ${DOCKER_AGENT_VERSION} jdk17-nanoserver-1809: image: jdk17-nanoserver-1809 build: - context: ./windows/nanoserver-1809/ + context: ./windows/ + dockerfile: Dockerfile.nanoserver args: - JAVA_HOME: "C:/openjdk-17" - JAVA_VERSION: "17.0.7_7" - VERSION: ${REMOTING_VERSION} + VERSION: ${DOCKER_AGENT_VERSION} jdk11-windowsservercore-ltsc2019: image: jdk11-windowsservercore-ltsc2019 build: - context: ./windows/windowsservercore-ltsc2019/ + context: ./windows/ + dockerfile: Dockerfile.windowsservercore args: - JAVA_HOME: "C:/openjdk-11" - JAVA_VERSION: "11.0.19_7" - VERSION: ${REMOTING_VERSION} + VERSION: ${DOCKER_AGENT_VERSION} jdk17-windowsservercore-ltsc2019: image: jdk17-windowsservercore-ltsc2019 build: - context: ./windows/windowsservercore-ltsc2019/ + context: ./windows/ + dockerfile: Dockerfile.windowsservercore args: - JAVA_HOME: "C:/openjdk-17" - JAVA_VERSION: "17.0.7_7" - VERSION: ${REMOTING_VERSION} + VERSION: ${DOCKER_AGENT_VERSION} diff --git a/build.ps1 b/build.ps1 index 8db02488..86f8102a 100644 --- a/build.ps1 +++ b/build.ps1 @@ -3,26 +3,28 @@ Param( [Parameter(Position=1)] [String] $Target = "build", [String] $Build = '', - [String] $RemotingVersion = '3131.vf2b_b_798b_ce99', + [String] $DockerAgentVersion = '3131.vf2b_b_798b_ce99-2', [String] $BuildNumber = '1', - [switch] $PushVersions = $false, - [switch] $DisableEnvProps = $false + [switch] $PushVersions = $false + # [switch] $PushVersions = $false, + # [switch] $DisableEnvProps = $false ) $ErrorActionPreference = "Stop" -$Repository = 'agent' +$Repository = 'inbound-agent' $Organization = 'jenkins' -if(!$DisableEnvProps) { - Get-Content env.props | ForEach-Object { - $items = $_.Split("=") - if($items.Length -eq 2) { - $name = $items[0].Trim() - $value = $items[1].Trim() - Set-Item -Path "env:$($name)" -Value $value - } - } -} +# TODO: not needed? Commented for now, env.props contains DOCKER_AGENT_VERSION in docker-agent +# if(!$DisableEnvProps) { +# Get-Content env.props | ForEach-Object { +# $items = $_.Split("=") +# if($items.Length -eq 2) { +# $name = $items[0].Trim() +# $value = $items[1].Trim() +# Set-Item -Path "env:$($name)" -Value $value +# } +# } +# } if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO)) { $Repository = $env:DOCKERHUB_REPO @@ -32,8 +34,8 @@ if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_ORGANISATION)) { $Organization = $env:DOCKERHUB_ORGANISATION } -if(![String]::IsNullOrWhiteSpace($env:REMOTING_VERSION)) { - $RemotingVersion = $env:REMOTING_VERSION +if(![String]::IsNullOrWhiteSpace($env:DOCKER_AGENT_VERSION)) { + $DockerAgentVersion = $env:DOCKER_AGENT_VERSION } # Check for required commands @@ -61,7 +63,7 @@ Function Test-CommandExists { # this is the jdk version that will be used for the 'bare tag' images, e.g., jdk8-windowsservercore-1809 -> windowsserver-1809 $defaultJdk = '11' $builds = @{} -$env:REMOTING_VERSION = "$RemotingVersion" +$env:DOCKER_AGENT_VERSION = "$DockerAgentVersion" $ProgressPreference = 'SilentlyContinue' # Disable Progress bar for faster downloads Test-CommandExists "docker" @@ -74,16 +76,20 @@ $baseDockerBuildCmd = '{0} build --parallel --pull' -f $baseDockerCmd Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object { $image = $_ $items = $image.Split("-") + # Remove the 'jdk' prefix (3 first characters) $jdkMajorVersion = $items[0].Remove(0,3) $windowsType = $items[1] $windowsVersion = $items[2] - + $baseImage = "${windowsType}-${windowsVersion}" - $versionTag = "${RemotingVersion}-${BuildNumber}-${image}" + $versionTag = "${DockerAgentVersion}-${BuildNumber}-${image}" $tags = @( $image, $versionTag ) - if($jdkMajorVersion -eq "$defaultJdk") { - $tags += $baseImage - } + # TODO: keep it here too? (from docker-agent) + # if($jdkMajorVersion -eq "$defaultJdk") { + # $tags += $baseImage + # } + + Write-Host "New Windows image to build ($image): ${Organization}/${Repository}:${baseImage} with JDK ${jdkMajorVersion}" $builds[$image] = @{ 'Tags' = $tags; @@ -114,8 +120,11 @@ function Test-Image { $env:AGENT_IMAGE = $ImageName $env:IMAGE_FOLDER = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${ImageName}.build.context" - $env:VERSION = "$RemotingVersion-$BuildNumber" + # TODO: review build number removal (?) + # $env:VERSION = "$DockerAgentVersion-$BuildNumber" + $env:VERSION = $DockerAgentVersion + Write-Host "= TEST: image folder ${env:IMAGE_FOLDER}, version ${env:VERSION}" if(Test-Path ".\target\$ImageName") { Remove-Item -Recurse -Force ".\target\$ImageName" @@ -180,6 +189,7 @@ if($target -eq "test") { } } +# TODO: dry mode? function Publish-Image { param ( [String] $Build, @@ -207,9 +217,9 @@ if($target -eq "publish") { } if($PushVersions) { - $buildTag = "$RemotingVersion-$BuildNumber-$tag" + $buildTag = "$DockerAgentVersion-$BuildNumber-$tag" if($tag -eq 'latest') { - $buildTag = "$RemotingVersion-$BuildNumber" + $buildTag = "$DockerAgentVersion-$BuildNumber" } Publish-Image "$Build" "${Organization}/${Repository}:${buildTag}" if($lastExitCode -ne 0) { @@ -226,9 +236,9 @@ if($target -eq "publish") { } if($PushVersions) { - $buildTag = "$RemotingVersion-$BuildNumber-$tag" + $buildTag = "$DockerAgentVersion-$BuildNumber-$tag" if($tag -eq 'latest') { - $buildTag = "$RemotingVersion-$BuildNumber" + $buildTag = "$DockerAgentVersion-$BuildNumber" } Publish-Image "$b" "${Organization}/${Repository}:${buildTag}" if($lastExitCode -ne 0) { diff --git a/docker-bake.hcl b/docker-bake.hcl index 362d1d6a..bf6731d7 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -30,8 +30,8 @@ variable "IMAGE_TAG" { default = "3071.v7e9b_0dc08466-1" } -#### This is for the "parent" image version to use (jenkins/agent:-) -variable "PARENT_IMAGE_VERSION" { +#### This is for the "parent" image version to use (jenkins/agent:-) +variable "DOCKER_AGENT_VERSION" { default = "3131.vf2b_b_798b_ce99-4" } @@ -48,15 +48,15 @@ variable "ON_TAG" { } target "alpine_jdk11" { - dockerfile = "alpine/Dockerfile" - context = "." + dockerfile = "Dockerfile.alpine" + context = "./linux/" args = { JAVA_MAJOR_VERSION = "11" - version = "${PARENT_IMAGE_VERSION}" + VERSION = "${DOCKER_AGENT_VERSION}" } tags = [ - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-alpine": "", - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-alpine-jdk11": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}-alpine": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}-alpine-jdk11": "", "${REGISTRY}/${JENKINS_REPO}:alpine", "${REGISTRY}/${JENKINS_REPO}:alpine-jdk11", "${REGISTRY}/${JENKINS_REPO}:latest-alpine", @@ -66,14 +66,14 @@ target "alpine_jdk11" { } target "alpine_jdk17" { - dockerfile = "alpine/Dockerfile" - context = "." + dockerfile = "Dockerfile.alpine" + context = "./linux/" args = { JAVA_MAJOR_VERSION = "17" - version = "${PARENT_IMAGE_VERSION}" + VERSION = "${DOCKER_AGENT_VERSION}" } tags = [ - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-alpine-jdk17": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}-alpine-jdk17": "", "${REGISTRY}/${JENKINS_REPO}:alpine-jdk17", "${REGISTRY}/${JENKINS_REPO}:latest-alpine-jdk17", ] @@ -81,15 +81,15 @@ target "alpine_jdk17" { } target "debian_jdk11" { - dockerfile = "debian/Dockerfile" - context = "." + dockerfile = "Dockerfile.debian" + context = "./linux/" args = { JAVA_MAJOR_VERSION = "11" - version = "${PARENT_IMAGE_VERSION}" + VERSION = "${DOCKER_AGENT_VERSION}" } tags = [ - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}": "", - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-jdk11": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}-jdk11": "", "${REGISTRY}/${JENKINS_REPO}:jdk11", "${REGISTRY}/${JENKINS_REPO}:latest", "${REGISTRY}/${JENKINS_REPO}:latest-jdk11", @@ -98,14 +98,14 @@ target "debian_jdk11" { } target "debian_jdk17" { - dockerfile = "debian/Dockerfile" - context = "." + dockerfile = "Dockerfile.debian" + context = "./linux/" args = { JAVA_MAJOR_VERSION = "17" - version = "${PARENT_IMAGE_VERSION}" + VERSION = "${DOCKER_AGENT_VERSION}" } tags = [ - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-jdk17": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}-jdk17": "", "${REGISTRY}/${JENKINS_REPO}:jdk17", "${REGISTRY}/${JENKINS_REPO}:latest-jdk17", ] diff --git a/alpine/Dockerfile b/linux/Dockerfile.alpine similarity index 88% rename from alpine/Dockerfile rename to linux/Dockerfile.alpine index 28357ca4..ce751cba 100644 --- a/alpine/Dockerfile +++ b/linux/Dockerfile.alpine @@ -21,17 +21,17 @@ # THE SOFTWARE. #TODO(oleg_nenashev): Does it also need an update? -ARG version=3131.vf2b_b_798b_ce99-4 +ARG VERSION=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 -FROM jenkins/agent:"${version}"-alpine-jdk"${JAVA_MAJOR_VERSION}" +FROM jenkins/agent:"${VERSION}"-alpine-jdk"${JAVA_MAJOR_VERSION}" -ARG version=3131.vf2b_b_798b_ce99-4 -LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols" Vendor="Jenkins project" Version="$version" +ARG VERSION=3131.vf2b_b_798b_ce99-4 +LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols" Vendor="Jenkins project" Version="$VERSION" ARG user=jenkins USER root -COPY ../../jenkins-agent /usr/local/bin/jenkins-agent +COPY jenkins-agent /usr/local/bin/jenkins-agent RUN chmod +x /usr/local/bin/jenkins-agent &&\ ln -s /usr/local/bin/jenkins-agent /usr/local/bin/jenkins-slave USER ${user} diff --git a/debian/Dockerfile b/linux/Dockerfile.debian similarity index 63% rename from debian/Dockerfile rename to linux/Dockerfile.debian index 87ae642e..4d90d719 100644 --- a/debian/Dockerfile +++ b/linux/Dockerfile.debian @@ -1,14 +1,14 @@ -ARG version=3131.vf2b_b_798b_ce99-4 +ARG VERSION=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 -FROM jenkins/agent:"${version}"-jdk"${JAVA_MAJOR_VERSION}" +FROM jenkins/agent:"${VERSION}"-jdk"${JAVA_MAJOR_VERSION}" -ARG version=3131.vf2b_b_798b_ce99-4 -LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols" Vendor="Jenkins project" Version="$version" +ARG VERSION=3131.vf2b_b_798b_ce99-4 +LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols" Vendor="Jenkins project" Version="$VERSION" ARG user=jenkins USER root -COPY ../../jenkins-agent /usr/local/bin/jenkins-agent +COPY jenkins-agent /usr/local/bin/jenkins-agent RUN chmod +x /usr/local/bin/jenkins-agent &&\ ln -s /usr/local/bin/jenkins-agent /usr/local/bin/jenkins-slave USER ${user} diff --git a/jenkins-agent b/linux/jenkins-agent similarity index 100% rename from jenkins-agent rename to linux/jenkins-agent diff --git a/make.ps1 b/make.ps1 deleted file mode 100644 index 0253807b..00000000 --- a/make.ps1 +++ /dev/null @@ -1,237 +0,0 @@ -[CmdletBinding()] -Param( - [Parameter(Position=1)] - [String] $Target = "build", - [String] $Build = '', - [String] $VersionTag = '3071.v7e9b_0dc08466-1', - [String] $DockerAgentVersion = '3131.vf2b_b_798b_ce99-4', - [switch] $PushVersions = $false -) - -$Repository = 'inbound-agent' -$Organization = 'jenkins' - -if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO)) { - $Repository = $env:DOCKERHUB_REPO -} - -if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_ORGANISATION)) { - $Organization = $env:DOCKERHUB_ORGANISATION -} - -# this is the jdk version that will be used for the 'bare tag' images, e.g., jdk8-windowsservercore-1809 -> windowsserver-1809 -$defaultBuild = '11' -$builds = @{} - -# TODO: use docker-compose / tooling like with docker-agent -$images = 'jdk11-nanoserver-1809', 'jdk11-windowsservercore-ltsc2019', 'jdk17-nanoserver-1809', 'jdk17-windowsservercore-ltsc2019' -Foreach($image in $images) { - $items = $image.Split('-') - # Remove the 'jdk' prefix (3 first characters) - $jdkMajorVersion = $items[0].Remove(0,3) - $windowsFlavor = $items[1] - $windowsVersion = $items[2] - $baseImage = "${windowsFlavor}-${windowsVersion}" - $dir = "windows/${baseImage}" - - Write-Host "New windows image to build: jenkins/jenkins:${baseImage} with JDK ${jdkMajorVersion} in ${dir}" - - $tags = @( $image ) - if($jdkMajorVersion -eq $defaultBuild) { - $tags += $baseImage - } - - $builds[$image] = @{ - 'Folder' = $dir; - 'Tags' = $tags; - 'JdkMajorVersion' = $jdkMajorVersion; - } -} - -function Build-Image { - param ( - [String] $Build, - [String] $ImageName, - [String] $RemotingVersion, - [String] $JdkMajorVersion, - [String] $Folder - ) - - Write-Host "Building $Build with name $imageName" - docker build --build-arg "version=${RemotingVersion}" --build-arg "JAVA_MAJOR_VERSION=${JdkMajorVersion}" --tag="${ImageName}" --file="${Folder}/Dockerfile" ./ -} - -$exitCodes = 0 -if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) { - foreach($tag in $builds[$Build]['Tags']) { - Build-Image -Build $Build -ImageName "${Organization}/${Repository}:${tag}" -RemotingVersion $DockerAgentVersion -JdkMajorVersion $builds[$Build]['JdkMajorVersion'] -Folder $builds[$Build]['Folder'] - $exitCodes += $lastExitCode - - if($PushVersions) { - $buildTag = "$VersionTag-$tag" - if($tag -eq 'latest') { - $buildTag = "$VersionTag" - } - Build-Image -Build $Build -ImageName "${Organization}/${Repository}:${buildTag}" -RemotingVersion $DockerAgentVersion -JdkMajorVersion $builds[$Build]['JdkMajorVersion'] -Folder $builds[$Build]['Folder'] - $exitCodes += $lastExitCode - } - } -} else { - foreach($b in $builds.Keys) { - foreach($tag in $builds[$b]['Tags']) { - Build-Image -Build $Build -ImageName "${Organization}/${Repository}:${tag}" -RemotingVersion $DockerAgentVersion -JdkMajorVersion $builds[$b]['JdkMajorVersion'] -Folder $builds[$b]['Folder'] - $exitCodes += $lastExitCode - - if($PushVersions) { - $buildTag = "$VersionTag-$tag" - if($tag -eq 'latest') { - $buildTag = "$VersionTag" - } - Build-Image -Build $Build -ImageName "${Organization}/${Repository}:${buildTag}" -RemotingVersion $DockerAgentVersion -JdkMajorVersion $builds[$b]['JdkMajorVersion'] -Folder $builds[$b]['Folder'] - $exitCodes += $lastExitCode - } - } - } -} - -if($exitCodes -ne 0) { - Write-Host "Image build stage failed!" - exit 1 -} else { - Write-Host "Image build stage passed!" -} - -if($Target -eq "test") { - # 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 -MaximumVersion 5.3.3 - } - - Import-Module Pester - $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 - - if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) { - $folder = $builds[$Build]['Folder'] - $env:AGENT_IMAGE = $Build - $env:FOLDER = $folder - $env:JAVA_MAJOR_VERSION = $builds[$Build]['JdkMajorVersion'] - $env:VERSION = $DockerAgentVersion - - if(Test-Path ".\target\$folder") { - Remove-Item -Recurse -Force ".\target\$folder" - } - New-Item -Path ".\target\$folder" -Type Directory | Out-Null - $configuration.TestResult.OutputPath = ".\target\$folder\junit-results.xml" - $TestResults = Invoke-Pester -Configuration $configuration - if ($TestResults.FailedCount -gt 0) { - Write-Host "There were $($TestResults.FailedCount) failed tests in $Build" - $testFailed = $true - } else { - Write-Host "There were $($TestResults.PassedCount) passed tests out of $($TestResults.TotalCount) in $Build" - } - Remove-Item env:\AGENT_IMAGE - Remove-Item env:\FOLDER - Remove-Item env:\JAVA_MAJOR_VERSION - Remove-Item env:\VERSION - } else { - foreach($b in $builds.Keys) { - $folder = $builds[$b]['Folder'] - $env:AGENT_IMAGE = $b - $env:FOLDER = $folder - $env:JAVA_MAJOR_VERSION = $builds[$Build]['JdkMajorVersion'] - $env:VERSION = $DockerAgentVersion - if(Test-Path ".\target\$folder") { - Remove-Item -Recurse -Force ".\target\$folder" - } - New-Item -Path ".\target\$folder" -Type Directory | Out-Null - $configuration.TestResult.OutputPath = ".\target\$folder\junit-results.xml" - $TestResults = Invoke-Pester -Configuration $configuration - if ($TestResults.FailedCount -gt 0) { - Write-Host "There were $($TestResults.FailedCount) failed tests in $Build" - $testFailed = $true - } else { - Write-Host "There were $($TestResults.PassedCount) passed tests out of $($TestResults.TotalCount) in $Build" - } - Remove-Item env:\AGENT_IMAGE - Remove-Item env:\FOLDER - Remove-Item env:\JAVA_MAJOR_VERSION - Remove-Item env:\VERSION - } - } - - # Fail if any test failures - if($testFailed -ne $false) { - Write-Error "Test stage failed!" - exit 1 - } else { - Write-Host "Test stage passed!" - } -} - -$exitCodes = 0 -if($Target -eq "publish") { - if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) { - foreach($tag in $Builds[$Build]['Tags']) { - Write-Host "Publishing $Build => tag=$tag" - $cmd = "docker push {0}/{1}:{2}" -f $Organization, $Repository, $tag - Invoke-Expression $cmd - $exitCodes += $lastExitCode - - if($PushVersions) { - $buildTag = "$VersionTag-$tag" - if($tag -eq 'latest') { - $buildTag = "$VersionTag" - } - Write-Host "Publishing $Build => tag=$buildTag" - $cmd = "docker push {0}/{1}:{2}" -f $Organization, $Repository, $buildTag - Invoke-Expression $cmd - $exitCodes += $lastExitCode - } - } - } else { - foreach($b in $builds.Keys) { - foreach($tag in $Builds[$b]['Tags']) { - Write-Host "Publishing $b => tag=$tag" - $cmd = "docker push {0}/{1}:{2}" -f $Organization, $Repository, $tag - Invoke-Expression $cmd - $exitCodes += $lastExitCode - - if($PushVersions) { - $buildTag = "$VersionTag-$tag" - if($tag -eq 'latest') { - $buildTag = "$VersionTag" - } - Write-Host "Publishing $Build => tag=$buildTag" - $cmd = "docker push {0}/{1}:{2}" -f $Organization, $Repository, $buildTag - Invoke-Expression $cmd - $exitCodes += $lastExitCode - } - } - } - } - - if($exitCodes -ne 0) { - Write-Error "Publish stage failed!" - } else { - Write-Host "Publish stage passed!" - } -} - -exit $exitCodes diff --git a/tests/inboundAgent.Tests.ps1 b/tests/inboundAgent.Tests.ps1 index 4ac1989b..dd94bb32 100644 --- a/tests/inboundAgent.Tests.ps1 +++ b/tests/inboundAgent.Tests.ps1 @@ -1,19 +1,19 @@ Import-Module -DisableNameChecking -Force $PSScriptRoot/test_helpers.psm1 $global:AGENT_IMAGE = Get-EnvOrDefault 'AGENT_IMAGE' '' -$global:FOLDER = Get-EnvOrDefault 'FOLDER' '' -$global:JAVA_MAJOR_VERSION = Get-EnvOrDefault 'JAVA_MAJOR_VERSION' '' +$global:IMAGE_FOLDER = Get-EnvOrDefault 'IMAGE_FOLDER' '' $global:VERSION = Get-EnvOrDefault 'VERSION' '' # TODO: make this name unique for concurency $global:CONTAINERNAME = 'pester-jenkins-inbound-agent-{0}' -f $global:AGENT_IMAGE -$REAL_FOLDER=Resolve-Path -Path "$PSScriptRoot/../${global:FOLDER}" +# TODO: delete as not used? +# $REAL_IMAGE_FOLDER=Resolve-Path -Path "$PSScriptRoot/../${global:IMAGE_FOLDER}" $items = $global:AGENT_IMAGE.Split("-") # Remove the 'jdk' prefix (3 first characters) -$global:JDKMAJORVERSION = $items[0].Remove(0,3) +$global:JAVA_MAJOR_VERSION = $items[0].Remove(0,3) $global:WINDOWSFLAVOR = $items[1] $global:WINDOWSVERSION = $items[2] @@ -31,7 +31,7 @@ BuildNcatImage Describe "[$global:AGENT_IMAGE] build image" { It 'builds image' { - $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg version=${global:VERSION} --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file $global:FOLDER/Dockerfile ./" + $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${global:VERSION} --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file ${global:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}" $exitCode | Should -Be 0 } } @@ -122,7 +122,7 @@ Describe "[$global:AGENT_IMAGE] custom build args" { } It 'builds image with arguments' { - $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg version=${ARG_TEST_VERSION} --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=${global:FOLDER}/Dockerfile ./" + $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${ARG_TEST_VERSION} --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=${global:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}" $exitCode | Should -Be 0 $exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --name $global:CONTAINERNAME $customImageName -Cmd $global:CONTAINERSHELL" @@ -165,7 +165,7 @@ Describe "[$global:AGENT_IMAGE] passing JVM options (slow test)" { Is-ContainerRunning $global:CONTAINERNAME | Should -BeTrue $exitCode, $stdout, $stderr = Run-Program 'docker' "logs $global:CONTAINERNAME" $exitCode | Should -Be 0 - $stdout | Should -Match "OpenJDK Runtime Environment Temurin-${global:JDKMAJORVERSION}" + $stdout | Should -Match "OpenJDK Runtime Environment Temurin-${global:JAVA_MAJOR_VERSION}" } AfterAll { diff --git a/tests/test_helpers.psm1 b/tests/test_helpers.psm1 index 69395da0..bbc82156 100644 --- a/tests/test_helpers.psm1 +++ b/tests/test_helpers.psm1 @@ -114,7 +114,7 @@ function Run-Program($cmd, $params, $quiet=$false, $debug=$false) { $stderr = $proc.StandardError.ReadToEnd() $proc.WaitForExit() if(($proc.ExitCode -ne 0) -and (-not $quiet)) { - Write-Host "`n`nstdout:`n$stdout`n`nstderr:`n$stderr`n`n" + Write-Host "`n`nstdout:`n$stdout`n`nstderr:`n$stderr`n`n`cmd:`n$cmd`n`nparams:`n$params`n`n" } return $proc.ExitCode, $stdout, $stderr diff --git a/tests/tests.bats b/tests/tests.bats index fd4633e9..8dc4abcd 100755 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -58,7 +58,7 @@ SUT_IMAGE="$(get_sut_image)" sut_image="${SUT_IMAGE}-tests-${BATS_TEST_NUMBER}" docker buildx bake \ - --set "${IMAGE}".args.version="${ARG_TEST_VERSION}" \ + --set "${IMAGE}".args.VERSION="${ARG_TEST_VERSION}" \ --set "${IMAGE}".args.user="${TEST_USER}" \ --set "${IMAGE}".platform=linux/"${ARCH}" \ --set "${IMAGE}".tags="${sut_image}" \ diff --git a/updatecli/updatecli.d/jenkins-agent-parent.yaml b/updatecli/updatecli.d/docker-agent.yaml similarity index 93% rename from updatecli/updatecli.d/jenkins-agent-parent.yaml rename to updatecli/updatecli.d/docker-agent.yaml index 38ca58a5..f0fe347b 100644 --- a/updatecli/updatecli.d/jenkins-agent-parent.yaml +++ b/updatecli/updatecli.d/docker-agent.yaml @@ -104,7 +104,7 @@ targets: name: Bump the parent image `jenkins/agent` version on JDK11 Alpine kind: dockerfile spec: - file: alpine/Dockerfile + file: linux/Dockerfile.alpine instruction: keyword: ARG matcher: version @@ -113,7 +113,7 @@ targets: name: Bump the parent image `jenkins/agent` version on JDK11 Debian kind: dockerfile spec: - file: debian/Dockerfile + file: linux/Dockerfile.debian instruction: keyword: ARG matcher: version @@ -122,7 +122,7 @@ targets: name: Bump the parent image `jenkins/agent` version on JDK11 Windows Nanoserver 1809 kind: dockerfile spec: - file: windows/nanoserver-1809/Dockerfile + file: windows/Dockerfile.nanoserver instruction: keyword: ARG matcher: version @@ -131,7 +131,7 @@ targets: name: Bump the parent image `jenkins/agent` version on JDK11 Windows Server 2019 kind: dockerfile spec: - file: windows/windowsservercore-ltsc2019/Dockerfile + file: windows/windowsservercore instruction: keyword: ARG matcher: version @@ -140,7 +140,7 @@ targets: name: Bump the parent image `jenkins/agent` version on JDK17 Alpine kind: dockerfile spec: - file: alpine/Dockerfile + file: linux/Dockerfile.alpine instruction: keyword: ARG matcher: version @@ -149,7 +149,7 @@ targets: name: Bump the parent image `jenkins/agent` version on JDK17 Debian kind: dockerfile spec: - file: debian/Dockerfile + file: linux/Dockerfile.debian instruction: keyword: ARG matcher: version @@ -158,7 +158,7 @@ targets: name: Bump the parent image `jenkins/agent` version on JDK17 Windows Nanoserver 1809 kind: dockerfile spec: - file: windows/nanoserver-1809/Dockerfile + file: windows/windowsservercore instruction: keyword: ARG matcher: version @@ -178,9 +178,9 @@ targets: spec: file: docker-bake.hcl matchpattern: >- - variable(.*)"PARENT_IMAGE_VERSION"(.*){(.*)(\r\n|\r|\n)(.*)default(.*)=(.*) + variable(.*)"DOCKER_AGENT_VERSION"(.*){(.*)(\r\n|\r|\n)(.*)default(.*)=(.*) replacepattern: >- - variable${1}"PARENT_IMAGE_VERSION"${2}{${3}${4}${5}default${6}= "{{ source "lastVersion" }}" + variable${1}"DOCKER_AGENT_VERSION"${2}{${3}${4}${5}default${6}= "{{ source "lastVersion" }}" scmid: default setWindowsMakePwshParentImage: name: Bump the parent image `jenkins/agent` version on the windows make.ps1 file diff --git a/windows/nanoserver-1809/Dockerfile b/windows/Dockerfile.nanoserver similarity index 89% rename from windows/nanoserver-1809/Dockerfile rename to windows/Dockerfile.nanoserver index ff455861..816813ec 100644 --- a/windows/nanoserver-1809/Dockerfile +++ b/windows/Dockerfile.nanoserver @@ -21,12 +21,12 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -ARG version=3131.vf2b_b_798b_ce99-4 +ARG VERSION=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 -FROM jenkins/agent:"${version}"-jdk"${JAVA_MAJOR_VERSION}"-nanoserver-1809 +FROM jenkins/agent:"${VERSION}"-jdk"${JAVA_MAJOR_VERSION}"-nanoserver-1809 -ARG version=3131.vf2b_b_798b_ce99-4 -LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols on Windows" Vendor="Jenkins Project" Version="$version" +ARG VERSION=3131.vf2b_b_798b_ce99-4 +LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols on Windows" Vendor="Jenkins Project" Version="$VERSION" COPY jenkins-agent.ps1 C:/ProgramData/Jenkins ENTRYPOINT ["pwsh.exe", "-f", "C:/ProgramData/Jenkins/jenkins-agent.ps1"] diff --git a/windows/windowsservercore-ltsc2019/Dockerfile b/windows/Dockerfile.windowsservercore similarity index 89% rename from windows/windowsservercore-ltsc2019/Dockerfile rename to windows/Dockerfile.windowsservercore index f97fd2c5..38f367b7 100644 --- a/windows/windowsservercore-ltsc2019/Dockerfile +++ b/windows/Dockerfile.windowsservercore @@ -21,12 +21,12 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -ARG version=3131.vf2b_b_798b_ce99-4 +ARG VERSION=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 -FROM jenkins/agent:"${version}"-jdk"${JAVA_MAJOR_VERSION}"-windowsservercore-ltsc2019 +FROM jenkins/agent:"${VERSION}"-jdk"${JAVA_MAJOR_VERSION}"-windowsservercore-ltsc2019 -ARG version=3131.vf2b_b_798b_ce99-4 -LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols on Windows" Vendor="Jenkins Project" Version="$version" +ARG VERSION=3131.vf2b_b_798b_ce99-4 +LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols on Windows" Vendor="Jenkins Project" Version="$VERSION" COPY jenkins-agent.ps1 C:/ProgramData/Jenkins ENTRYPOINT ["powershell.exe", "-f", "C:/ProgramData/Jenkins/jenkins-agent.ps1"] diff --git a/jenkins-agent.ps1 b/windows/jenkins-agent.ps1 similarity index 100% rename from jenkins-agent.ps1 rename to windows/jenkins-agent.ps1 From 6b35c967866ca4a951323a431ec7993333d8e163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Sun, 9 Jul 2023 11:50:52 +0200 Subject: [PATCH 03/17] refactor builds like https://github.com/jenkinsci/docker-agent/pull/445 --- Jenkinsfile | 153 ++++++++++-------------- build-windows.yaml | 20 ++-- build.ps1 | 30 ++++- linux/Dockerfile.alpine | 1 - tests/inboundAgent.Tests.ps1 | 18 +-- tests/netcat-helper/Dockerfile-windows | 3 +- tests/test_helpers.psm1 | 8 +- tests/tests.bats | 4 +- updatecli/updatecli.d/docker-agent.yaml | 58 ++------- windows/Dockerfile.nanoserver | 3 +- windows/Dockerfile.windowsservercore | 3 +- 11 files changed, 135 insertions(+), 166 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 92e0387b..58801b65 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,103 +3,82 @@ pipeline { options { buildDiscarder(logRotator(daysToKeepStr: '10')) - timestamps() } stages { - stage('Build') { - parallel { - stage('Windows') { - agent { - label "docker-windows" + stage('docker-inbound-agent') { + failFast true + matrix { + axes { + axis { + name 'AGENT_TYPE' + values 'linux', 'windows-2019' } - options { - timeout(time: 60, unit: 'MINUTES') - } - environment { - DOCKERHUB_ORGANISATION = "${infra.isTrusted() ? 'jenkins' : 'jenkins4eval'}" - } - stages { - stage('Build and Test') { - // This stage is the "CI" and should be run on all code changes triggered by a code change - when { - not { buildingTag() } - } - steps { - powershell '& ./build.ps1 test' - } - post { - always { - junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/**/junit-results.xml') - } - } + } + stages { + stage('Main') { + agent { + label env.AGENT_TYPE } - stage('Deploy to DockerHub') { - // This stage is the "CD" and should only be run when a tag triggered the build - when { - buildingTag() - } - steps { - script { - infra.withDockerCredentials { - // TODO: check if this function has the same beahvior in build.ps1 vs make.ps1 - powershell '& ./build.ps1 -PushVersions -VersionTag $env:TAG_NAME publish' - } - } - } + options { + timeout(time: 30, unit: 'MINUTES') } - } - } - stage('Linux') { - agent { - label "docker&&linux" - } - options { - timeout(time: 30, unit: 'MINUTES') - } - environment { - JENKINS_REPO = "${infra.isTrusted() ? 'jenkins' : 'jenkins4eval'}/inbound-agent" - } - stages { - stage('Prepare Docker') { - steps { - sh ''' - docker buildx create --use - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes - ''' - } + environment { + DOCKERHUB_ORGANISATION = "${infra.isTrusted() ? 'jenkins' : 'jenkins4eval'}" } - stage('Build and Test') { - // This stage is the "CI" and should be run on all code changes triggered by a code change - when { - not { buildingTag() } - } - steps { - sh 'make build' - sh 'make test' - // If the tests are passing for Linux AMD64, then we can build all the CPU architectures - sh 'docker buildx bake --file docker-bake.hcl linux' - } - post { - always { - junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/*.xml') + stages { + stage('Prepare Docker') { + when { + environment name: 'AGENT_TYPE', value: 'linux' + } + steps { + sh ''' + docker buildx create --use + docker run --rm --privileged multiarch/qemu-user-static --reset -p yes + ''' } } - } - stage('Deploy to DockerHub') { - // This stage is the "CD" and should only be run when a tag triggered the build - when { - buildingTag() + stage('Build and Test') { + // This stage is the "CI" and should be run on all code changes triggered by a code change + when { + not { buildingTag() } + } + steps { + script { + if(isUnix()) { + sh 'make build' + sh 'make test' + // If the tests are passing for Linux AMD64, then we can build all the CPU architectures + sh 'docker buildx bake --file docker-bake.hcl linux' + } else { + powershell "& ./build.ps1 test" + } + } + } + post { + always { + junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/**/junit-results.xml') + } + } } - steps { - script { - // This function is defined in the jenkins-infra/pipeline-library - infra.withDockerCredentials { - sh ''' - export IMAGE_TAG="${TAG_NAME}" - export ON_TAG=true - docker buildx bake --push --file docker-bake.hcl linux - ''' + stage('Deploy to DockerHub') { + // This stage is the "CD" and should only be run when a tag triggered the build + when { + buildingTag() + } + steps { + script { + infra.withDockerCredentials { + if (isUnix()) { + sh ''' + export IMAGE_TAG="${TAG_NAME}" + export ON_TAG=true + docker buildx bake --push --file docker-bake.hcl linux + ''' + } else { + powershell "& ./build.ps1 -PushVersions -VersionTag $env:TAG_NAME publish" + } + } } } } diff --git a/build-windows.yaml b/build-windows.yaml index 7febdfa5..a84aef9f 100644 --- a/build-windows.yaml +++ b/build-windows.yaml @@ -1,29 +1,33 @@ services: - jdk11-nanoserver-1809: - image: jdk11-nanoserver-1809 + jdk11-nanoserver: + image: jdk11-nanoserver-${NANOSERVER_VERSION_NAME} build: context: ./windows/ dockerfile: Dockerfile.nanoserver args: VERSION: ${DOCKER_AGENT_VERSION} - jdk17-nanoserver-1809: - image: jdk17-nanoserver-1809 + WINDOWS_VERSION_TAG: ${NANOSERVER_VERSION_TAG} + jdk17-nanoserver: + image: jdk17-nanoserver-${NANOSERVER_VERSION_NAME} build: context: ./windows/ dockerfile: Dockerfile.nanoserver args: VERSION: ${DOCKER_AGENT_VERSION} - jdk11-windowsservercore-ltsc2019: - image: jdk11-windowsservercore-ltsc2019 + WINDOWS_VERSION_TAG: ${NANOSERVER_VERSION_TAG} + jdk11-windowsservercore: + image: jdk11-windowsservercore-${WINDOWS_VERSION_NAME} build: context: ./windows/ dockerfile: Dockerfile.windowsservercore args: VERSION: ${DOCKER_AGENT_VERSION} - jdk17-windowsservercore-ltsc2019: - image: jdk17-windowsservercore-ltsc2019 + WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG} + jdk17-windowsservercore: + image: jdk17-windowsservercore-${WINDOWS_VERSION_NAME} build: context: ./windows/ dockerfile: Dockerfile.windowsservercore args: VERSION: ${DOCKER_AGENT_VERSION} + WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG} diff --git a/build.ps1 b/build.ps1 index 86f8102a..032a6bae 100644 --- a/build.ps1 +++ b/build.ps1 @@ -3,16 +3,17 @@ Param( [Parameter(Position=1)] [String] $Target = "build", [String] $Build = '', - [String] $DockerAgentVersion = '3131.vf2b_b_798b_ce99-2', + [String] $DockerAgentVersion = '3131.vf2b_b_798b_ce99-4', [String] $BuildNumber = '1', [switch] $PushVersions = $false # [switch] $PushVersions = $false, # [switch] $DisableEnvProps = $false ) -$ErrorActionPreference = "Stop" +$ErrorActionPreference ='Stop' $Repository = 'inbound-agent' $Organization = 'jenkins' +$AgentType = 'windows-2019' # TODO: not needed? Commented for now, env.props contains DOCKER_AGENT_VERSION in docker-agent # if(!$DisableEnvProps) { @@ -38,6 +39,10 @@ if(![String]::IsNullOrWhiteSpace($env:DOCKER_AGENT_VERSION)) { $DockerAgentVersion = $env:DOCKER_AGENT_VERSION } +if(![String]::IsNullOrWhiteSpace($env:AGENT_TYPE)) { + $AgentType = $env:AGENT_TYPE +} + # Check for required commands Function Test-CommandExists { # From https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/ @@ -60,10 +65,19 @@ Function Test-CommandExists { } } -# this is the jdk version that will be used for the 'bare tag' images, e.g., jdk8-windowsservercore-1809 -> windowsserver-1809 -$defaultJdk = '11' +# # this is the jdk version that will be used for the 'bare tag' images, e.g., jdk8-windowsservercore-1809 -> windowsserver-1809 +# $defaultJdk = '11' $builds = @{} $env:DOCKER_AGENT_VERSION = "$DockerAgentVersion" +$env:WINDOWS_VERSION_NAME = $AgentType.replace('windows-', 'ltsc') +$env:NANOSERVER_VERSION_NAME = $env:WINDOWS_VERSION_NAME +$env:WINDOWS_VERSION_TAG = $env:WINDOWS_VERSION_NAME +$env:NANOSERVER_VERSION_TAG = $env:WINDOWS_VERSION_NAME +# We need to keep the `jdkN-nanoserver-1809` images for now, cf https://github.com/jenkinsci/docker-agent/issues/451 +if ($AgentType -eq 'windows-2019') { + $env:NANOSERVER_VERSION_TAG = 1809 + $env:NANOSERVER_VERSION_NAME = 1809 +} $ProgressPreference = 'SilentlyContinue' # Disable Progress bar for faster downloads Test-CommandExists "docker" @@ -74,7 +88,9 @@ $baseDockerCmd = 'docker-compose --file=build-windows.yaml' $baseDockerBuildCmd = '{0} build --parallel --pull' -f $baseDockerCmd Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object { - $image = $_ + $image = '{0}-{1}' -f $_, $env:WINDOWS_VERSION_NAME + # Special case for nanoserver-1809 images + $image = $image.replace('nanoserver-ltsc2019', 'nanoserver-1809') $items = $image.Split("-") # Remove the 'jdk' prefix (3 first characters) $jdkMajorVersion = $items[0].Remove(0,3) @@ -119,7 +135,8 @@ function Test-Image { Write-Host "= TEST: Testing image ${ImageName}:" $env:AGENT_IMAGE = $ImageName - $env:IMAGE_FOLDER = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${ImageName}.build.context" + $serviceName = $ImageName.SubString(0, $ImageName.LastIndexOf('-')) + $env:IMAGE_FOLDER = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${serviceName}.build.context" # TODO: review build number removal (?) # $env:VERSION = "$DockerAgentVersion-$BuildNumber" $env:VERSION = $DockerAgentVersion @@ -175,6 +192,7 @@ if($target -eq "test") { if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) { Test-Image $Build } else { + Write-Host "= TEST: Testing all images" foreach($image in $builds.Keys) { Test-Image $image } diff --git a/linux/Dockerfile.alpine b/linux/Dockerfile.alpine index ce751cba..a3c49ed1 100644 --- a/linux/Dockerfile.alpine +++ b/linux/Dockerfile.alpine @@ -20,7 +20,6 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -#TODO(oleg_nenashev): Does it also need an update? ARG VERSION=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 FROM jenkins/agent:"${VERSION}"-alpine-jdk"${JAVA_MAJOR_VERSION}" diff --git a/tests/inboundAgent.Tests.ps1 b/tests/inboundAgent.Tests.ps1 index dd94bb32..390ca48a 100644 --- a/tests/inboundAgent.Tests.ps1 +++ b/tests/inboundAgent.Tests.ps1 @@ -3,13 +3,11 @@ Import-Module -DisableNameChecking -Force $PSScriptRoot/test_helpers.psm1 $global:AGENT_IMAGE = Get-EnvOrDefault 'AGENT_IMAGE' '' $global:IMAGE_FOLDER = Get-EnvOrDefault 'IMAGE_FOLDER' '' $global:VERSION = Get-EnvOrDefault 'VERSION' '' +$global:WINDOWS_VERSION_TAG = Get-EnvOrDefault 'WINDOWS_VERSION_TAG' '' # TODO: make this name unique for concurency $global:CONTAINERNAME = 'pester-jenkins-inbound-agent-{0}' -f $global:AGENT_IMAGE -# TODO: delete as not used? -# $REAL_IMAGE_FOLDER=Resolve-Path -Path "$PSScriptRoot/../${global:IMAGE_FOLDER}" - $items = $global:AGENT_IMAGE.Split("-") # Remove the 'jdk' prefix (3 first characters) @@ -20,6 +18,10 @@ $global:WINDOWSVERSION = $items[2] $global:CONTAINERSHELL="powershell.exe" if($global:WINDOWSFLAVOR -eq 'nanoserver') { $global:CONTAINERSHELL = "pwsh.exe" + # Special case for nanoserver-1809 + if($global:WINDOWSVERSION -eq '1809') { + $global:WINDOWS_VERSION_TAG = '1809' + } } @@ -27,11 +29,11 @@ Cleanup($global:CONTAINERNAME) Cleanup("nmap") CleanupNetwork("jnlp-network") -BuildNcatImage +BuildNcatImage($global:WINDOWS_VERSION_TAG) Describe "[$global:AGENT_IMAGE] build image" { It 'builds image' { - $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${global:VERSION} --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file ${global:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}" + $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${global:VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file ${global:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}" $exitCode | Should -Be 0 } } @@ -115,14 +117,14 @@ Describe "[$global:AGENT_IMAGE] custom build args" { Push-Location -StackName 'agent' -Path "$PSScriptRoot/.." # Old version used to test overriding the build arguments. # This old version must have the same tag suffixes as the current 4 windows images (`-jdk11-nanoserver` etc.) - $TEST_VERSION = "3046.v38db_38a_b_7a_86" - $DOCKER_AGENT_VERSION_SUFFIX = "1" + $TEST_VERSION = "3131.vf2b_b_798b_ce99" + $DOCKER_AGENT_VERSION_SUFFIX = "4" $ARG_TEST_VERSION = "${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}" $customImageName = "custom-${global:AGENT_IMAGE}" } It 'builds image with arguments' { - $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${ARG_TEST_VERSION} --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=${global:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}" + $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${ARG_TEST_VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=${global:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}" $exitCode | Should -Be 0 $exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --name $global:CONTAINERNAME $customImageName -Cmd $global:CONTAINERSHELL" diff --git a/tests/netcat-helper/Dockerfile-windows b/tests/netcat-helper/Dockerfile-windows index d42d5244..e8e18811 100644 --- a/tests/netcat-helper/Dockerfile-windows +++ b/tests/netcat-helper/Dockerfile-windows @@ -22,7 +22,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -FROM mcr.microsoft.com/windows/servercore:1809 +ARG WINDOWS_VERSION_TAG=1809 +FROM mcr.microsoft.com/windows/servercore:"${WINDOWS_VERSION_TAG}" SHELL ["powershell.exe", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"] diff --git a/tests/test_helpers.psm1 b/tests/test_helpers.psm1 index bbc82156..5edb8067 100644 --- a/tests/test_helpers.psm1 +++ b/tests/test_helpers.psm1 @@ -120,13 +120,13 @@ function Run-Program($cmd, $params, $quiet=$false, $debug=$false) { return $proc.ExitCode, $stdout, $stderr } -function BuildNcatImage() { - Write-Host "Building nmap image for testing" +function BuildNcatImage($windowsVersionTag) { + Write-Host "Building nmap image (Windows version '${windowsVersionTag}') for testing" $exitCode, $stdout, $stderr = Run-Program 'docker.exe' "inspect --type=image nmap" $true if($exitCode -ne 0) { Push-Location -StackName 'agent' -Path "$PSScriptRoot/.." - $exitCode, $stdout, $stderr = Run-Program 'docker.exe' "build -t nmap -f ./tests/netcat-helper/Dockerfile-windows ./tests/netcat-helper" + $exitCode, $stdout, $stderr = Run-Program 'docker.exe' "build -t nmap --build-arg `"WINDOWS_VERSION_TAG=${windowsVersionTag}`" -f ./tests/netcat-helper/Dockerfile-windows ./tests/netcat-helper" $exitCode | Should -Be 0 Pop-Location -StackName 'agent' } -} \ No newline at end of file +} diff --git a/tests/tests.bats b/tests/tests.bats index 8dc4abcd..5cbb2a23 100755 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -49,8 +49,8 @@ SUT_IMAGE="$(get_sut_image)" # Old version used to test overriding the build arguments. # This old version must have the same tag suffixes as the ones defined in the docker-bake file (`-jdk17`, `jdk11`, etc.) - TEST_VERSION="3046.v38db_38a_b_7a_86" - DOCKER_AGENT_VERSION_SUFFIX="1" + TEST_VERSION="3131.vf2b_b_798b_ce99" + DOCKER_AGENT_VERSION_SUFFIX="4" ARG_TEST_VERSION="${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}" TEST_USER="root" diff --git a/updatecli/updatecli.d/docker-agent.yaml b/updatecli/updatecli.d/docker-agent.yaml index f0fe347b..7a12a433 100644 --- a/updatecli/updatecli.d/docker-agent.yaml +++ b/updatecli/updatecli.d/docker-agent.yaml @@ -100,8 +100,8 @@ conditions: tag: '{{source "lastVersion" }}-jdk17-windowsservercore-ltsc2019' targets: - setJdk11AlpineDockerImage: - name: Bump the parent image `jenkins/agent` version on JDK11 Alpine + setAlpineDockerImage: + name: Bump the parent image `jenkins/agent` version on Alpine kind: dockerfile spec: file: linux/Dockerfile.alpine @@ -109,8 +109,8 @@ targets: keyword: ARG matcher: version scmid: default - setJdk11DebianDockerImage: - name: Bump the parent image `jenkins/agent` version on JDK11 Debian + setDebianDockerImage: + name: Bump the parent image `jenkins/agent` version on Debian kind: dockerfile spec: file: linux/Dockerfile.debian @@ -118,8 +118,8 @@ targets: keyword: ARG matcher: version scmid: default - setJdk11WindowsNanoserver1809DockerImage: - name: Bump the parent image `jenkins/agent` version on JDK11 Windows Nanoserver 1809 + setsNanoserverDockerImage: + name: Bump the parent image `jenkins/agent` version on Windows Nanoserver kind: dockerfile spec: file: windows/Dockerfile.nanoserver @@ -127,8 +127,8 @@ targets: keyword: ARG matcher: version scmid: default - setJdk11WindowsServer2019DockerImage: - name: Bump the parent image `jenkins/agent` version on JDK11 Windows Server 2019 + setWindowsServerCoreDockerImage: + name: Bump the parent image `jenkins/agent` version on Windows Server Core kind: dockerfile spec: file: windows/windowsservercore @@ -136,42 +136,6 @@ targets: keyword: ARG matcher: version scmid: default - setJdk17AlpineDockerImage: - name: Bump the parent image `jenkins/agent` version on JDK17 Alpine - kind: dockerfile - spec: - file: linux/Dockerfile.alpine - instruction: - keyword: ARG - matcher: version - scmid: default - setJdk17DebianDockerImage: - name: Bump the parent image `jenkins/agent` version on JDK17 Debian - kind: dockerfile - spec: - file: linux/Dockerfile.debian - instruction: - keyword: ARG - matcher: version - scmid: default - setJdk17WindowsNanoserver1809DockerImage: - name: Bump the parent image `jenkins/agent` version on JDK17 Windows Nanoserver 1809 - kind: dockerfile - spec: - file: windows/windowsservercore - instruction: - keyword: ARG - matcher: version - scmid: default - setJdk17WindowsServer2019DockerImage: - name: Bump the parent image `jenkins/agent` version on JDK17 Windows Server 2019 - kind: dockerfile - spec: - file: windows/windowsservercore-ltsc2019/Dockerfile - instruction: - keyword: ARG - matcher: version - scmid: default setDockerBakeDefaultParentImage: name: Bump the parent image `jenkins/agent` version on the docker-bake.hcl file kind: file @@ -182,11 +146,11 @@ targets: replacepattern: >- variable${1}"DOCKER_AGENT_VERSION"${2}{${3}${4}${5}default${6}= "{{ source "lastVersion" }}" scmid: default - setWindowsMakePwshParentImage: - name: Bump the parent image `jenkins/agent` version on the windows make.ps1 file + setWindowsBuildPwshParentImage: + name: Bump the parent image `jenkins/agent` version on the Windows build.ps1 powershell script kind: file spec: - file: make.ps1 + file: build.ps1 matchpattern: >- \$DockerAgentVersion(.*)=(.*), replacepattern: >- diff --git a/windows/Dockerfile.nanoserver b/windows/Dockerfile.nanoserver index 816813ec..82015846 100644 --- a/windows/Dockerfile.nanoserver +++ b/windows/Dockerfile.nanoserver @@ -23,7 +23,8 @@ ARG VERSION=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 -FROM jenkins/agent:"${VERSION}"-jdk"${JAVA_MAJOR_VERSION}"-nanoserver-1809 +ARG WINDOWS_VERSION_TAG=1809 +FROM jenkins/agent:"${VERSION}"-jdk"${JAVA_MAJOR_VERSION}"-nanoserver-"${WINDOWS_VERSION_TAG}" ARG VERSION=3131.vf2b_b_798b_ce99-4 LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols on Windows" Vendor="Jenkins Project" Version="$VERSION" diff --git a/windows/Dockerfile.windowsservercore b/windows/Dockerfile.windowsservercore index 38f367b7..8f86103d 100644 --- a/windows/Dockerfile.windowsservercore +++ b/windows/Dockerfile.windowsservercore @@ -23,7 +23,8 @@ ARG VERSION=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 -FROM jenkins/agent:"${VERSION}"-jdk"${JAVA_MAJOR_VERSION}"-windowsservercore-ltsc2019 +ARG WINDOWS_VERSION_TAG=ltsc2019 +FROM jenkins/agent:"${VERSION}"-jdk"${JAVA_MAJOR_VERSION}"-windowsservercore-"${WINDOWS_VERSION_TAG}" ARG VERSION=3131.vf2b_b_798b_ce99-4 LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols on Windows" Vendor="Jenkins Project" Version="$VERSION" From 7f32ea1102056e778d06a34ceb2a5c57e7d159b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 15:14:48 +0200 Subject: [PATCH 04/17] revert images regroupment by OS type --- linux/Dockerfile.alpine => alpine/Dockerfile | 0 build-windows.yaml | 16 ++++++++-------- build.ps1 | 6 +++--- linux/Dockerfile.debian => debian/Dockerfile | 0 docker-bake.hcl | 16 ++++++++-------- tests/inboundAgent.Tests.ps1 | 8 +++++--- updatecli/updatecli.d/docker-agent.yaml | 8 ++++---- .../Dockerfile} | 0 .../Dockerfile} | 0 9 files changed, 28 insertions(+), 26 deletions(-) rename linux/Dockerfile.alpine => alpine/Dockerfile (100%) rename linux/Dockerfile.debian => debian/Dockerfile (100%) rename windows/{Dockerfile.nanoserver => nanoserver/Dockerfile} (100%) rename windows/{Dockerfile.windowsservercore => windowsservercore/Dockerfile} (100%) diff --git a/linux/Dockerfile.alpine b/alpine/Dockerfile similarity index 100% rename from linux/Dockerfile.alpine rename to alpine/Dockerfile diff --git a/build-windows.yaml b/build-windows.yaml index a84aef9f..6839585b 100644 --- a/build-windows.yaml +++ b/build-windows.yaml @@ -2,32 +2,32 @@ services: jdk11-nanoserver: image: jdk11-nanoserver-${NANOSERVER_VERSION_NAME} build: - context: ./windows/ - dockerfile: Dockerfile.nanoserver + context: ./ + dockerfile: ./windows/nanoserver/Dockerfile args: VERSION: ${DOCKER_AGENT_VERSION} WINDOWS_VERSION_TAG: ${NANOSERVER_VERSION_TAG} jdk17-nanoserver: image: jdk17-nanoserver-${NANOSERVER_VERSION_NAME} build: - context: ./windows/ - dockerfile: Dockerfile.nanoserver + context: ./ + dockerfile: ./windows/nanoserver/Dockerfile args: VERSION: ${DOCKER_AGENT_VERSION} WINDOWS_VERSION_TAG: ${NANOSERVER_VERSION_TAG} jdk11-windowsservercore: image: jdk11-windowsservercore-${WINDOWS_VERSION_NAME} build: - context: ./windows/ - dockerfile: Dockerfile.windowsservercore + context: ./ + dockerfile: ./windows/windowsservercore/Dockerfile args: VERSION: ${DOCKER_AGENT_VERSION} WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG} jdk17-windowsservercore: image: jdk17-windowsservercore-${WINDOWS_VERSION_NAME} build: - context: ./windows/ - dockerfile: Dockerfile.windowsservercore + context: ./ + dockerfile: ./windows/windowsservercore/Dockerfile args: VERSION: ${DOCKER_AGENT_VERSION} WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG} diff --git a/build.ps1 b/build.ps1 index 032a6bae..1bc4b354 100644 --- a/build.ps1 +++ b/build.ps1 @@ -136,12 +136,12 @@ function Test-Image { $env:AGENT_IMAGE = $ImageName $serviceName = $ImageName.SubString(0, $ImageName.LastIndexOf('-')) - $env:IMAGE_FOLDER = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${serviceName}.build.context" + $env:BUILD_CONTEXT = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${serviceName}.build.context" # TODO: review build number removal (?) # $env:VERSION = "$DockerAgentVersion-$BuildNumber" $env:VERSION = $DockerAgentVersion - Write-Host "= TEST: image folder ${env:IMAGE_FOLDER}, version ${env:VERSION}" + Write-Host "= TEST: image folder ${env:BUILD_CONTEXT}, version ${env:VERSION}" if(Test-Path ".\target\$ImageName") { Remove-Item -Recurse -Force ".\target\$ImageName" @@ -156,7 +156,7 @@ function Test-Image { Write-Host "There were $($TestResults.PassedCount) passed tests out of $($TestResults.TotalCount) in $ImageName" } Remove-Item env:\AGENT_IMAGE - Remove-Item env:\IMAGE_FOLDER + Remove-Item env:\BUILD_CONTEXT Remove-Item env:\VERSION } diff --git a/linux/Dockerfile.debian b/debian/Dockerfile similarity index 100% rename from linux/Dockerfile.debian rename to debian/Dockerfile diff --git a/docker-bake.hcl b/docker-bake.hcl index bf6731d7..25d4731a 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -48,8 +48,8 @@ variable "ON_TAG" { } target "alpine_jdk11" { - dockerfile = "Dockerfile.alpine" - context = "./linux/" + dockerfile = "./alpine/Dockerfile" + context = "./" args = { JAVA_MAJOR_VERSION = "11" VERSION = "${DOCKER_AGENT_VERSION}" @@ -66,8 +66,8 @@ target "alpine_jdk11" { } target "alpine_jdk17" { - dockerfile = "Dockerfile.alpine" - context = "./linux/" + dockerfile = "./alpine/Dockerfile" + context = "./" args = { JAVA_MAJOR_VERSION = "17" VERSION = "${DOCKER_AGENT_VERSION}" @@ -81,8 +81,8 @@ target "alpine_jdk17" { } target "debian_jdk11" { - dockerfile = "Dockerfile.debian" - context = "./linux/" + dockerfile = "./debian/Dockerfile" + context = "./" args = { JAVA_MAJOR_VERSION = "11" VERSION = "${DOCKER_AGENT_VERSION}" @@ -98,8 +98,8 @@ target "debian_jdk11" { } target "debian_jdk17" { - dockerfile = "Dockerfile.debian" - context = "./linux/" + dockerfile = "./debian/Dockerfile" + context = "./" args = { JAVA_MAJOR_VERSION = "17" VERSION = "${DOCKER_AGENT_VERSION}" diff --git a/tests/inboundAgent.Tests.ps1 b/tests/inboundAgent.Tests.ps1 index 390ca48a..80fadc6d 100644 --- a/tests/inboundAgent.Tests.ps1 +++ b/tests/inboundAgent.Tests.ps1 @@ -1,7 +1,7 @@ Import-Module -DisableNameChecking -Force $PSScriptRoot/test_helpers.psm1 $global:AGENT_IMAGE = Get-EnvOrDefault 'AGENT_IMAGE' '' -$global:IMAGE_FOLDER = Get-EnvOrDefault 'IMAGE_FOLDER' '' +$global:BUILD_CONTEXT = Get-EnvOrDefault 'BUILD_CONTEXT' '' $global:VERSION = Get-EnvOrDefault 'VERSION' '' $global:WINDOWS_VERSION_TAG = Get-EnvOrDefault 'WINDOWS_VERSION_TAG' '' @@ -33,7 +33,8 @@ BuildNcatImage($global:WINDOWS_VERSION_TAG) Describe "[$global:AGENT_IMAGE] build image" { It 'builds image' { - $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${global:VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file ${global:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}" + # TODO: fix + $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${global:VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file ./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}" $exitCode | Should -Be 0 } } @@ -124,7 +125,8 @@ Describe "[$global:AGENT_IMAGE] custom build args" { } It 'builds image with arguments' { - $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${ARG_TEST_VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=${global:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}" + # TODO: fix + $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${ARG_TEST_VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}" $exitCode | Should -Be 0 $exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --name $global:CONTAINERNAME $customImageName -Cmd $global:CONTAINERSHELL" diff --git a/updatecli/updatecli.d/docker-agent.yaml b/updatecli/updatecli.d/docker-agent.yaml index 7a12a433..8686c910 100644 --- a/updatecli/updatecli.d/docker-agent.yaml +++ b/updatecli/updatecli.d/docker-agent.yaml @@ -104,7 +104,7 @@ targets: name: Bump the parent image `jenkins/agent` version on Alpine kind: dockerfile spec: - file: linux/Dockerfile.alpine + file: alpine/Dockerfile instruction: keyword: ARG matcher: version @@ -113,7 +113,7 @@ targets: name: Bump the parent image `jenkins/agent` version on Debian kind: dockerfile spec: - file: linux/Dockerfile.debian + file: debian/Dockerfile instruction: keyword: ARG matcher: version @@ -122,7 +122,7 @@ targets: name: Bump the parent image `jenkins/agent` version on Windows Nanoserver kind: dockerfile spec: - file: windows/Dockerfile.nanoserver + file: windows/nanoserver/Dockerfile instruction: keyword: ARG matcher: version @@ -131,7 +131,7 @@ targets: name: Bump the parent image `jenkins/agent` version on Windows Server Core kind: dockerfile spec: - file: windows/windowsservercore + file: windows/windowsservercore/Dockerfile instruction: keyword: ARG matcher: version diff --git a/windows/Dockerfile.nanoserver b/windows/nanoserver/Dockerfile similarity index 100% rename from windows/Dockerfile.nanoserver rename to windows/nanoserver/Dockerfile diff --git a/windows/Dockerfile.windowsservercore b/windows/windowsservercore/Dockerfile similarity index 100% rename from windows/Dockerfile.windowsservercore rename to windows/windowsservercore/Dockerfile From f1df67ab049455df8a37490f57d7811793058cf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 15:19:57 +0200 Subject: [PATCH 05/17] restore jenkins-agent.ps1 location at root folder --- windows/jenkins-agent.ps1 => jenkins-agent.ps1 | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename windows/jenkins-agent.ps1 => jenkins-agent.ps1 (100%) diff --git a/windows/jenkins-agent.ps1 b/jenkins-agent.ps1 similarity index 100% rename from windows/jenkins-agent.ps1 rename to jenkins-agent.ps1 From 2dcf69dad5abee3ca7090edc8156970c0d04b069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 15:35:58 +0200 Subject: [PATCH 06/17] clean comments --- tests/inboundAgent.Tests.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/inboundAgent.Tests.ps1 b/tests/inboundAgent.Tests.ps1 index 80fadc6d..f68147d9 100644 --- a/tests/inboundAgent.Tests.ps1 +++ b/tests/inboundAgent.Tests.ps1 @@ -33,7 +33,6 @@ BuildNcatImage($global:WINDOWS_VERSION_TAG) Describe "[$global:AGENT_IMAGE] build image" { It 'builds image' { - # TODO: fix $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${global:VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file ./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}" $exitCode | Should -Be 0 } @@ -117,7 +116,7 @@ Describe "[$global:AGENT_IMAGE] custom build args" { BeforeAll { Push-Location -StackName 'agent' -Path "$PSScriptRoot/.." # Old version used to test overriding the build arguments. - # This old version must have the same tag suffixes as the current 4 windows images (`-jdk11-nanoserver` etc.) + # This old version must have the same tag suffixes as the current windows images (`-jdk11-nanoserver` etc.), and the same Windows version (2019, 2022, etc.) $TEST_VERSION = "3131.vf2b_b_798b_ce99" $DOCKER_AGENT_VERSION_SUFFIX = "4" $ARG_TEST_VERSION = "${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}" @@ -125,7 +124,6 @@ Describe "[$global:AGENT_IMAGE] custom build args" { } It 'builds image with arguments' { - # TODO: fix $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${ARG_TEST_VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}" $exitCode | Should -Be 0 From 55b83a819ebd3e4dc6af7bc3b20794fbd90b73f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 15:36:33 +0200 Subject: [PATCH 07/17] restore (strange) context for jenkins-agent script COPY in linux Dockerfiles --- alpine/Dockerfile | 2 +- debian/Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/alpine/Dockerfile b/alpine/Dockerfile index a3c49ed1..d8b88925 100644 --- a/alpine/Dockerfile +++ b/alpine/Dockerfile @@ -30,7 +30,7 @@ LABEL Description="This is a base image, which allows connecting Jenkins agents ARG user=jenkins USER root -COPY jenkins-agent /usr/local/bin/jenkins-agent +COPY ../../jenkins-agent /usr/local/bin/jenkins-agent RUN chmod +x /usr/local/bin/jenkins-agent &&\ ln -s /usr/local/bin/jenkins-agent /usr/local/bin/jenkins-slave USER ${user} diff --git a/debian/Dockerfile b/debian/Dockerfile index 4d90d719..2b96b0e7 100644 --- a/debian/Dockerfile +++ b/debian/Dockerfile @@ -8,7 +8,7 @@ LABEL Description="This is a base image, which allows connecting Jenkins agents ARG user=jenkins USER root -COPY jenkins-agent /usr/local/bin/jenkins-agent +COPY ../../jenkins-agent /usr/local/bin/jenkins-agent RUN chmod +x /usr/local/bin/jenkins-agent &&\ ln -s /usr/local/bin/jenkins-agent /usr/local/bin/jenkins-slave USER ${user} From 65d3001e8f2d1264392444ada93478bf4facd4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 15:58:03 +0200 Subject: [PATCH 08/17] restore docker-bake & jenkins-agent script location --- docker-bake.hcl | 16 ++++++++-------- linux/jenkins-agent => jenkins-agent | 0 2 files changed, 8 insertions(+), 8 deletions(-) rename linux/jenkins-agent => jenkins-agent (100%) diff --git a/docker-bake.hcl b/docker-bake.hcl index 25d4731a..e43962cf 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -48,8 +48,8 @@ variable "ON_TAG" { } target "alpine_jdk11" { - dockerfile = "./alpine/Dockerfile" - context = "./" + dockerfile = "alpine/Dockerfile" + context = "." args = { JAVA_MAJOR_VERSION = "11" VERSION = "${DOCKER_AGENT_VERSION}" @@ -66,8 +66,8 @@ target "alpine_jdk11" { } target "alpine_jdk17" { - dockerfile = "./alpine/Dockerfile" - context = "./" + dockerfile = "alpine/Dockerfile" + context = "." args = { JAVA_MAJOR_VERSION = "17" VERSION = "${DOCKER_AGENT_VERSION}" @@ -81,8 +81,8 @@ target "alpine_jdk17" { } target "debian_jdk11" { - dockerfile = "./debian/Dockerfile" - context = "./" + dockerfile = "debian/Dockerfile" + context = "." args = { JAVA_MAJOR_VERSION = "11" VERSION = "${DOCKER_AGENT_VERSION}" @@ -98,8 +98,8 @@ target "debian_jdk11" { } target "debian_jdk17" { - dockerfile = "./debian/Dockerfile" - context = "./" + dockerfile = "debian/Dockerfile" + context = "." args = { JAVA_MAJOR_VERSION = "17" VERSION = "${DOCKER_AGENT_VERSION}" diff --git a/linux/jenkins-agent b/jenkins-agent similarity index 100% rename from linux/jenkins-agent rename to jenkins-agent From 79beb20ebf1fe1d151952cc6dfdd5bd2ac0b3d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 16:03:08 +0200 Subject: [PATCH 09/17] restore PARENT_IMAGE_VERSION instead of DOCKER_AGENT_VERSION --- docker-bake.hcl | 24 ++++++++++++------------ updatecli/updatecli.d/docker-agent.yaml | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/docker-bake.hcl b/docker-bake.hcl index e43962cf..b0908d1d 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -30,8 +30,8 @@ variable "IMAGE_TAG" { default = "3071.v7e9b_0dc08466-1" } -#### This is for the "parent" image version to use (jenkins/agent:-) -variable "DOCKER_AGENT_VERSION" { +#### This is for the "parent" image version to use (jenkins/agent:-) +variable "PARENT_IMAGE_VERSION" { default = "3131.vf2b_b_798b_ce99-4" } @@ -52,11 +52,11 @@ target "alpine_jdk11" { context = "." args = { JAVA_MAJOR_VERSION = "11" - VERSION = "${DOCKER_AGENT_VERSION}" + VERSION = "${PARENT_IMAGE_VERSION}" } tags = [ - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}-alpine": "", - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}-alpine-jdk11": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-alpine": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-alpine-jdk11": "", "${REGISTRY}/${JENKINS_REPO}:alpine", "${REGISTRY}/${JENKINS_REPO}:alpine-jdk11", "${REGISTRY}/${JENKINS_REPO}:latest-alpine", @@ -70,10 +70,10 @@ target "alpine_jdk17" { context = "." args = { JAVA_MAJOR_VERSION = "17" - VERSION = "${DOCKER_AGENT_VERSION}" + VERSION = "${PARENT_IMAGE_VERSION}" } tags = [ - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}-alpine-jdk17": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-alpine-jdk17": "", "${REGISTRY}/${JENKINS_REPO}:alpine-jdk17", "${REGISTRY}/${JENKINS_REPO}:latest-alpine-jdk17", ] @@ -85,11 +85,11 @@ target "debian_jdk11" { context = "." args = { JAVA_MAJOR_VERSION = "11" - VERSION = "${DOCKER_AGENT_VERSION}" + VERSION = "${PARENT_IMAGE_VERSION}" } tags = [ - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}": "", - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}-jdk11": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-jdk11": "", "${REGISTRY}/${JENKINS_REPO}:jdk11", "${REGISTRY}/${JENKINS_REPO}:latest", "${REGISTRY}/${JENKINS_REPO}:latest-jdk11", @@ -102,10 +102,10 @@ target "debian_jdk17" { context = "." args = { JAVA_MAJOR_VERSION = "17" - VERSION = "${DOCKER_AGENT_VERSION}" + VERSION = "${PARENT_IMAGE_VERSION}" } tags = [ - equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${DOCKER_AGENT_VERSION}-jdk17": "", + equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-jdk17": "", "${REGISTRY}/${JENKINS_REPO}:jdk17", "${REGISTRY}/${JENKINS_REPO}:latest-jdk17", ] diff --git a/updatecli/updatecli.d/docker-agent.yaml b/updatecli/updatecli.d/docker-agent.yaml index 8686c910..ac2df7fd 100644 --- a/updatecli/updatecli.d/docker-agent.yaml +++ b/updatecli/updatecli.d/docker-agent.yaml @@ -142,9 +142,9 @@ targets: spec: file: docker-bake.hcl matchpattern: >- - variable(.*)"DOCKER_AGENT_VERSION"(.*){(.*)(\r\n|\r|\n)(.*)default(.*)=(.*) + variable(.*)"PARENT_IMAGE_VERSION"(.*){(.*)(\r\n|\r|\n)(.*)default(.*)=(.*) replacepattern: >- - variable${1}"DOCKER_AGENT_VERSION"${2}{${3}${4}${5}default${6}= "{{ source "lastVersion" }}" + variable${1}"PARENT_IMAGE_VERSION"${2}{${3}${4}${5}default${6}= "{{ source "lastVersion" }}" scmid: default setWindowsBuildPwshParentImage: name: Bump the parent image `jenkins/agent` version on the Windows build.ps1 powershell script From c3f7f7a6ae9cda0e6bdc97478a955a36d9e02fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 16:07:35 +0200 Subject: [PATCH 10/17] replace DOCKER_AGENT_VERSION by PARENT_IMAGE_VERSION --- build-windows.yaml | 8 ++++---- build.ps1 | 24 ++++++++++++------------ tests/inboundAgent.Tests.ps1 | 4 ++-- tests/tests.bats | 6 +++--- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/build-windows.yaml b/build-windows.yaml index 6839585b..85bf0a9c 100644 --- a/build-windows.yaml +++ b/build-windows.yaml @@ -5,7 +5,7 @@ services: context: ./ dockerfile: ./windows/nanoserver/Dockerfile args: - VERSION: ${DOCKER_AGENT_VERSION} + version: ${PARENT_IMAGE_VERSION} WINDOWS_VERSION_TAG: ${NANOSERVER_VERSION_TAG} jdk17-nanoserver: image: jdk17-nanoserver-${NANOSERVER_VERSION_NAME} @@ -13,7 +13,7 @@ services: context: ./ dockerfile: ./windows/nanoserver/Dockerfile args: - VERSION: ${DOCKER_AGENT_VERSION} + version: ${PARENT_IMAGE_VERSION} WINDOWS_VERSION_TAG: ${NANOSERVER_VERSION_TAG} jdk11-windowsservercore: image: jdk11-windowsservercore-${WINDOWS_VERSION_NAME} @@ -21,7 +21,7 @@ services: context: ./ dockerfile: ./windows/windowsservercore/Dockerfile args: - VERSION: ${DOCKER_AGENT_VERSION} + version: ${PARENT_IMAGE_VERSION} WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG} jdk17-windowsservercore: image: jdk17-windowsservercore-${WINDOWS_VERSION_NAME} @@ -29,5 +29,5 @@ services: context: ./ dockerfile: ./windows/windowsservercore/Dockerfile args: - VERSION: ${DOCKER_AGENT_VERSION} + version: ${PARENT_IMAGE_VERSION} WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG} diff --git a/build.ps1 b/build.ps1 index 1bc4b354..4b42e5d0 100644 --- a/build.ps1 +++ b/build.ps1 @@ -3,7 +3,7 @@ Param( [Parameter(Position=1)] [String] $Target = "build", [String] $Build = '', - [String] $DockerAgentVersion = '3131.vf2b_b_798b_ce99-4', + [String] $ParentImageVersion = '3131.vf2b_b_798b_ce99-4', [String] $BuildNumber = '1', [switch] $PushVersions = $false # [switch] $PushVersions = $false, @@ -15,7 +15,7 @@ $Repository = 'inbound-agent' $Organization = 'jenkins' $AgentType = 'windows-2019' -# TODO: not needed? Commented for now, env.props contains DOCKER_AGENT_VERSION in docker-agent +# TODO: not needed? Commented for now, env.props contains PARENT_IMAGE_VERSION in docker-agent # if(!$DisableEnvProps) { # Get-Content env.props | ForEach-Object { # $items = $_.Split("=") @@ -35,8 +35,8 @@ if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_ORGANISATION)) { $Organization = $env:DOCKERHUB_ORGANISATION } -if(![String]::IsNullOrWhiteSpace($env:DOCKER_AGENT_VERSION)) { - $DockerAgentVersion = $env:DOCKER_AGENT_VERSION +if(![String]::IsNullOrWhiteSpace($env:PARENT_IMAGE_VERSION)) { + $ParentImageVersion = $env:PARENT_IMAGE_VERSION } if(![String]::IsNullOrWhiteSpace($env:AGENT_TYPE)) { @@ -68,7 +68,7 @@ Function Test-CommandExists { # # this is the jdk version that will be used for the 'bare tag' images, e.g., jdk8-windowsservercore-1809 -> windowsserver-1809 # $defaultJdk = '11' $builds = @{} -$env:DOCKER_AGENT_VERSION = "$DockerAgentVersion" +$env:PARENT_IMAGE_VERSION = "$ParentImageVersion" $env:WINDOWS_VERSION_NAME = $AgentType.replace('windows-', 'ltsc') $env:NANOSERVER_VERSION_NAME = $env:WINDOWS_VERSION_NAME $env:WINDOWS_VERSION_TAG = $env:WINDOWS_VERSION_NAME @@ -98,7 +98,7 @@ Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object { $windowsVersion = $items[2] $baseImage = "${windowsType}-${windowsVersion}" - $versionTag = "${DockerAgentVersion}-${BuildNumber}-${image}" + $versionTag = "${ParentImageVersion}-${BuildNumber}-${image}" $tags = @( $image, $versionTag ) # TODO: keep it here too? (from docker-agent) # if($jdkMajorVersion -eq "$defaultJdk") { @@ -138,8 +138,8 @@ function Test-Image { $serviceName = $ImageName.SubString(0, $ImageName.LastIndexOf('-')) $env:BUILD_CONTEXT = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${serviceName}.build.context" # TODO: review build number removal (?) - # $env:VERSION = "$DockerAgentVersion-$BuildNumber" - $env:VERSION = $DockerAgentVersion + # $env:VERSION = "$ParentImageVersion-$BuildNumber" + $env:VERSION = $ParentImageVersion Write-Host "= TEST: image folder ${env:BUILD_CONTEXT}, version ${env:VERSION}" @@ -235,9 +235,9 @@ if($target -eq "publish") { } if($PushVersions) { - $buildTag = "$DockerAgentVersion-$BuildNumber-$tag" + $buildTag = "$ParentImageVersion-$BuildNumber-$tag" if($tag -eq 'latest') { - $buildTag = "$DockerAgentVersion-$BuildNumber" + $buildTag = "$ParentImageVersion-$BuildNumber" } Publish-Image "$Build" "${Organization}/${Repository}:${buildTag}" if($lastExitCode -ne 0) { @@ -254,9 +254,9 @@ if($target -eq "publish") { } if($PushVersions) { - $buildTag = "$DockerAgentVersion-$BuildNumber-$tag" + $buildTag = "$ParentImageVersion-$BuildNumber-$tag" if($tag -eq 'latest') { - $buildTag = "$DockerAgentVersion-$BuildNumber" + $buildTag = "$ParentImageVersion-$BuildNumber" } Publish-Image "$b" "${Organization}/${Repository}:${buildTag}" if($lastExitCode -ne 0) { diff --git a/tests/inboundAgent.Tests.ps1 b/tests/inboundAgent.Tests.ps1 index f68147d9..8c89b7a0 100644 --- a/tests/inboundAgent.Tests.ps1 +++ b/tests/inboundAgent.Tests.ps1 @@ -118,8 +118,8 @@ Describe "[$global:AGENT_IMAGE] custom build args" { # Old version used to test overriding the build arguments. # This old version must have the same tag suffixes as the current windows images (`-jdk11-nanoserver` etc.), and the same Windows version (2019, 2022, etc.) $TEST_VERSION = "3131.vf2b_b_798b_ce99" - $DOCKER_AGENT_VERSION_SUFFIX = "4" - $ARG_TEST_VERSION = "${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}" + $PARENT_IMAGE_VERSION_SUFFIX = "4" + $ARG_TEST_VERSION = "${TEST_VERSION}-${PARENT_IMAGE_VERSION_SUFFIX}" $customImageName = "custom-${global:AGENT_IMAGE}" } diff --git a/tests/tests.bats b/tests/tests.bats index 5cbb2a23..a216d1fc 100755 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -45,14 +45,14 @@ SUT_IMAGE="$(get_sut_image)" @test "[${SUT_IMAGE}] use build args correctly" { cd "${BATS_TEST_DIRNAME}"/.. || false - local TEST_VERSION DOCKER_AGENT_VERSION_SUFFIX ARG_TEST_VERSION TEST_USER sut_image sut_cid + local TEST_VERSION PARENT_IMAGE_VERSION_SUFFIX ARG_TEST_VERSION TEST_USER sut_image sut_cid # Old version used to test overriding the build arguments. # This old version must have the same tag suffixes as the ones defined in the docker-bake file (`-jdk17`, `jdk11`, etc.) TEST_VERSION="3131.vf2b_b_798b_ce99" - DOCKER_AGENT_VERSION_SUFFIX="4" + PARENT_IMAGE_VERSION_SUFFIX="4" - ARG_TEST_VERSION="${TEST_VERSION}-${DOCKER_AGENT_VERSION_SUFFIX}" + ARG_TEST_VERSION="${TEST_VERSION}-${PARENT_IMAGE_VERSION_SUFFIX}" TEST_USER="root" sut_image="${SUT_IMAGE}-tests-${BATS_TEST_NUMBER}" From 5aeca2edbe9c8ffa26f4ed0193bd655a103ed7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 16:11:40 +0200 Subject: [PATCH 11/17] replace 'VERSION' by 'version' --- alpine/Dockerfile | 8 ++++---- build.ps1 | 6 +++--- debian/Dockerfile | 8 ++++---- docker-bake.hcl | 8 ++++---- tests/inboundAgent.Tests.ps1 | 6 +++--- windows/nanoserver/Dockerfile | 8 ++++---- windows/windowsservercore/Dockerfile | 8 ++++---- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/alpine/Dockerfile b/alpine/Dockerfile index d8b88925..e3acb175 100644 --- a/alpine/Dockerfile +++ b/alpine/Dockerfile @@ -20,12 +20,12 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -ARG VERSION=3131.vf2b_b_798b_ce99-4 +ARG version=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 -FROM jenkins/agent:"${VERSION}"-alpine-jdk"${JAVA_MAJOR_VERSION}" +FROM jenkins/agent:"${version}"-alpine-jdk"${JAVA_MAJOR_VERSION}" -ARG VERSION=3131.vf2b_b_798b_ce99-4 -LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols" Vendor="Jenkins project" Version="$VERSION" +ARG version=3131.vf2b_b_798b_ce99-4 +LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols" Vendor="Jenkins project" Version="$version" ARG user=jenkins diff --git a/build.ps1 b/build.ps1 index 4b42e5d0..933970f5 100644 --- a/build.ps1 +++ b/build.ps1 @@ -138,10 +138,10 @@ function Test-Image { $serviceName = $ImageName.SubString(0, $ImageName.LastIndexOf('-')) $env:BUILD_CONTEXT = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${serviceName}.build.context" # TODO: review build number removal (?) - # $env:VERSION = "$ParentImageVersion-$BuildNumber" - $env:VERSION = $ParentImageVersion + # $env:version = "$ParentImageVersion-$BuildNumber" + $env:version = $ParentImageVersion - Write-Host "= TEST: image folder ${env:BUILD_CONTEXT}, version ${env:VERSION}" + Write-Host "= TEST: image folder ${env:BUILD_CONTEXT}, version ${env:version}" if(Test-Path ".\target\$ImageName") { Remove-Item -Recurse -Force ".\target\$ImageName" diff --git a/debian/Dockerfile b/debian/Dockerfile index 2b96b0e7..87ae642e 100644 --- a/debian/Dockerfile +++ b/debian/Dockerfile @@ -1,9 +1,9 @@ -ARG VERSION=3131.vf2b_b_798b_ce99-4 +ARG version=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 -FROM jenkins/agent:"${VERSION}"-jdk"${JAVA_MAJOR_VERSION}" +FROM jenkins/agent:"${version}"-jdk"${JAVA_MAJOR_VERSION}" -ARG VERSION=3131.vf2b_b_798b_ce99-4 -LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols" Vendor="Jenkins project" Version="$VERSION" +ARG version=3131.vf2b_b_798b_ce99-4 +LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols" Vendor="Jenkins project" Version="$version" ARG user=jenkins diff --git a/docker-bake.hcl b/docker-bake.hcl index b0908d1d..76601963 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -52,7 +52,7 @@ target "alpine_jdk11" { context = "." args = { JAVA_MAJOR_VERSION = "11" - VERSION = "${PARENT_IMAGE_VERSION}" + version = "${PARENT_IMAGE_VERSION}" } tags = [ equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-alpine": "", @@ -70,7 +70,7 @@ target "alpine_jdk17" { context = "." args = { JAVA_MAJOR_VERSION = "17" - VERSION = "${PARENT_IMAGE_VERSION}" + version = "${PARENT_IMAGE_VERSION}" } tags = [ equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-alpine-jdk17": "", @@ -85,7 +85,7 @@ target "debian_jdk11" { context = "." args = { JAVA_MAJOR_VERSION = "11" - VERSION = "${PARENT_IMAGE_VERSION}" + version = "${PARENT_IMAGE_VERSION}" } tags = [ equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}": "", @@ -102,7 +102,7 @@ target "debian_jdk17" { context = "." args = { JAVA_MAJOR_VERSION = "17" - VERSION = "${PARENT_IMAGE_VERSION}" + version = "${PARENT_IMAGE_VERSION}" } tags = [ equal(ON_TAG, "true") ? "${REGISTRY}/${JENKINS_REPO}:${PARENT_IMAGE_VERSION}-jdk17": "", diff --git a/tests/inboundAgent.Tests.ps1 b/tests/inboundAgent.Tests.ps1 index 8c89b7a0..7a47b59a 100644 --- a/tests/inboundAgent.Tests.ps1 +++ b/tests/inboundAgent.Tests.ps1 @@ -2,7 +2,7 @@ Import-Module -DisableNameChecking -Force $PSScriptRoot/test_helpers.psm1 $global:AGENT_IMAGE = Get-EnvOrDefault 'AGENT_IMAGE' '' $global:BUILD_CONTEXT = Get-EnvOrDefault 'BUILD_CONTEXT' '' -$global:VERSION = Get-EnvOrDefault 'VERSION' '' +$global:version = Get-EnvOrDefault 'VERSION' '' $global:WINDOWS_VERSION_TAG = Get-EnvOrDefault 'WINDOWS_VERSION_TAG' '' # TODO: make this name unique for concurency @@ -33,7 +33,7 @@ BuildNcatImage($global:WINDOWS_VERSION_TAG) Describe "[$global:AGENT_IMAGE] build image" { It 'builds image' { - $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${global:VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file ./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}" + $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg version=${global:version} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${global:AGENT_IMAGE} --file ./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}" $exitCode | Should -Be 0 } } @@ -124,7 +124,7 @@ Describe "[$global:AGENT_IMAGE] custom build args" { } It 'builds image with arguments' { - $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg VERSION=${ARG_TEST_VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}" + $exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg version=${ARG_TEST_VERSION} --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVA_MAJOR_VERSION} --tag=${customImageName} --file=./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}" $exitCode | Should -Be 0 $exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --name $global:CONTAINERNAME $customImageName -Cmd $global:CONTAINERSHELL" diff --git a/windows/nanoserver/Dockerfile b/windows/nanoserver/Dockerfile index 82015846..f71e02ba 100644 --- a/windows/nanoserver/Dockerfile +++ b/windows/nanoserver/Dockerfile @@ -21,13 +21,13 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -ARG VERSION=3131.vf2b_b_798b_ce99-4 +ARG version=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 ARG WINDOWS_VERSION_TAG=1809 -FROM jenkins/agent:"${VERSION}"-jdk"${JAVA_MAJOR_VERSION}"-nanoserver-"${WINDOWS_VERSION_TAG}" +FROM jenkins/agent:"${version}"-jdk"${JAVA_MAJOR_VERSION}"-nanoserver-"${WINDOWS_VERSION_TAG}" -ARG VERSION=3131.vf2b_b_798b_ce99-4 -LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols on Windows" Vendor="Jenkins Project" Version="$VERSION" +ARG version=3131.vf2b_b_798b_ce99-4 +LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols on Windows" Vendor="Jenkins Project" Version="$version" COPY jenkins-agent.ps1 C:/ProgramData/Jenkins ENTRYPOINT ["pwsh.exe", "-f", "C:/ProgramData/Jenkins/jenkins-agent.ps1"] diff --git a/windows/windowsservercore/Dockerfile b/windows/windowsservercore/Dockerfile index 8f86103d..af38ac64 100644 --- a/windows/windowsservercore/Dockerfile +++ b/windows/windowsservercore/Dockerfile @@ -21,13 +21,13 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -ARG VERSION=3131.vf2b_b_798b_ce99-4 +ARG version=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 ARG WINDOWS_VERSION_TAG=ltsc2019 -FROM jenkins/agent:"${VERSION}"-jdk"${JAVA_MAJOR_VERSION}"-windowsservercore-"${WINDOWS_VERSION_TAG}" +FROM jenkins/agent:"${version}"-jdk"${JAVA_MAJOR_VERSION}"-windowsservercore-"${WINDOWS_VERSION_TAG}" -ARG VERSION=3131.vf2b_b_798b_ce99-4 -LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols on Windows" Vendor="Jenkins Project" Version="$VERSION" +ARG version=3131.vf2b_b_798b_ce99-4 +LABEL Description="This is a base image, which allows connecting Jenkins agents via JNLP protocols on Windows" Vendor="Jenkins Project" Version="$version" COPY jenkins-agent.ps1 C:/ProgramData/Jenkins ENTRYPOINT ["powershell.exe", "-f", "C:/ProgramData/Jenkins/jenkins-agent.ps1"] From 2e234ab1fe5bba89ce8102cf05c8d5441dd77d11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 18:44:54 +0200 Subject: [PATCH 12/17] cleanup build.ps1 --- build.ps1 | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/build.ps1 b/build.ps1 index 933970f5..d85ad7ba 100644 --- a/build.ps1 +++ b/build.ps1 @@ -6,8 +6,6 @@ Param( [String] $ParentImageVersion = '3131.vf2b_b_798b_ce99-4', [String] $BuildNumber = '1', [switch] $PushVersions = $false - # [switch] $PushVersions = $false, - # [switch] $DisableEnvProps = $false ) $ErrorActionPreference ='Stop' @@ -15,18 +13,6 @@ $Repository = 'inbound-agent' $Organization = 'jenkins' $AgentType = 'windows-2019' -# TODO: not needed? Commented for now, env.props contains PARENT_IMAGE_VERSION in docker-agent -# if(!$DisableEnvProps) { -# Get-Content env.props | ForEach-Object { -# $items = $_.Split("=") -# if($items.Length -eq 2) { -# $name = $items[0].Trim() -# $value = $items[1].Trim() -# Set-Item -Path "env:$($name)" -Value $value -# } -# } -# } - if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO)) { $Repository = $env:DOCKERHUB_REPO } @@ -66,7 +52,7 @@ Function Test-CommandExists { } # # this is the jdk version that will be used for the 'bare tag' images, e.g., jdk8-windowsservercore-1809 -> windowsserver-1809 -# $defaultJdk = '11' +$defaultJdk = '11' $builds = @{} $env:PARENT_IMAGE_VERSION = "$ParentImageVersion" $env:WINDOWS_VERSION_NAME = $AgentType.replace('windows-', 'ltsc') @@ -100,10 +86,9 @@ Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object { $baseImage = "${windowsType}-${windowsVersion}" $versionTag = "${ParentImageVersion}-${BuildNumber}-${image}" $tags = @( $image, $versionTag ) - # TODO: keep it here too? (from docker-agent) - # if($jdkMajorVersion -eq "$defaultJdk") { - # $tags += $baseImage - # } + if($jdkMajorVersion -eq "$defaultJdk") { + $tags += $baseImage + } Write-Host "New Windows image to build ($image): ${Organization}/${Repository}:${baseImage} with JDK ${jdkMajorVersion}" @@ -137,8 +122,6 @@ function Test-Image { $env:AGENT_IMAGE = $ImageName $serviceName = $ImageName.SubString(0, $ImageName.LastIndexOf('-')) $env:BUILD_CONTEXT = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${serviceName}.build.context" - # TODO: review build number removal (?) - # $env:version = "$ParentImageVersion-$BuildNumber" $env:version = $ParentImageVersion Write-Host "= TEST: image folder ${env:BUILD_CONTEXT}, version ${env:version}" From 65b632580479e7464cc67934b5932e2341e7d75c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 18:48:56 +0200 Subject: [PATCH 13/17] restore 'version' case in tests.bats --- tests/tests.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests.bats b/tests/tests.bats index a216d1fc..72605188 100755 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -58,7 +58,7 @@ SUT_IMAGE="$(get_sut_image)" sut_image="${SUT_IMAGE}-tests-${BATS_TEST_NUMBER}" docker buildx bake \ - --set "${IMAGE}".args.VERSION="${ARG_TEST_VERSION}" \ + --set "${IMAGE}".args.version="${ARG_TEST_VERSION}" \ --set "${IMAGE}".args.user="${TEST_USER}" \ --set "${IMAGE}".platform=linux/"${ARCH}" \ --set "${IMAGE}".tags="${sut_image}" \ From 4d5c5a9929eb3b650608c42beb99d2e8c950bd0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= Date: Tue, 11 Jul 2023 18:52:26 +0200 Subject: [PATCH 14/17] restore outdated TODO comment, to be removed in another PR --- alpine/Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/alpine/Dockerfile b/alpine/Dockerfile index e3acb175..28357ca4 100644 --- a/alpine/Dockerfile +++ b/alpine/Dockerfile @@ -20,6 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +#TODO(oleg_nenashev): Does it also need an update? ARG version=3131.vf2b_b_798b_ce99-4 ARG JAVA_MAJOR_VERSION=17 FROM jenkins/agent:"${version}"-alpine-jdk"${JAVA_MAJOR_VERSION}" From d9d68fd8781bab1382e98daa0abdbe2c6af931dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= <91831478+lemeurherve@users.noreply.github.com> Date: Wed, 12 Jul 2023 11:52:30 +0200 Subject: [PATCH 15/17] Implement https://github.com/jenkinsci/docker-agent/pull/461/ too --- build.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build.ps1 b/build.ps1 index d85ad7ba..c781fd32 100644 --- a/build.ps1 +++ b/build.ps1 @@ -97,6 +97,9 @@ Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object { } } +Write-Host '= PREPARE: List of images and tags to be processed:' +ConvertTo-Json $builds + if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) { Write-Host "= BUILD: Building image ${Build}..." $dockerBuildCmd = '{0} {1}' -f $baseDockerBuildCmd, $Build From ff94a04000dc0a4d660f781d5c0c782f21d65bbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= <91831478+lemeurherve@users.noreply.github.com> Date: Wed, 12 Jul 2023 12:01:14 +0200 Subject: [PATCH 16/17] trim spaces on empty line --- build.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.ps1 b/build.ps1 index c781fd32..65dba32b 100644 --- a/build.ps1 +++ b/build.ps1 @@ -82,7 +82,7 @@ Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object { $jdkMajorVersion = $items[0].Remove(0,3) $windowsType = $items[1] $windowsVersion = $items[2] - + $baseImage = "${windowsType}-${windowsVersion}" $versionTag = "${ParentImageVersion}-${BuildNumber}-${image}" $tags = @( $image, $versionTag ) From 6fe472c24768545abeaa49fe38a7255c86498989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Le=20Meur?= <91831478+lemeurherve@users.noreply.github.com> Date: Wed, 12 Jul 2023 12:19:57 +0200 Subject: [PATCH 17/17] cleanup Co-authored-by: Damien Duportal --- build.ps1 | 6 ------ 1 file changed, 6 deletions(-) diff --git a/build.ps1 b/build.ps1 index 65dba32b..75014b9c 100644 --- a/build.ps1 +++ b/build.ps1 @@ -90,8 +90,6 @@ Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object { $tags += $baseImage } - Write-Host "New Windows image to build ($image): ${Organization}/${Repository}:${baseImage} with JDK ${jdkMajorVersion}" - $builds[$image] = @{ 'Tags' = $tags; } @@ -193,15 +191,11 @@ if($target -eq "test") { } } -# TODO: dry mode? function Publish-Image { param ( [String] $Build, [String] $ImageName ) - # foreach($tag in $builds[$ImageName]['Tags']) { - # $fullImageName = '{0}/{1}:{2}' -f $Organization, $Repository, $tag - # $cmd = "docker tag {0} {1}" -f $ImageName, $tag Write-Host "= PUBLISH: Tagging $Build => full name = $ImageName" docker tag "$Build" "$ImageName"