Skip to content

Commit

Permalink
chore(Windows): define tags in docker compose file instead of code
Browse files Browse the repository at this point in the history
  • Loading branch information
lemeurherve committed Jan 14, 2024
1 parent 06ade7e commit 2cda621
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 117 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pipeline {
}

stages {
stage('docker-agent') {
stage('Matrix') {
matrix {
axes {
axis {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![GitHub stars](https://img.shields.io/github/stars/jenkinsci/docker-agent?label=GitHub%20stars)](https://github.com/jenkinsci/docker-agent)
[![GitHub release](https://img.shields.io/github/release/jenkinsci/docker-agent.svg?label=changelog)](https://github.com/jenkinsci/docker-agent/releases/latest)

This repository contains the definition of two images:
This repository contains the definition of Jenkins agent and inbound agent Docker images.

## agent
[![Docker Pulls](https://img.shields.io/docker/pulls/jenkins/agent.svg)](https://hub.docker.com/r/jenkins/agent/)
Expand All @@ -16,6 +16,6 @@ See [the `agent` README](./README_agent.md)
## inbound-agent
[![Docker Pulls](https://img.shields.io/docker/pulls/jenkins/inbound-agent.svg)](https://hub.docker.com/r/jenkins/inbound-agent/)

This is an image based on `agent` for [Jenkins](https://jenkins.io) agents using TCP or WebSockets to establish inbound connection to the Jenkins master.
This is an image based on `agent` for [Jenkins](https://jenkins.io) agents using TCP or WebSockets to establish inbound connection to the Jenkins controller.

See [the `inbound-agent` README](./README_inbound-agent.md)
2 changes: 1 addition & 1 deletion README_inbound-agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:exclamation: **Warning!** This image used to be published as [jenkinsci/jnlp-slave](https://hub.docker.com/r/jenkinsci/jnlp-slave/) and [jenkins/jnlp-slave](https://hub.docker.com/r/jenkins/jnlp-slave/).
These images are deprecated, use [jenkins/inbound-agent](https://hub.docker.com/r/jenkins/inbound-agent/).

This is an image for [Jenkins](https://jenkins.io) agents using TCP or WebSockets to establish inbound connection to the Jenkins master.
This is an image for [Jenkins](https://jenkins.io) agents using TCP or WebSockets to establish inbound connection to the Jenkins controller.
This agent is powered by the [Jenkins Remoting library](https://github.com/jenkinsci/remoting), which version is being taken from the base [Docker Agent](https://github.com/jenkinsci/docker-agent/) image.

See [Using Agents](https://www.jenkins.io/doc/book/using/using-agents/) for more info.
Expand Down
14 changes: 11 additions & 3 deletions build-windows.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
jdk11:
image: jdk11-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
image: ${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:jdk11-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
context: ./
dockerfile: ./windows/${WINDOWS_FLAVOR}/Dockerfile
Expand All @@ -10,8 +10,10 @@ services:
VERSION: ${REMOTING_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
TOOLS_WINDOWS_VERSION: ${TOOLS_WINDOWS_VERSION}
tags:
- "${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:${REMOTING_VERSION}-${BUILD_NUMBER}-jdk11-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}"
jdk17:
image: jdk17-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
image: ${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:jdk17-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
context: ./
dockerfile: ./windows/${WINDOWS_FLAVOR}/Dockerfile
Expand All @@ -21,8 +23,12 @@ services:
VERSION: ${REMOTING_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
TOOLS_WINDOWS_VERSION: ${TOOLS_WINDOWS_VERSION}
tags:
- "${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:${REMOTING_VERSION}-${BUILD_NUMBER}-jdk17-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}"
- "${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:${REMOTING_VERSION}-${BUILD_NUMBER}-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}"
- "${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}"
jdk21:
image: jdk21-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
image: ${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:jdk21-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}
build:
context: ./
dockerfile: ./windows/${WINDOWS_FLAVOR}/Dockerfile
Expand All @@ -32,3 +38,5 @@ services:
VERSION: ${REMOTING_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
TOOLS_WINDOWS_VERSION: ${TOOLS_WINDOWS_VERSION}
tags:
- "${DOCKERHUB_ORGANISATION}/${DOCKERHUB_REPO}:${REMOTING_VERSION}-${BUILD_NUMBER}-jdk21-${WINDOWS_FLAVOR}-${WINDOWS_VERSION_TAG}"
115 changes: 42 additions & 73 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ if ($AgentType -ne '' -and $AgentType -in $AgentTypes) {
}
$ImageType = 'windowsservercore-ltsc2019'
$Organisation = 'jenkins4eval'
$AgentRepository = 'agent'
$InboundAgentRepository = 'inbound-agent'
$Repository = @{
'agent' = 'agent'
'inbound-agent' = 'inbound-agent'
}

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

if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO_AGENT)) {
$AgentRepository = $env:DOCKERHUB_REPO_AGENT
$Repository['agent'] = $env:DOCKERHUB_REPO_AGENT
}

if(![String]::IsNullOrWhiteSpace($env:DOCKERHUB_REPO_INBOUND_AGENT)) {
$InboundAgentRepository = $env:DOCKERHUB_REPO_INBOUND_AGENT
$Repository['inbound-agent'] = $env:DOCKERHUB_REPO_INBOUND_AGENT
}

if(![String]::IsNullOrWhiteSpace($env:REMOTING_VERSION)) {
Expand Down Expand Up @@ -72,9 +74,10 @@ Function Test-CommandExists {
}
}

# this is the jdk version that will be used for the 'bare tag' images, e.g., jdk17-windowsservercore-1809 -> windowsserver-1809
$defaultJdk = '17'
# Ensure constant env vars used in the docker compose file are defined
$env:DOCKERHUB_ORGANISATION = "$Organisation"
$env:REMOTING_VERSION = "$RemotingVersion"
$env:BUILD_NUMBER = $BuildNumber

$items = $ImageType.Split("-")
$env:WINDOWS_FLAVOR = $items[0]
Expand All @@ -99,98 +102,70 @@ function Test-Image {
$items = $AgentTypeAndImageName.Split("|")
$agentType = $items[0]
$imageName = $items[1]
$imageNameItems = $imageName.Split(":")
$imageTag = $imageNameItems[1]

Write-Host "= TEST: Testing ${agentType} image ${imageName}:"
Write-Host "= TEST: Testing ${imageName} image:"

$env:AGENT_TYPE = $agentType
$env:AGENT_IMAGE = $imageName
$env:VERSION = "$RemotingVersion-$BuildNumber"
$env:IMAGE_NAME = $imageName
$env:VERSION = "$RemotingVersion"

$targetPath = '.\target\{0}\{1}' -f $agentType, $imageName
$targetPath = '.\target\{0}\{1}' -f $agentType, $imageTag
if(Test-Path $targetPath) {
Remove-Item -Recurse -Force $targetPath
}
New-Item -Path $targetPath -Type Directory | Out-Null
$configuration.Run.Path = 'tests\{0}.Tests.ps1' -f $agentType
$configuration.TestResult.OutputPath = '{0}\junit-results.xml' -f $targetPath
$TestResults = Invoke-Pester -Configuration $configuration
$failed = $false
if ($TestResults.FailedCount -gt 0) {
Write-Host "There were $($TestResults.FailedCount) failed tests in ${agentType} $imageName"
$testFailed = $true
Write-Host "There were $($TestResults.FailedCount) failed tests out of $($TestResults.TotalCount) in ${imageName}"
$failed = $true
} else {
Write-Host "There were $($TestResults.PassedCount) passed tests out of $($TestResults.TotalCount) in ${agentType} $imageName"
Write-Host "There were $($TestResults.PassedCount) passed tests in ${imageName}"
}

Remove-Item env:\AGENT_TYPE
Remove-Item env:\AGENT_IMAGE
Remove-Item env:\IMAGE_NAME
Remove-Item env:\VERSION
}

function Publish-Image {
param (
[String] $Build,
[String] $ImageName
)
if ($DryRun) {
Write-Host "= PUBLISH: (dry-run) docker tag then publish '$Build $ImageName'"
} else {
Write-Host "= PUBLISH: Tagging $Build => full name = $ImageName"
docker tag "$Build" "$ImageName"

Write-Host "= PUBLISH: Publishing $ImageName..."
docker push "$ImageName"
}
return $failed
}


$originalDockerComposeFile = 'build-windows.yaml'
$finalDockerComposeFile = 'build-windows-current.yaml'
$baseDockerCmd = 'docker-compose --file={0}' -f $finalDockerComposeFile
$baseDockerBuildCmd = '{0} build --parallel --pull' -f $baseDockerCmd

foreach($agentType in $AgentTypes) {
# Ensure remaining env vars used in the docker compose file are defined
$env:AGENT_TYPE = $agentType
$env:DOCKERHUB_REPO = $Repository[$agentType]

# Temporary docker compose file (git ignored)
Copy-Item -Path $originalDockerComposeFile -Destination $finalDockerComposeFile
$repository = $InboundAgentRepository
# If it's a type "agent", set corresponding target and repository
# If it's an "agent" type, add the corresponding target
if($agentType -eq 'agent') {
yq '.services.[].build.target = \"agent\"' $originalDockerComposeFile | Out-File -FilePath $finalDockerComposeFile
$repository = $AgentRepository
}

$builds = @{}

Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object {
$image = '{0}-{1}-{2}' -f $_, $env:WINDOWS_FLAVOR, $env:WINDOWS_VERSION_TAG # Ex: "jdk17-nanoserver-1809"

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

$versionTag = "${RemotingVersion}-${BuildNumber}-${image}"
$tags = @( $image, $versionTag )

# Additional image tag without any 'jdk' prefix for the default JDK
$baseImage = "${env:WINDOWS_FLAVOR}-${env:WINDOWS_VERSION_TAG}"
if($jdkMajorVersion -eq "$defaultJdk") {
$tags += $baseImage
$tags += "${RemotingVersion}-${BuildNumber}-${baseImage}"
}

$builds[$image] = @{
'Tags' = $tags;
$compose = Invoke-Expression "$baseDockerCmd config --format=json" 2>$null | ConvertFrom-Json
foreach ($service in $compose.services.PSObject.Properties) {
$builds[$service.Value.image] = @{
'AdditionalTags' = $service.Value.build.tags;
}
}

Write-Host "= PREPARE: List of $Organisation/$repository images and tags to be processed:"
Write-Host "= PREPARE: List of $Organisation/$env:DOCKERHUB_REPO images and tags to be processed:"
ConvertTo-Json $builds
Invoke-Expression "$baseDockerCmd config"

Write-Host "= BUILD: Building all images..."
if ($DryRun) {
Write-Host "(dry-run) $baseDockerBuildCmd"
} else {
Invoke-Expression $baseDockerBuildCmd
switch ($DryRun) {
$true { Write-Host "(dry-run) $baseDockerBuildCmd" }
$false { Invoke-Expression $baseDockerBuildCmd }
}
Write-Host "= BUILD: Finished building all images."

Expand All @@ -204,8 +179,6 @@ foreach($agentType in $AgentTypes) {
} else {
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..."
Expand All @@ -230,10 +203,11 @@ foreach($agentType in $AgentTypes) {
$configuration.Output.Verbosity = 'Diagnostic'
$configuration.CodeCoverage.Enabled = $false

Write-Host "= TEST: Testing all images..."
Write-Host "= TEST: Testing all ${agentType} images..."
# Only fail the run afterwards in case of any test failures
$testFailed = $false
foreach($image in $builds.Keys) {
Test-Image ('{0}|{1}' -f $agentType, $image)
$testFailed = $testFailed -or (Test-Image ('{0}|{1}' -f $agentType, $image))
}

# Fail if any test failures
Expand All @@ -247,20 +221,15 @@ foreach($agentType in $AgentTypes) {
}

if($target -eq "publish") {
# Only fail the run afterwards in case of any issues when publishing the docker images
$publishFailed = 0
foreach($b in $builds.Keys) {
foreach($tag in $Builds[$b]['Tags']) {
Publish-Image "$b" "${Organisation}/${Repository}:${tag}"
if($lastExitCode -ne 0) {
$publishFailed = 1
}
}
Write-Host "= PUBLISH: push all images and tags"
switch($DryRun) {
$true { Write-Host "(dry-run) $baseDockerCmd push" }
$false { Invoke-Expression "$baseDockerCmd push" }
}

# Fail if any issues when publising the docker images
if($publishFailed -ne 0) {
Write-Error "Publish failed for ${Organisation}/${repository}!"
if($lastExitCode -ne 0) {
Write-Error "= PUBLISH: failed!"
exit 1
}
}
Expand Down

0 comments on commit 2cda621

Please sign in to comment.