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

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.
`

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.
go into the Config of one of your freestyle jobs that uses the MSBuild plugin
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.
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.)
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.)
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.
Congratulations you can now configure your build! Replace SolutionName.sln with your build file and pass the correct parameters to it.

I am surprised that all solutions doesn't work for me.
Platform : Window 10 Jenkins at latest version in 2020-11-05.
I am not sure if there's any reason that window & other OS will cause the pipeline script engine behaved differently.
Below are the possible encountered issues and solution finally combined with the above solution.
in my compiler, a single '' will cause error, '' must be escaped by replaced by '\'. which is similar to other char such as '"'
${tool 'MSBuild'} OR ${tool 'MSBuild 15.0'}\msbuild\ does not work, the error is the path cannot specified or cannot find the bat program. It must be an absolute path of the actual msbuild.exe
So this is the working solution for adding MSbuild in the pipeline script in window platform's jenkin
bat '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild" YourSolution.sln /p:Configuration=Release /p:Platform="Any CPU" /p:ProductVersion=1.0.0.${env.BUILD_NUMBER}'

Related

Setting up jenkins slave/node global tools

I am fairly new to CI.
I am receiving the following msbuild error when trying to run my build on Dobby (my other Windows slave/node):
FATAL: \msbuild.exe doesn't exist
However, my Master's Global Tools path is set to:
Name: MSBuild v4.5.1
Path to MSBuild: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe
And on Dobby under Configure --> Node Properties I've added the tool locations as written above.
My question is will Dobby be able to execute the MSBuild from the Master server's file location? Or would I need to add tools to the Jenkins Home --> tools location in order for it to be recognized globally?
Let me know if you have any questions, thanks!
If I understand your question correctly then you would have to have the MSBuild set up on the node. When we set this up we used the absolute file path in the build. The code snippet below is from a Jenkinsfile (declarative pipeline) so we had to escape the backslashes.
bat 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\msbuild .\\solution_file.sln'
Typically with the master/agent setup in Jenkins you want to make sure the agent contains all of the information and tools necessary to handle the job.

MSBuild configuration in jenkins

I am facing the error below during configuration of MSBuild in Jenkins:
[ImageResize] $ cmd.exe /C " msbuild.exe p:Configuration=Release
E:\Heena\Applications\ImageResize\vbimage\ImageResize.sln " && exit
%%ERRORLEVEL%% 'msbuild.exe' is not recognized as an internal or
external command, operable program or batch file. Build step 'Build a
Visual Studio project or solution using MSBuild' marked build as
failure Finished: FAILURE
Please note that I have set MSBuild path up to msBuild.exe during setting up of MSbuild plugin. I have successfully deployed Visual Studio project on jenkins, but unable to configure it with MSBuild.
In MSBuild installations option of jenkins,
Choose the path to MSBuild = C:\Windows\Microsoft.NET\Framework64\v4.0.30319
Then go to your respective jenkins project where you want to build,
from,Add Build Step menu choose
Build Visual Studio Project or solution using MSBuild option
MSBuild version will be what you defined in Step 1 and in MSBuild File
type the path of your solution file and in Command Line Arguments option
type /p:Configuration=Release.
Hope you got the answer what you were looking for.
A Guess from my side , msbuild.exe is not part of the PATH when you execute the msbuild command , check that before you run the command , try to export the the path to msbuild to the path variable.
Looking at the error i assume you are using windows/Batch , so try some thing like this before running msbuild
set PATH=%PATH%;path/to/msbuild/binary
Feel free to revert in case of any issues/concerns

Configure Jenkins to run Visual Studio bat file before Maven job

I have a Maven module that utilizes the NAR plugin to build some JNI libraries, and in Jenkins I have configured a Maven project to build this module. In order for these libs to be built, the Visual Studio bat file that sets up the path and other environment variables must be run. I have tried several different ways to get this bat file to execute before the Maven commands are called, and none of them have worked correctly. I know that Jenkins isolates all of the build process steps, so it can be difficult to get the environment set up, but I'm hoping someone has solved this particular issue. Here's what I've tried:
calling the bat file as a pre build step.
Using the EnvInject plugin to call the bat file, both as a pre build step and as a pre job step.
Setting the environment variables directly without calling the bat file using EnvInject.
calling the bat file from the mvn.bat file (this failed because it appears Jenkins will call Maven directly, without using the bat file).
As a workaround, I'm using the Freeform project type and setting the build steps to
call the bat file.
Directly call maven with appropriate parameters.
This works, but it's not as nice as using the Maven project type, ex. a failed unit test will fail the entire build instead of just sending a warning. Is there a way to configure this as a Maven project?

How can one determine if a csproj is being run on a TFS build agent?

We use TFS 2010.
There are a couple of projects with deployment steps which must know whether they are running on a dev machine or on the TFS build agent.
Right now they check whether the build is from within Visual Studio assuming that only devs compile from VS. Alas, it means I cannot compile from the command line!
So, my question is how an msbuild script can determine if it is being run by the TFS build agent?
You have a few options:
'$(BuildingInsideVisualStudio)' != ''
'$(TeamBuildConstants)' != '' (supported in team Build 2008)
'$(IsDesktopBuild)' == 'false'
'$(tf_build)' != '' (supported in recent versions of Azure Pipelines)
You can check either one to detect the context the task has been executed in. If both don't evaluate then MsBuild has been called from the commandline or some other process.
I have TFS2012 and use this:
<IsTfsServerBuild Condition=" '$(IsTfsServerBuild)' == '' ">false</IsTfsServerBuild>
<IsTfsServerBuild Condition=" '$(BuildingInsideVisualStudio)' != 'true' AND '$(BuildUri)' != '' ">true</IsTfsServerBuild>
When calling MSBuild from the command line, you can pass/overwrite properties like this:
# Simulate Visual Studio build
. msbuild.exe Project.csproj /p:BuildingInsideVisualStudio=true [...]
# Custom property
. msbuild.exe Project.csproj /p:MyCustomProperty=true [...]
Is use these to check them in my post/afterbuild events.
I thought to provide my own answer updated to 2020.
The way we do it is by checking an environment variable, because TFS (now Azure DevOps) exposes build variables through the environment. When tested in msbuild code they look the same build variables passed on the msbuild command line, but we also employ powershell scripts, which rely only on the environment.
Also, I would sometimes like to distinguish between the Build and the Release pipeline, because in the classic Release pipeline (as opposed to yaml, which we do not have on prem) some things do not work the same (like vsts logging commands)
So, here is what we are using:
BUILD_BUILDNUMBER - exists in both build and release, because all of our releases include a build artifact
RELEASE_ENVIRONMENTNAME - exists only in the classic release pipeline
TF_BUILD - not sure if exists in release, definitely in build
I would not bother with BuildingInsideVisualStudio, because it is false when build with msbuild on the command line, yet it is not a build on the CI server.

Jenkins on Windows using something other than ant

I am looking at using Jenkins on Windows.
I currently have an ant script. It works pretty well. Except for instance, when the build breaks because of a syntax error, I can not see the error in the Jenkins console log.
So I am thinking may be ant is not the best tool for use on Windows.
What do most of you use for Jenkins on Windows?
If it is ant, how do you send the build output, VStudio, to the Jenkins console?
Thanks
Primarily I use Jenkins with maven projects, although you should see the ANT output in the online logs regardless.
What are you building? Are you building Java projects? Ant is what you use. If you are bulding a C project, you should use Make. If you're building a VisualStudio project, you should use msbuild. You use the build tool for your project. Jenkins will execute them without a problem.
Take a look at the build in question. On the left side of the screen, there's a Console output item. Click on that. Is there any output. No matter what tool you use, Jenkins captures the STDOUT and STDERR in that console output. If nothing else, you should see the exact commands Jenkins is executing to checkout and to build your project. Try executing those commands.
Still, you didn't give us much to go on. No idea what you're building or what you're doing with Ant. You didn't state any error, the console output, or even what the Jenkins error log is stating.
Jenkins does two things:
It watches your repository for changes.
Once it detects the changes, it executes the very commands you'd execute to build the project.
Jenkins doesn't care whether you use Ant, Maven, Make, or simply do a del /s/q. Jenkins will simply execute the commands you tell it to execute.
Addendum
It is c, c++, Java and InstallShield. I use ant to do file copy and move, call msdev.exe project. Some Javac calls, InstallShield command line builds..
Jenkins can execute multiple step builds in a single job. After you specify the build step, you can press the Add button to add another build step. There's no reason that all the build steps even have to be of the same type. Just select a "Freestyle" build, and use the right build tools for the job.
There's an optional MSBUILD plugin in Jenkins that should do your MS Build. This should give you the complete output from MSBuild, so you can see any errors.
After you do your MSBuild step, you can create a second build step to run an Ant task to build your InstallShield. After that, you could run another build step to do the copying you need either on the command line or through something like Ant (or Nant.
Whatever the output of the various tools is the output you'll get in the build console.
dev.cmd shows the output and I continue to use ant.

Resources