Skip to content

Commit

Permalink
Merge pull request #459 from lemeurherve/patch-4
Browse files Browse the repository at this point in the history
breaking: use a `ltsc2019` base image for `windowsservercore-ltsc2019` agent images
  • Loading branch information
dduportal authored Sep 4, 2023
2 parents 323db16 + 7c2cec1 commit 0bfd3a0
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 68 deletions.
23 changes: 18 additions & 5 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
def agentSelector(String imageType) {
// Image type running on a Linux agent
if (imageType == 'linux') {
return 'linux'
}
// Image types running on a Windows Server Core 2022 agent
if (imageType.contains('2022')) {
return 'windows-2022'
}
// Remaining image types running on a Windows Server Core 2019 agent: (nanoserver|windowservercore)-(1809|2019)
return 'windows-2019'
}

pipeline {
agent none

Expand All @@ -10,14 +23,14 @@ pipeline {
matrix {
axes {
axis {
name 'AGENT_TYPE'
values 'linux', 'windows-2019', 'windows-2022'
name 'IMAGE_TYPE'
values 'linux', 'nanoserver-1809', 'nanoserver-ltsc2022', 'windowsservercore-1809', 'windowsservercore-ltsc2019', 'windowsservercore-ltsc2022'
}
}
stages {
stage('Main') {
agent {
label env.AGENT_TYPE
label agentSelector(env.IMAGE_TYPE)
}
options {
timeout(time: 30, unit: 'MINUTES')
Expand All @@ -28,7 +41,7 @@ pipeline {
stages {
stage('Prepare Docker') {
when {
environment name: 'AGENT_TYPE', value: 'linux'
environment name: 'IMAGE_TYPE', value: 'linux'
}
steps {
sh '''
Expand All @@ -50,7 +63,7 @@ pipeline {
// 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"
powershell '& ./build.ps1 test'
}
}
}
Expand Down
34 changes: 10 additions & 24 deletions build-windows.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
services:
jdk11-nanoserver:
image: jdk11-nanoserver-${NANOSERVER_VERSION_NAME}
jdk11:
image: jdk11-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
context: ./windows/nanoserver/
context: ./
dockerfile: ./windows/${WINDOWS_FLAVOR}/Dockerfile
args:
JAVA_HOME: "C:/openjdk-11"
JAVA_VERSION: 11.0.20.1_1
VERSION: ${REMOTING_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
jdk17-nanoserver:
image: jdk17-nanoserver-${NANOSERVER_VERSION_NAME}
TOOLS_WINDOWS_VERSION: ${TOOLS_WINDOWS_VERSION}
jdk17:
image: jdk17-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
context: ./windows/nanoserver/
args:
JAVA_HOME: "C:/openjdk-17"
JAVA_VERSION: 17.0.8.1_1
VERSION: ${REMOTING_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
jdk11-windowsservercore:
image: jdk11-windowsservercore-${WINDOWS_VERSION_NAME}
build:
context: ./windows/windowsservercore/
args:
JAVA_HOME: "C:/openjdk-11"
JAVA_VERSION: 11.0.20.1_1
VERSION: ${REMOTING_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
jdk17-windowsservercore:
image: jdk17-windowsservercore-${WINDOWS_VERSION_NAME}
build:
context: ./windows/windowsservercore/
context: ./
dockerfile: ./windows/${WINDOWS_FLAVOR}/Dockerfile
args:
JAVA_HOME: "C:/openjdk-17"
JAVA_VERSION: 17.0.8.1_1
VERSION: ${REMOTING_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
TOOLS_WINDOWS_VERSION: ${TOOLS_WINDOWS_VERSION}
48 changes: 23 additions & 25 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Param(
$ErrorActionPreference = 'Stop'
$Repository = 'agent'
$Organization = 'jenkins'
$AgentType = 'windows-2019'
$ImageType = 'windows-ltsc2019'

if(!$DisableEnvProps) {
Get-Content env.props | ForEach-Object {
Expand All @@ -37,8 +37,8 @@ if(![String]::IsNullOrWhiteSpace($env:REMOTING_VERSION)) {
$RemotingVersion = $env:REMOTING_VERSION
}

if(![String]::IsNullOrWhiteSpace($env:AGENT_TYPE)) {
$AgentType = $env:AGENT_TYPE
if(![String]::IsNullOrWhiteSpace($env:IMAGE_TYPE)) {
$ImageType = $env:IMAGE_TYPE
}

# Check for required commands
Expand Down Expand Up @@ -67,15 +67,16 @@ Function Test-CommandExists {
$defaultJdk = '11'
$builds = @{}
$env:REMOTING_VERSION = "$RemotingVersion"
$env:WINDOWS_VERSION_NAME = $AgentType.replace('windows-', 'ltsc')
$env:NANOSERVER_VERSION_NAME = $env:WINDOWS_VERSION_NAME
$env:WINDOWS_VERSION_TAG = $env:WINDOWS_VERSION_NAME
# Unconsistent naming for the 2019 version, needed as while nanoserver-ltsc2019 and windowsserver-ltsc2019 tags exist eclipse-temurin:<...>-ltsc2019 does not
# We also 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:WINDOWS_VERSION_TAG = 1809
$env:NANOSERVER_VERSION_NAME = 1809

$items = $ImageType.Split("-")
$env:WINDOWS_FLAVOR = $items[0]
$env:WINDOWS_VERSION_TAG = $items[1]
$env:TOOLS_WINDOWS_VERSION = $items[1]
if ($items[1] -eq 'ltsc2019') {
# There are no eclipse-temurin:*-ltsc2019 or mcr.microsoft.com/powershell:*-ltsc2019 docker images unfortunately, only "1809" ones
$env:TOOLS_WINDOWS_VERSION = '1809'
}

$ProgressPreference = 'SilentlyContinue' # Disable Progress bar for faster downloads

Test-CommandExists "docker"
Expand All @@ -86,17 +87,15 @@ $baseDockerCmd = 'docker-compose --file=build-windows.yaml'
$baseDockerBuildCmd = '{0} build --parallel --pull' -f $baseDockerCmd

Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object {
$image = '{0}-{1}' -f $_, $env:WINDOWS_VERSION_NAME
# Special case for nanoserver-1809 images
$image = $image.replace('nanoserver-ltsc2019', 'nanoserver-1809')
$items = $image.Split("-")
$jdkMajorVersion = $items[0].Remove(0,3)
$windowsType = $items[1]
$windowsVersion = $items[2]

$baseImage = "${windowsType}-${windowsVersion}"
$image = '{0}-{1}-{2}' -f $_, $env:WINDOWS_FLAVOR, $env:WINDOWS_VERSION_TAG # Ex: "jdk11-nanoserver-1809"

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

$baseImage = "${env:WINDOWS_FLAVOR}-${env:WINDOWS_VERSION_TAG}"
$versionTag = "${RemotingVersion}-${BuildNumber}-${image}"
$tags = @( $image, $versionTag )
# Additional image tag without any 'jdk' prefix for the default JDK
if($jdkMajorVersion -eq "$defaultJdk") {
$tags += $baseImage
}
Expand All @@ -106,7 +105,7 @@ Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object {
}
}

Write-Host '= PREPARE: List of images and tags to be processed:'
Write-Host "= PREPARE: List of $Organization/$Repository images and tags to be processed:"
ConvertTo-Json $builds

if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) {
Expand All @@ -132,11 +131,10 @@ function Test-Image {
Write-Host "= TEST: Testing image ${ImageName}:"

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


if(Test-Path ".\target\$ImageName") {
Remove-Item -Recurse -Force ".\target\$ImageName"
}
Expand All @@ -150,7 +148,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
}

Expand Down
17 changes: 10 additions & 7 deletions tests/agent.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
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' ''

$items = $global:AGENT_IMAGE.Split("-")

# Remove the 'jdk' prefix (3 first characters)
$global:JDKMAJORVERSION = $items[0].Remove(0,3)
# Remove the 'jdk' prefix
$global:JAVAMAJORVERSION = $items[0].Remove(0,3)
$global:WINDOWSFLAVOR = $items[1]
$global:WINDOWSVERSION = $items[2]
$global:WINDOWSVERSIONTAG = $items[2]
$global:WINDOWSVERSIONFALLBACKTAG = $items[2]
if ($items[2] -eq 'ltsc2019') {
$global:WINDOWSVERSIONFALLBACKTAG = '1809'
}

# TODO: make this name unique for concurency
$global:CONTAINERNAME = 'pester-jenkins-agent-{0}' -f $global:AGENT_IMAGE
Expand Down Expand Up @@ -52,7 +55,7 @@ Describe "[$global:AGENT_IMAGE] image has correct applications in the PATH" {
$r = [regex] "^openjdk version `"(?<major>\d+)"
$m = $r.Match($stdout)
$m | Should -Not -Be $null
$m.Groups['major'].ToString() | Should -Be $global:JDKMAJORVERSION
$m.Groups['major'].ToString() | Should -Be $global:JAVAMAJORVERSION
}

It 'has AGENT_WORKDIR in the environment' {
Expand Down Expand Up @@ -127,7 +130,7 @@ Describe "[$global:AGENT_IMAGE] can be built with custom build arguments" {
BeforeAll {
Push-Location -StackName 'agent' -Path "$PSScriptRoot/.."

$exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg `"VERSION=${global:TEST_VERSION}`" --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWS_VERSION_TAG}`" --build-arg `"user=${global:TEST_USER}`" --build-arg `"AGENT_WORKDIR=${global:TEST_AGENT_WORKDIR}`" -t ${global:AGENT_IMAGE} ${global:IMAGE_FOLDER}"
$exitCode, $stdout, $stderr = Run-Program 'docker' "build --build-arg `"VERSION=${global:TEST_VERSION}`" --build-arg `"WINDOWS_VERSION_TAG=${global:WINDOWSVERSIONTAG}`" --build-arg `"TOOLS_WINDOWS_VERSION=${global:WINDOWSVERSIONFALLBACKTAG}`" --build-arg `"user=${global:TEST_USER}`" --build-arg `"AGENT_WORKDIR=${global:TEST_AGENT_WORKDIR}`" --tag ${global:AGENT_IMAGE} --file ./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}"
$exitCode | Should -Be 0

docker run -d -it --name "$global:CONTAINERNAME" -P "$global:AGENT_IMAGE" "$global:CONTAINERSHELL"
Expand Down
5 changes: 4 additions & 1 deletion tests/test_helpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ function Run-Program($cmd, $params) {
$stderr = $proc.StandardError.ReadToEnd()
$proc.WaitForExit()
if($proc.ExitCode -ne 0) {
Write-Host "`n`nstdout:`n$stdout`n`nstderr:`n$stderr`n`ncmd:`n$cmd`n`nparams:`n$param`n`n"
Write-Host "[err] stdout:`n$stdout"
Write-Host "[err] stderr:`n$stderr"
Write-Host "[err] cmd:`n$cmd"
Write-Host "[err] params:`n$param"
}

return $proc.ExitCode, $stdout, $stderr
Expand Down
12 changes: 9 additions & 3 deletions windows/nanoserver/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@
# THE SOFTWARE.

ARG JAVA_VERSION=17.0.7_7
ARG WINDOWS_VERSION_TAG=1809
FROM eclipse-temurin:"${JAVA_VERSION}"-jdk-windowsservercore-"${WINDOWS_VERSION_TAG}" AS jdk-core
ARG WINDOWS_VERSION_TAG=ltsc2019
ARG TOOLS_WINDOWS_VERSION=1809
FROM eclipse-temurin:"${JAVA_VERSION}"-jdk-windowsservercore-"${TOOLS_WINDOWS_VERSION}" AS jdk-core

FROM mcr.microsoft.com/powershell:nanoserver-"${WINDOWS_VERSION_TAG}"
FROM mcr.microsoft.com/powershell:nanoserver-"${TOOLS_WINDOWS_VERSION}" AS pwsh-source

FROM mcr.microsoft.com/windows/nanoserver:"${WINDOWS_VERSION_TAG}"

ARG JAVA_HOME="C:\openjdk-17"
ENV PSHOME="C:\Program Files\PowerShell"
ENV PATH="C:\Windows\system32;C:\Windows;${PSHOME};"

# The nanoserver image is nice and small, but we need a couple of things to get SSH working
COPY --from=jdk-core /windows/system32/netapi32.dll /windows/system32/netapi32.dll
COPY --from=jdk-core /windows/system32/whoami.exe /windows/system32/whoami.exe
COPY --from=jdk-core $JAVA_HOME $JAVA_HOME
COPY --from=pwsh-source $PSHOME $PSHOME

SHELL ["pwsh.exe", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
USER ContainerAdministrator
Expand Down
7 changes: 4 additions & 3 deletions windows/windowsservercore/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
# THE SOFTWARE.

ARG JAVA_VERSION=17.0.7_7
ARG WINDOWS_VERSION_TAG=1809
FROM eclipse-temurin:"${JAVA_VERSION}"-jdk-windowsservercore-"${WINDOWS_VERSION_TAG}" AS jdk-core
ARG WINDOWS_VERSION_TAG=ltsc2019
ARG TOOLS_WINDOWS_VERSION=1809
FROM eclipse-temurin:"${JAVA_VERSION}"-jdk-windowsservercore-"${TOOLS_WINDOWS_VERSION}" AS jdk-core

FROM mcr.microsoft.com/powershell:windowsservercore-"${WINDOWS_VERSION_TAG}"
FROM mcr.microsoft.com/windows/servercore:"${WINDOWS_VERSION_TAG}"

ARG JAVA_HOME="C:\openjdk-17"
ENV JAVA_HOME=${JAVA_HOME}
Expand Down

0 comments on commit 0bfd3a0

Please sign in to comment.