Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
refactor builds like jenkinsci/docker-agent#445
Browse files Browse the repository at this point in the history
  • Loading branch information
lemeurherve committed Jul 10, 2023
1 parent 1ded0b2 commit 0c44c26
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 149 deletions.
154 changes: 67 additions & 87 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,103 +3,83 @@ pipeline {

options {
buildDiscarder(logRotator(daysToKeepStr: '10'))
timestamps()
}

stages {
stage('Build') {
parallel {
stage('Windows') {
agent {
label "docker-windows"
stage('docker-inbound-agent') {
failFast true
matrix {
axes {
axis {
name 'AGENT_TYPE'
values 'linux', 'windows-2019'
}
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() }
}
steps {
powershell '& ./build.ps1 test'
}
post {
always {
junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/**/junit-results.xml')
}
}
}
stages {
stage('Main') {
agent {
label env.AGENT_TYPE
}
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'
}
}
}
options {
timeout(time: 30, unit: 'MINUTES')
}
}
}
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
'''
}
environment {
DOCKERHUB_ORGANISATION = "${infra.isTrusted() ? 'jenkins' : 'jenkins4eval'}"
}
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')
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
'''
}
}
}
stage('Deploy to DockerHub') {
// This stage is the "CD" and should only be run when a tag triggered the build
when {
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 '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'
} else {
powershell "& ./build.ps1 test"
}
}
}
post {
always {
junit(allowEmptyResults: true, keepLongStdio: true, testResults: 'target/**/junit-results.xml')
}
}
}
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
'''
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 {
if (isUnix()) {
sh '''
export IMAGE_TAG="${TAG_NAME}"
export ON_TAG=true
docker buildx bake --push --file docker-bake.hcl linux
'''
} else {
// TODO: check if this function has the same beahvior in build.ps1 vs make.ps1
powershell "& ./build.ps1 -PushVersions -VersionTag $env:TAG_NAME publish"
}
}
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions build-windows.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
services:
jdk11-nanoserver-1809:
image: jdk11-nanoserver-1809
jdk11-nanoserver:
image: jdk11-nanoserver-${NANOSERVER_VERSION_NAME}
build:
context: ./windows/
dockerfile: Dockerfile.nanoserver
args:
VERSION: ${DOCKER_AGENT_VERSION}
jdk17-nanoserver-1809:
image: jdk17-nanoserver-1809
WINDOWS_VERSION_TAG: ${NANOSERVER_VERSION_TAG}
jdk17-nanoserver:
image: jdk17-nanoserver-${NANOSERVER_VERSION_NAME}
build:
context: ./windows/
dockerfile: Dockerfile.nanoserver
args:
VERSION: ${DOCKER_AGENT_VERSION}
jdk11-windowsservercore-ltsc2019:
image: jdk11-windowsservercore-ltsc2019
WINDOWS_VERSION_TAG: ${NANOSERVER_VERSION_TAG}
jdk11-windowsservercore:
image: jdk11-windowsservercore-${WINDOWS_VERSION_NAME}
build:
context: ./windows/
dockerfile: Dockerfile.windowsservercore
args:
VERSION: ${DOCKER_AGENT_VERSION}
jdk17-windowsservercore-ltsc2019:
image: jdk17-windowsservercore-ltsc2019
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
jdk17-windowsservercore:
image: jdk17-windowsservercore-${WINDOWS_VERSION_NAME}
build:
context: ./windows/
dockerfile: Dockerfile.windowsservercore
args:
VERSION: ${DOCKER_AGENT_VERSION}
WINDOWS_VERSION_TAG: ${WINDOWS_VERSION_TAG}
24 changes: 21 additions & 3 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ Param(
# [switch] $DisableEnvProps = $false
)

$ErrorActionPreference = "Stop"
$ErrorActionPreference ='Stop'
$Repository = 'inbound-agent'
$Organization = 'jenkins'
$AgentType = 'windows-2019'

# TODO: not needed? Commented for now, env.props contains DOCKER_AGENT_VERSION in docker-agent
# if(!$DisableEnvProps) {
Expand All @@ -38,6 +39,10 @@ if(![String]::IsNullOrWhiteSpace($env:DOCKER_AGENT_VERSION)) {
$DockerAgentVersion = $env:DOCKER_AGENT_VERSION
}

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

# Check for required commands
Function Test-CommandExists {
# From https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
Expand All @@ -64,6 +69,15 @@ Function Test-CommandExists {
$defaultJdk = '11'
$builds = @{}
$env:DOCKER_AGENT_VERSION = "$DockerAgentVersion"
$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
}
$ProgressPreference = 'SilentlyContinue' # Disable Progress bar for faster downloads

Test-CommandExists "docker"
Expand All @@ -74,7 +88,9 @@ $baseDockerCmd = 'docker-compose --file=build-windows.yaml'
$baseDockerBuildCmd = '{0} build --parallel --pull' -f $baseDockerCmd

Invoke-Expression "$baseDockerCmd config --services" 2>$null | ForEach-Object {
$image = $_
$image = '{0}-{1}' -f $_, $env:WINDOWS_VERSION_NAME
# Special case for nanoserver-1809 images
$image = $image.replace('nanoserver-ltsc2019', 'nanoserver-1809')
$items = $image.Split("-")
# Remove the 'jdk' prefix (3 first characters)
$jdkMajorVersion = $items[0].Remove(0,3)
Expand Down Expand Up @@ -119,7 +135,8 @@ function Test-Image {
Write-Host "= TEST: Testing image ${ImageName}:"

$env:AGENT_IMAGE = $ImageName
$env:IMAGE_FOLDER = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${ImageName}.build.context"
$serviceName = $ImageName.SubString(0, $ImageName.LastIndexOf('-'))
$env:IMAGE_FOLDER = Invoke-Expression "$baseDockerCmd config" 2>$null | yq -r ".services.${serviceName}.build.context"
# TODO: review build number removal (?)
# $env:VERSION = "$DockerAgentVersion-$BuildNumber"
$env:VERSION = $DockerAgentVersion
Expand Down Expand Up @@ -175,6 +192,7 @@ if($target -eq "test") {
if(![System.String]::IsNullOrWhiteSpace($Build) -and $builds.ContainsKey($Build)) {
Test-Image $Build
} else {
Write-Host "= TEST: Testing all images"
foreach($image in $builds.Keys) {
Test-Image $image
}
Expand Down
9 changes: 7 additions & 2 deletions tests/inboundAgent.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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' ''

# TODO: make this name unique for concurency
$global:CONTAINERNAME = 'pester-jenkins-inbound-agent-{0}' -f $global:AGENT_IMAGE
Expand All @@ -20,6 +21,10 @@ $global:WINDOWSVERSION = $items[2]
$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'
}
}


Expand All @@ -31,7 +36,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:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}"
$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 ${global:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}"
$exitCode | Should -Be 0
}
}
Expand Down Expand Up @@ -122,7 +127,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:IMAGE_FOLDER}/Dockerfile.${global:WINDOWSFLAVOR} ${global:IMAGE_FOLDER}"
$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=${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"
Expand Down
Loading

0 comments on commit 0c44c26

Please sign in to comment.