Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lemeurherve committed Sep 7, 2023
1 parent fd18e0b commit 8a006a5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 71 deletions.
22 changes: 17 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 @@ -7,18 +20,17 @@ pipeline {

stages {
stage('docker-inbound-agent') {
failFast true
matrix {
axes {
axis {
name 'AGENT_TYPE'
values 'linux', 'windows-2019', 'windows-2022'
name 'IMAGE_TYPE'
values 'linux', 'nanoserver-1809', 'nanoserver-ltsc2019', '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 @@ -29,7 +41,7 @@ pipeline {
stages {
stage('Prepare Docker') {
when {
environment name: 'AGENT_TYPE', value: 'linux'
environment name: 'IMAGE_TYPE', value: 'linux'
}
steps {
sh '''
Expand Down
30 changes: 6 additions & 24 deletions build-windows.yaml
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
services:
jdk11-nanoserver:
image: jdk11-nanoserver-${NANOSERVER_VERSION_NAME}
jdk11:
image: jdk11-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
context: ./
dockerfile: ./windows/nanoserver/Dockerfile
args:
JAVA_MAJOR_VERSION: 11
version: ${PARENT_IMAGE_VERSION}
WINDOWS_VERSION_TAG: ${NANOSERVER_VERSION_TAG}
jdk17-nanoserver:
image: jdk17-nanoserver-${NANOSERVER_VERSION_NAME}
build:
context: ./
dockerfile: ./windows/nanoserver/Dockerfile
args:
JAVA_MAJOR_VERSION: 17
version: ${PARENT_IMAGE_VERSION}
WINDOWS_VERSION_TAG: ${NANOSERVER_VERSION_TAG}
jdk11-windowsservercore:
image: jdk11-windowsservercore-${WINDOWS_VERSION_NAME}
build:
context: ./
dockerfile: ./windows/windowsservercore/Dockerfile
dockerfile: ./windows/${WINDOWS_FLAVOR}/Dockerfile
args:
JAVA_MAJOR_VERSION: 11
version: ${PARENT_IMAGE_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
jdk17-windowsservercore:
image: jdk17-windowsservercore-${WINDOWS_VERSION_NAME}
jdk17:
image: jdk17-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
context: ./
dockerfile: ./windows/windowsservercore/Dockerfile
dockerfile: ./windows/${WINDOWS_FLAVOR}/Dockerfile
args:
JAVA_MAJOR_VERSION: 17
version: ${PARENT_IMAGE_VERSION}
Expand Down
49 changes: 22 additions & 27 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Param(
[switch] $PushVersions = $false
)

$ErrorActionPreference ='Stop'
$ErrorActionPreference = 'Stop'
$Repository = 'inbound-agent'
$Organization = 'jenkins'
$AgentType = 'windows-2019'
$ImageType = 'windowsservercore-ltsc2019'

if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO)) {
$Repository = $env:DOCKERHUB_REPO
Expand All @@ -25,8 +25,8 @@ if(![String]::IsNullOrWhiteSpace($env:PARENT_IMAGE_VERSION)) {
$ParentImageVersion = $env:PARENT_IMAGE_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 @@ -55,15 +55,15 @@ Function Test-CommandExists {
$defaultJdk = '11'
$builds = @{}
$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
$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
}

$items = $ImageType.Split("-")
$env:WINDOWS_FLAVOR = $items[0]
$env:WINDOWS_VERSION_TAG = $items[1]

# # Uncomment to help debugging when working on this script
# Write-Host "= DEBUG: env vars"
# Get-ChildItem Env: | ForEach-Object { Write-Host "$($_.Name) = $($_.Value)" }

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

Test-CommandExists "docker"
Expand All @@ -74,18 +74,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("-")
$image = '{0}-{1}-{2}' -f $_, $env:WINDOWS_FLAVOR, $env:WINDOWS_VERSION_TAG # Ex: "jdk11-windowsservercore-ltsc2019"

# Remove the 'jdk' prefix (3 first characters)
$jdkMajorVersion = $items[0].Remove(0,3)
$windowsType = $items[1]
$windowsVersion = $items[2]
$jdkMajorVersion = $_.Remove(0,3)

$baseImage = "${windowsType}-${windowsVersion}"
$baseImage = "${env:WINDOWS_FLAVOR}-${env:WINDOWS_VERSION_TAG}"
$completeVersionTag = "${VersionTag}-${image}"
$tags = @( $image, $completeVersionTag )
# Additional image tag without any 'jdk' prefix for the default JDK
if($jdkMajorVersion -eq "$defaultJdk") {
$tags += $baseImage
}
Expand All @@ -95,7 +92,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 for ${ImageType}:"
ConvertTo-Json $builds

if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) {
Expand All @@ -118,15 +115,13 @@ function Test-Image {
$ImageName
)

Write-Host "= TEST: Testing image ${ImageName}:"
Write-Host "= TEST: Testing image ${ImageName}:" # Ex: jdk11-windowsservercore-ltsc2019

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

Write-Host "= TEST: image folder ${env:BUILD_CONTEXT}, version ${env:version}"

if(Test-Path ".\target\$ImageName") {
Remove-Item -Recurse -Force ".\target\$ImageName"
}
Expand Down Expand Up @@ -176,7 +171,7 @@ if($target -eq "test") {
if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) {
Test-Image $Build
} else {
Write-Host "= TEST: Testing all images"
Write-Host "= TEST: Testing all images..."
foreach($image in $builds.Keys) {
Test-Image $image
}
Expand Down
28 changes: 13 additions & 15 deletions tests/inboundAgent.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,35 @@ 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: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

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

# Remove the 'jdk' prefix (3 first characters)
$global:JAVA_MAJOR_VERSION = $items[0].Remove(0,3)
$global:JAVAMAJORVERSION = $items[0].Remove(0,3)
$global:WINDOWSFLAVOR = $items[1]
$global:WINDOWSVERSION = $items[2]
$global:WINDOWSVERSIONTAG = $items[2]

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

$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'
}
}

# # Uncomment to help debugging when working on this script
# Write-Host "= DEBUG: global vars"
# Get-Variable -Scope Global | ForEach-Object { Write-Host "$($_.Name) = $($_.Value)" }

Cleanup($global:CONTAINERNAME)
Cleanup("nmap")
CleanupNetwork("jnlp-network")

BuildNcatImage($global:WINDOWS_VERSION_TAG)
BuildNcatImage($global:WINDOWSVERSIONTAG)

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:WINDOWSVERSIONTAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVAMAJORVERSION} --tag=${global:AGENT_IMAGE} --file ./windows/${global:WINDOWSFLAVOR}/Dockerfile ${global:BUILD_CONTEXT}"
$exitCode | Should -Be 0
}
}
Expand Down Expand Up @@ -124,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 `"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:WINDOWSVERSIONTAG}`" --build-arg JAVA_MAJOR_VERSION=${global:JAVAMAJORVERSION} --build-arg WINDOWS_FLAVOR=${global:WINDOWSFLAVOR} --build-arg CONTAINER_SHELL=${global:CONTAINERSHELL} --tag=${customImageName} --file=./windows/Dockerfile ${global:BUILD_CONTEXT}"
$exitCode | Should -Be 0

$exitCode, $stdout, $stderr = Run-Program 'docker' "run --detach --tty --name $global:CONTAINERNAME $customImageName -Cmd $global:CONTAINERSHELL"
Expand All @@ -145,7 +143,7 @@ Describe "[$global:AGENT_IMAGE] custom build args" {
}

Describe "[$global:AGENT_IMAGE] passing JVM options (slow test)" {
It "shows the java version ${global:JAVA_MAJOR_VERSION} with --show-version" {
It "shows the java version ${global:JAVAMAJORVERSION} with --show-version" {
$exitCode, $stdout, $stderr = Run-Program 'docker' "network create --driver nat jnlp-network"
# Launch the netcat utility, listening at port 5000 for 30 sec
# bats will capture the output from netcat and compare the first line
Expand All @@ -167,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:JAVA_MAJOR_VERSION}"
$stdout | Should -Match "OpenJDK Runtime Environment Temurin-${global:JAVAMAJORVERSION}"
}

AfterAll {
Expand Down
1 change: 1 addition & 0 deletions tests/netcat-helper/Dockerfile-windows
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Available tags: https://mcr.microsoft.com/v2/windows/servercore/tags/list
ARG WINDOWS_VERSION_TAG=1809
FROM mcr.microsoft.com/windows/servercore:"${WINDOWS_VERSION_TAG}"

Expand Down

0 comments on commit 8a006a5

Please sign in to comment.