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.
`
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.
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.
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.
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
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.
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...
(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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With