Skip to content

Instantly share code, notes, and snippets.

@martyngigg
Last active November 9, 2022 15:03
Show Gist options
  • Save martyngigg/e2cb75bd0a14ebcbbaf8f0d1c98e98bb to your computer and use it in GitHub Desktop.
Save martyngigg/e2cb75bd0a14ebcbbaf8f0d1c98e98bb to your computer and use it in GitHub Desktop.
Windows docker-based Jenkins Agent for C++ Builds

Docker-Based Jenkins Agent

Here I describe the steps to attempt a docker-based Jenkins agent on Windows, including Visual Studio 2019. This assumes we can use Docker Desktop.

Steps:

  • Install Docker Desktop for Windows with default settings
    • If installed with a separate Admin account, add your user account to the docker-users group:
      • Navigate to C:\Windows\System32 in file explorer, find lusrmgr, hold shift & right-click run as different user and enter admin user details
      • Go to groups, docker-users and add your account to the group.
  • Open powershell as an administrator and enable Hyper-V containers:
    • Enable-WindowsOptionalFeature -Online -FeatureName $("Microsoft-Hyper-V", "Containers") -All
  • Start Docker Desktop
    • Cancel any message about WSL2 as we will use Windows containers
    • While starting, right-click on the tray icon and click "Switch to Windows Containers"
  • Microsoft don't license VS build tools images for public use so we can't use a prebuilt image and instead the image must be built on each machine:
    • Copy Dockerfile and Install.cmd to your local machine
    • Change to the directory containing these files
    • Build the image: docker build -t mantid-builder -m 2GB (this will take some time as it has to install VS)
    • Run the container: docker run -m 2G -d --name [CONTAINER_NAME] buildtools2019native:latest -Url [JENKINS_URL] -Secret [JENKINS_SECRET] -Name [CONTAINER_NAME] (replace the [] variables with the real values, including the square brackets).
    • The container should show running under docker ps or in the GUI.
# escape=`
# Use JNLP agent as base and install required tools
ARG FROM_IMAGE=jenkins/inbound-agent:4.13-2-jdk11-windowsservercore-ltsc2019
FROM ${FROM_IMAGE}
# Reset the shell.
SHELL ["cmd", "/S", "/C"]
# Set up environment to collect install errors.
COPY Install.cmd C:\TEMP\
ADD https://aka.ms/vscollect.exe C:\TEMP\collect.exe
# Install Node.js LTS
ADD https://nodejs.org/dist/v8.11.3/node-v8.11.3-x64.msi C:\TEMP\node-install.msi
RUN start /wait msiexec.exe /i C:\TEMP\node-install.msi /l*vx "%TEMP%\MSI-node-install.log" /qn ADDLOCAL=ALL
# Download channel for fixed install.
ARG CHANNEL_URL=https://aka.ms/vs/16/release/channel
ADD ${CHANNEL_URL} C:\TEMP\VisualStudio.chman
# Download and install Build Tools for Visual Studio 2019 for native desktop workload.
ADD https://aka.ms/vs/16/release/vs_buildtools.exe C:\TEMP\vs_buildtools.exe
RUN C:\TEMP\Install.cmd C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache `
--channelUri C:\TEMP\VisualStudio.chman `
--installChannelUri C:\TEMP\VisualStudio.chman `
--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended`
--installPath C:\BuildTools
# Start the agent process
ENTRYPOINT ["powershell.exe", "-f", "C:/ProgramData/Jenkins/jenkins-agent.ps1"]
@rem Copyright (C) Microsoft Corporation. All rights reserved.
@rem Licensed under the MIT license. See LICENSE.txt in the project root for license information.
@if not defined _echo echo off
setlocal enabledelayedexpansion
call %*
if "%ERRORLEVEL%"=="3010" (
exit /b 0
) else (
if not "%ERRORLEVEL%"=="0" (
set ERR=%ERRORLEVEL%
call C:\TEMP\collect.exe -zip:C:\vslogs.zip
exit /b !ERR!
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment