Setup continuous integration for GitHub hosted .net project in Jenkins

In this tutorial, we will go through how you can setup an Automated build for your GitHub hosted .NET project in Jenkins each time you push new code.

If you don’t have Jenkins setup with IIS, you can do so by following my previous post.

For this tutorial, I am going to use my own GitHub hosted FakeRest project, but you can basically do the same for yours.

Prerequisites:

  • Jenkins hosted on a Windows server ( for MSBuild to work)
  • Git Plugin for Jenkins
  • Able to add Services under Integration & Services in Settings of GitHub hosted project. Basically a Hook.

Let’s start.

Install Git on the server

Go to: Git-Scm, download and install on the server.

Install Git Plugin for Jenkins

Go to Manage Jenkins -> Manage Plugin -> Available and search for “Git Plugin” and Install it.

Configure Git location in Jenkins

Go to Manage Jenkins -> Global Tool Configuration.

Under the Git section provided the git location from above installation. It should something like below:

jenkins-git-location-configuration

jenkins-git-location-configuration


You can also configure it to be installed Automatically.

 

Install MSBuild Plugin for Jenkins

Go to Manage Jenkins -> Manage Plugin -> Available and search for “MsBuild” and Install it. Wiki

Install MSBuild on your build server

Download “Build Tools for Visual Studio 2017” from https://www.visualstudio.com/downloads/

You can find it under “Other Tools and Framework”. Install it on the build server.

Once done, go to Manage Jenkins -> Global Tool Configuration and find “MSBuild” section and provide location path for msbuild.exe

It would be  “C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin”

msbuild-path

msbuild-path


Setup a new  FreeStyle Project in Jenkins

From Jenkins homepage create a new Item of type “FreeStyle Project”, provide a name and click OK.

new-freestyle-project

new-freestyle-project


On the next page, under “Source Code Management” choose “Git” and provide git project URL and credential.

source-code-management-git

source-code-management-git


Under the “Build Trigger” select “Poll SCM”, GitHub will use this setting to post information about a push to Jenkins. You can leave the schedule empty.

build-trigger-poll-scm

build-trigger-poll-scm


Setup Nuget restore

You will need to NuGet install on the server. With Visual Studio 2017 Nuget comes prepacked but on the build server, there is no Visual Studio so we have to download it. Download from: Nuget

Store the exe somewhere on the server.

Under the Build, section Add a new “Execute Windows batch command” Build Step. and set it up like shown in the screenshot below. Replace the location of nuget.exe with yours. Provide the name of the Solution. This will restore all the nuget packages your solution requires.

build-restore-nuget

build-restore-nuget


Next, Create a new build step of type “Build a visual studio project or solution using MSBuild”. Under the MSBuild version choose the one we added previously. Provide your Project file (NOT solution file) to build. And we will use “/P:Configuration=Release” as command line Arguments to create a release build.

build-msbuild-step

build-msbuild-step


And that’s it for Jenkins. Note, Order of the Build Step is important. Make sure Nuget Restore step is before actual Build step.

Create GitHub Hook (Service)

Go to your GitHub repository -> Settings -> Integrations & services.

Click “Add Service” and Choose Jenkins (Git Plugin) from the pop-up menu.

git-jenkins-service

git-jenkins-service


On the next step, provide you Jenkins Url and that’s it. Github and Jenkins take care of everything else.

git-jenkins-service2

git-jenkins-service2


Now, each time you push something, GitHub will trigger a new Jenkins build for the project setup.

 

Sandeep

a dev, an amateur photographer and a father

 

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: