Recommendations for MSBuild in a Jenkins pipeline

MSBuild has a lot of command line options that are relevant when building in a Jenkins pipeline.

A useful command argument to set is /nodeReuse:false. This ensures that MSBuild.exe exits after being called. The default is true and is meant to reduce the startup time for subsequent builds. In a Jenkins pipeline, this isn't really useful since all processes are killed off at the end of a build. In fact, the default true option can be problematic in cases where you have later stages in your pipeline that might do a cleanup or a git clean -fdx but can't because a MSBuild.exe process is holding a file open (like a DLL from a Nuget package).

At work we use declarative pipelines for everything and based on our experience building Visual Studio projects, a simple pipeline would look something like this:

#!/usr/bin/env groovy

pipeline {
  environment {
    MSBUILD = "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin"
    CONFIG = 'Release'
    PLATFORM = 'x64'
  }
  stages {
    stage('Build') {
      steps {
        bat "NuGet.exe restore your_project.sln"
        bat "\"${MSBUILD}\" your_project.sln /p:Configuration=${env.CONFIG};Platform=${env.PLATFORM} /maxcpucount:%NUMBER_OF_PROCESSORS% /nodeReuse:false"
      }
    }
  }
}

It's pretty bare bones, and doesn't include tests, but you get the idea.

Show Comments