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