Jenkins Docker Slave Windows Build Agent

· Read in about 3 min · (502 words) ·

I spent quite some time on getting this working on Windows with Windows Docker slaves.

In the end it turned out to be mostly matter of using correct Jenkins docker plugin.

Jenkins docker slave

  • Use Yet Another Docker Plugin
    • With this plugin it just works (Docker Plugin doesn’t work)
  • Install JAVA into docker image and make it available on PATH
  • In Configuration add Yet Another Docker Plugin as a Cloud
    • Point it to Docker server
    • Test that it is available
    • Add the docker image
    • Add label which can be references from builds
  • Jenkins now automatically creates a docker container as a build is executed
    • It does not require any additional changes
    • It properly handles the operating system (Windows)
  • For local only images make sure that the Pull option is set to Never

Base Dockerfile

  • This Dockerfile creates the base image for Windows build agent
  • The only requirements is that java.exe must be accessible in path.

    • It could be perhaps even installed using Chocolatey

      FROM microsoft/windowsservercore
      
      SHELL ["powershell.exe", "-ExecutionPolicy", "Bypass", "-Command"]
      
      RUN iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
      RUN choco install -y git --params='/NoShellIntegration'
      RUN choco install -y nuget.commandline
      
      ENV JAVA_HOME c:\\jre1.8.0_91
      ENV JENKINS_HOME c:\\jenkins
      
      RUN (new-object System.Net.WebClient).Downloadfile('http://javadl.oracle.com/webapps/download/AutoDL?BundleId=210185', 'C:\jre-8u91-windows-x64.exe')
      RUN start-process -filepath C:\\jre-8u91-windows-x64.exe -passthru -wait -argumentlist "/s,INSTALLDIR=$env:JAVA_HOME,/L,install64.log"
      RUN del C:\jre-8u91-windows-x64.exe
      
      RUN $env:PATH = $env:JAVA_HOME + '\\bin;' + $env:PATH; \
      	[Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::Machine);
      
      RUN mkdir $env:JENKINS_HOME
      WORKDIR $JENKINS_HOME
      
      ENTRYPOINT ["cmd.exe"]
      

Visual Studio 2017 Dockerfile

  • This Dockerfile uses the above as the base image and installs VS 2017

    • It add SDK for .NET 4.5.2 and 4.6.1

      FROM vkloz/winjava
      
      SHELL ["powershell.exe", "-ExecutionPolicy", "Bypass", "-Command"]
      
      ENV TEST_CONTAINER=1 \
      VS_CHANNEL_URI=https://aka.ms/vs/15/release/799c44140/channel \
      VS_BUILDTOOLS_URI=https://aka.ms/vs/15/release/799c44140/vs_buildtools.exe \
      VS_BUILDTOOLS_SHA256=FA29EB83297AECADB0C4CD41E54512C953164E64EEDD9FB9D3BF9BD70C9A2D29
      
      # Download log collection utility
      RUN $ErrorActionPreference = 'Stop'; \
      $ProgressPreference = 'SilentlyContinue'; \
      $VerbosePreference = 'Continue'; \
      Invoke-WebRequest -Uri https://aka.ms/vscollect.exe -OutFile C:\collect.exe
      
      # Download vs_buildtools.exe
      RUN $ErrorActionPreference = 'Stop'; \
      $ProgressPreference = 'SilentlyContinue'; \
      $VerbosePreference = 'Continue'; \
      Invoke-WebRequest -Uri $env:VS_BUILDTOOLS_URI -OutFile C:\vs_buildtools.exe; \
      if ((Get-FileHash -Path C:\vs_buildtools.exe -Algorithm SHA256).Hash -ne $env:VS_BUILDTOOLS_SHA256) { throw 'Download hash does not match' }
      
      # https://docs.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-build-tools
      # Install Visual Studio Build Tools
      RUN $ErrorActionPreference = 'Stop'; \
      $VerbosePreference = 'Continue'; \
      $p = Start-Process -Wait -PassThru -FilePath C:\vs_buildtools.exe -ArgumentList '--quiet --nocache --wait --installPath C:\BuildTools'; \
      if ($ret = $p.ExitCode) { c:\collect.exe; throw ('Install failed with exit code 0x{0:x}' -f $ret) }
      
      RUN $ErrorActionPreference = 'Stop'; \
      $VerbosePreference = 'Continue'; \
      $p = Start-Process -Wait -PassThru -FilePath C:\vs_buildtools.exe -ArgumentList 'modify --quiet --nocache --wait --installPath C:\BuildTools --add Microsoft.VisualStudio.Workload.MSBuildTools'; \
      if ($ret = $p.ExitCode) { c:\collect.exe; throw ('Install failed with exit code 0x{0:x}' -f $ret) }
      
      RUN $ErrorActionPreference = 'Stop'; \
      $VerbosePreference = 'Continue'; \
      $p = Start-Process -Wait -PassThru -FilePath C:\vs_buildtools.exe -ArgumentList 'modify --quiet --nocache --wait --installPath C:\BuildTools --add Microsoft.VisualStudio.Component.Static.Analysis.Tools'; \
      if ($ret = $p.ExitCode) { c:\collect.exe; throw ('Install failed with exit code 0x{0:x}' -f $ret) }
      
      RUN choco install -y netfx-4.5.2-devpack
      RUN choco install -y netfx-4.6.1-devpack
      
      
      # Use shell form to start developer command prompt and any other commands specified
      SHELL ["cmd.exe", "/s", "/c"]
      ENTRYPOINT C:\BuildTools\Common7\Tools\VsDevCmd.bat &&
      
      # Default to PowerShell console running within developer command prompt environment
      CMD ["powershell.exe", "-nologo"]