Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Jenkins MSBuild plug-in in a Jenkinsfile?

I have Jenkins v2.60.3 with the MSBuild Plugin v1.27 installed on Windows.

I have configured the path to my msbuild.exe in Jenkins' Global Tool Configuration. I have also setup a Multi Branch Pipeline in Jenkins that picks up a Jenkinsfile from git repo successfully.

My question is: How do I invoke the MSBuild Plugin as a step in my Jenkinsfile?

Please note I know I can invoke msbuild.exe directly as a Windows batch step but I prefer to go through the MSBuild Plugin if possible. `

like image 279
urig Avatar asked Sep 06 '17 20:09

urig


People also ask

How do I use MSBuild plugin in Jenkins?

Install the MSBuild Plugin for Jenkins: Open Jenkins, http://localhost:8080/ Navigate to “Manage Jenkins” Navigate to “Manage Plugins”Direct link: http://localhost:8080/pluginManager. Use the “filter” box to search for “MSBUILD plugin” and install the plugin.

Does Jenkins use MSBuild?

Usage. To use this plugin, specify the location directory of MSBuild.exe on Jenkin's configuration page. The MSBuild executable is usually situated in a subfolder of C:\WINDOWS\Microsoft.NET\Framework.

How use MSBuild command line?

To run MSBuild at a command prompt, pass a project file to MSBuild.exe, together with the appropriate command-line options. Command-line options let you set properties, execute specific targets, and set other options that control the build process.


Video Answer


2 Answers

It looks like MSBuild is not supported by pipeline yet https://github.com/jenkinsci/pipeline-plugin/blob/master/COMPATIBILITY.md

You can try this in the meantime. https://github.com/jenkinsci/pipeline-examples/blob/master/jenkinsfile-examples/msbuild/Jenkinsfile

like image 96
mjd Avatar answered Sep 23 '22 01:09

mjd


Our teams need to migrate a ton of freestyle MSBuild jobs that were created in the UI. mjd's answer helped but still left me scratching my head. The examples just didn't work... until I figured out the disconnect.

Here's the secret sauce:

You have to call the "named msbuild configuration" directly using the "bat" and "tool" commands.

1) go into the Config of one of your freestyle jobs that uses the MSBuild plugin

2) scroll down to the msbuild section and click the "MSBuild Version" drop down, take note of the exact names that are listed. This is your 'named msbuild configuration'. Choose one name that you will use in the next step. enter image description here

3) open your jenkinsfile, locate the stage and step where you want to call msbuild, then add this line and replace 'MSBuild 15.0' with the name that you chose in step 2:

bat "\"${tool 'MSBuild 15.0'}\msbuild\" SolutionName.sln /p:Configuration=Release /p:Platform=\"Any CPU\" /p:ProductVersion=1.0.0.${env.BUILD_NUMBER}"

like so...

enter image description here

(For Declarative Pipelines, you'll need to put this inside of a "script" block. If you don't know what a 'Declarative Pipeline' is, it is one of two styles of writing pipeline scripts in Jenkins using the "groovy" language. For more info here is a comparison of the scripted vs declarative styles.)

4) run the pipeline and examine the output - the code you added in step three won't build anything, you just want to use it to see if msbuild will actually get called before investing anymore time into my script.

(I usually use the Replay button which allows me to edit the script online in Jenkins rather than editing, committing, and pushing to remote repo... it just saves a bit of time debugging.)

enter image description here

5) examine the output of the pipeline job you ran in step 4. You should see something like below indicating that the correct version of MSBuild was called. If not, you either have a typo or your administrator needs to intervene.

workspace\Pipeline_Test>"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\msbuild" SolutionName.sln /p:Configuration=Release /p:Platform="Any CPU" /p:ProductVersion=1.0.0.308 Microsoft (R) Build Engine version 15.9.21+g9802d43bc3 for .NET Framework Copyright (C) Microsoft Corporation. All rights reserved.

6) Congratulations you can now configure your build! Replace SolutionName.sln with your build file and pass the correct parameters to it.

like image 27
neoscribe Avatar answered Sep 27 '22 01:09

neoscribe