Hello everyone, we are back with one more DevOps topic. Jenkins is a very popular CI/CD tool in the DevOps world. Today we will talk about how to build a visual studio project in Jenkins. As it is open source, you may face some issues when you try to build the visual studio project in Jenkins first time. If you are also facing that issue, you are at the right place. I will explain step by step procedure to build a visual studio project in Jenkins.
Before Proceeding, please make sure you have fulfilled the below pre-requites to complete this topic:
- Jenkins server is already installed on Linux/Windows VM and up and running.
- Configure a windows agent under Jenkins Nodes
- Install MS Build software on the Windows machine.
Once the above three prerequisites are in place, we are good to move ahead with our topic. Let’s just dig in. Below is the main list of tasks that we need to complete to build a visual studio project in Jenkins.
- Install MS Build Plugin in Jenkins
- Configure MS Build Plugin
- Create a Free Style Project
- Restrict the job to Windows Agent
- Add Git Repository
- Add Build Steps
- Archive the Artifacts.
- Build the project.
Install MS Build Plugin in Jenkins
The First step is to install the MS Build plugin in Jenkins.
- Click on Manage Jenkins
- Select Manage Plugins
- Click on Available
- Search with the term: “msbuild”
- Click on Install without restart
- This plugin makes it possible to build a Visual Studio project (.proj) and solution files (.sln).
Configure MS Build Plugin
After installing the msbuild plugin, next step is to configure it so that we can use it in our jenkins pipeline.
- From our Jenkins Dashboard, click on Manage Jenkins.
- Click on Global Tool Configuration.
- Scroll Down till you find MSBuild written.
- Click on MSBuild Installations.
- Click on Add MSBuild. This is basically telling jenkins where exactly the msbuild has been installed on our agent.
- Give a name to the installation. Name can be any. You can specify the Visual Studio version also here if any specific version has been installed.
- Paste the msbuild installation path in the Path to MSBuild box.
- Click on Save
Create A Folder and A Project
Our MSBuild plugin has been configured now and ready to use in our project. Let’s create a project now:
- Click on New Item.
- Enter “Visual Studio Project” (any name) and Select Folder.
- Click on OK
- In next Screen, just click on Save
- Now go into the newly created folder “Visual Studio Project” and Click on New Item.
- Now give this project a name and select Freestyle project.
- Click OK.
Restrict the Job to Windows Agent
As you know that we have visual studio installed on a windows agent, hence in the pipeline project also, we need to configure the pipeline to run on windows agent only so that it can be built successfully. To configure that, follow below steps:
- Click on your project which you newly created in the previous step. Click on Configure
- After Description box, there would be checkboxes, just check the “Restrict where this project can be run“
- In the Label Expression, enter the label name you have given to your windows agent. like below:
Add Git Repository
Under Source Code Management, we need to give the source location where we have our code. This code is basically Visual Studio solution files which are stored on a git repo.
- Under Source Code Management, Select Git
- In Repository URL, you can give repo link. If you don’t have any, you can use below one with one sample visual studio project: https://github.com/amantcs/JenkinsAPP2.git
- In the above step, if you get error “Failed to Connect to Repository Error Performing Git Command Git Ls-Remote“, then pls check out this link
- In Branch to build, you can leave it as default value “*/master” or specify your own branch which you want to build.
Add Build Steps
This is very important step. Please follow each and every step very carefully in this section.
- Under Build, click on Add build step, A drop down menu will pop up.
- Select Execute Windows Batch command.
- Under command box, enter “dotnet restore”
- Now click again on Add Build step.
- Select Build a Visual Studio project or solution using MSBuild
- Under MSBuild Version, select the name you gave to msbuild installation during msbuild configuration step.
- Next, give the .sln file name under MSBuild Build File. for example: JenkinsAPP.sln
We have successfully added all the build steps required to build a Visual Studio solution. Now it is time to save the artifacts which will be produced as part of build. for that, please follow below:
- Under Post-build Actions, click on Add post-build action.
- From drop down, select Archive the artifacts
- In Files to archive field, add path to the location of dll files which would be generated after build. For example: JenkinsAPP/bin/Debug/netcoreapp3.1/*.*archive the artifacts
Build The Project
After completing all the above steps, now you can click on Save. Our pipeline is ready now. Click on Build Now to build the project. You will see a progress bar:
You can click on console output to see real time processing data from console. Once finished, you will see SUCCESS in the end of console output. On project home page, you will see the artifacts files as below.
Congratulations!! You have successfully built the Visual Studio project in Jenkins CI/CD tool and produced the artifacts. You can use these artifacts for deployment later. If you face any issue while following above steps, please do let us know in the comments, we will try our best to help you out. Goodbye!
Read more about Jenkins here.