Skip to content

Commit

Permalink
Switch from powershell:<windows_flavor>-<windows_tag> to windows/<win…
Browse files Browse the repository at this point in the history
…dows_flavor>:<windows_tag>
  • Loading branch information
lemeurherve committed Aug 6, 2023
1 parent 24b8891 commit e8ec3c7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 29 deletions.
12 changes: 6 additions & 6 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def agentSelector() {
def agentSelector(String imageType) {
// Image type running on a Linux agent
if (env.IMAGE_TYPE == 'linux') {
if (imageType == 'linux') {
return 'linux'
}
// Image types running on a Windows Server Core 2022 agent
if (env.IMAGE_TYPE.contains('2022')) {
if (imageType.contains('2022')) {
return 'windows-2022'
}
// Remaining image types running on a Windows Server Core 2019 agent: (nanoserver|windowservercore)-(1809|2019)
Expand All @@ -25,13 +25,13 @@ pipeline {
axes {
axis {
name 'IMAGE_TYPE'
values 'linux', 'nanoserver-1809', 'nanoserver-ltsc2019', 'nanoserver-ltsc2022', 'windowsservercore-ltsc2019', 'windowsservercore-ltsc2022'
values 'linux', 'nanoserver-1809', 'nanoserver-ltsc2019', 'nanoserver-ltsc2022', 'windowsservercore-1809', 'windowsservercore-ltsc2019', 'windowsservercore-ltsc2022'
}
}
stages {
stage('Main') {
agent {
label agentSelector()
label agentSelector(env.IMAGE_TYPE)
}
options {
timeout(time: 30, unit: 'MINUTES')
Expand All @@ -42,7 +42,7 @@ pipeline {
stages {
stage('Prepare Docker') {
when {
environment name: 'AGENT_TYPE', value: 'linux'
environment name: 'IMAGE_TYPE', value: 'linux'
}
steps {
sh '''
Expand Down
4 changes: 2 additions & 2 deletions build-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ services:
JAVA_HOME: "C:/openjdk-11"
JAVA_VERSION: 11.0.20_8
VERSION: ${REMOTING_VERSION}
WINDOWS_FLAVOR: ${WINDOWS_FLAVOR}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
WINDOWS_VERSION_FALLBACK_TAG: ${WINDOWS_VERSION_FALLBACK_TAG}
jdk17:
image: jdk17-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
Expand All @@ -17,5 +17,5 @@ services:
JAVA_HOME: "C:/openjdk-17"
JAVA_VERSION: 17.0.8_7
VERSION: ${REMOTING_VERSION}
WINDOWS_FLAVOR: ${WINDOWS_FLAVOR}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
WINDOWS_VERSION_FALLBACK_TAG: ${WINDOWS_VERSION_FALLBACK_TAG}
14 changes: 9 additions & 5 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ $defaultJdk = '11'
$builds = @{}
$env:REMOTING_VERSION = "$RemotingVersion"

$imageItems = $ImageType.Split("-")
$env:WINDOWS_FLAVOR = $imageItems[0]
$env:WINDOWS_VERSION_TAG = $imageItems[1]
$items = $ImageType.Split("-")
$env:WINDOWS_FLAVOR = $items[0]
$env:WINDOWS_VERSION_TAG = $items[1]
$env:WINDOWS_VERSION_FALLBACK_TAG = $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:WINDOWS_VERSION_FALLBACK_TAG = '1809'
}

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

Expand Down Expand Up @@ -128,11 +133,10 @@ function Test-Image {
Write-Host "= TEST: Testing image ${ImageName}:"

$env:AGENT_IMAGE = $ImageName
$serviceName = $ImageName.SubString(0, $ImageName.LastIndexOf('-'))
$serviceName = $ImageName.SubString(0, $ImageName.IndexOf('-'))
$env:IMAGE_FOLDER = 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 Down
17 changes: 10 additions & 7 deletions tests/agent.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ 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' ''

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

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

# TODO: make this name unique for concurency
$global:CONTAINERNAME = 'pester-jenkins-agent-{0}' -f $global:AGENT_IMAGE

$global:CONTAINERSHELL="powershell.exe"
if($global:WINDOWSFLAVOR -eq 'nanoserver') {
if($global:WINDOWS_FLAVOR -eq 'nanoserver') {
$global:CONTAINERSHELL = "pwsh.exe"
}

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:JAVA_MAJOR_VERSION
}

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:WINDOWS_VERSION_TAG}`" --build-arg `"WINDOWS_VERSION_FALLBACK_TAG=${global:WINDOWS_VERSION_FALLBACK_TAG}`" --build-arg `"user=${global:TEST_USER}`" --build-arg `"AGENT_WORKDIR=${global:TEST_AGENT_WORKDIR}`" -t ${global:AGENT_IMAGE} ${global:IMAGE_FOLDER}"
$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
13 changes: 9 additions & 4 deletions windows/nanoserver/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@
# THE SOFTWARE.

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

FROM mcr.microsoft.com/powershell:"${WINDOWS_FLAVOR}"-"${WINDOWS_VERSION_TAG}"
FROM mcr.microsoft.com/powershell:nanoserver-"${WINDOWS_VERSION_FALLBACK_TAG}" 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
8 changes: 4 additions & 4 deletions windows/windowsservercore/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
# THE SOFTWARE.

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

FROM mcr.microsoft.com/powershell:"${WINDOWS_FLAVOR}"-"${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 e8ec3c7

Please sign in to comment.