Jenkins – Continuous Deployment of .NET Projects with Git Hub

Jenkins is free and open source automation server. It helps to automate tasks related to building, testing and delivering/deploying the software. Jenkins backed by powerful plugins to integrate with source control management software like GitHub and can be setup to automatically get the source code form GitHub repository, build it and autonomously.

Procedure to setup Jenkins to build and deploy .NET Projects.

This tutorial divided into four parts

  • Visual Studio Setup
  • Jenkin Setup
  • Git-hub Setup
  • Creating project

Visual Studio Setup

Download Visual Studio setup and follow the instructions below to complete setup of Build Environment.

Download Nuget Commandline and copy it to local directory. We will use it to restore Nuget Packages for your Visual Studio Solution.

Select required feature also as we are creating the environment to build the .NET Framework 4.7.1 Project, so select the Framework.

Visual Studio installation

Select  .NET Framework, Web Deploy, NuGet Package Manager features

Visual Studio installation

Jenkin Setup

Once VS download as completed, download and Install Jenkins from their Official Website. I recommend downloading the Long-term Support Build for better stability and support. The installation is quite straightforward, just continue through the steps and the installer with setup and start the service. At the end of installation, it will open Jenkins Admin Interface in the browser. If it fails to open, open http://localhost:8080/ in any browser.

Jenkin installation

first time, Jenkins will ask to enter Initial Admin Password.

  • Open Notepad
  • Open the File in Highlighted Path in Notepad
  • Copy the Long Password String in the File and Paste into the “Administrator password” field in the browser.
  • Click on “Select plugins to install”
Jenkin installation step 2

Search for “msbuild”  and “Git parameter”  plugins from search box and select them. Don’t change anything in default selected plugin. Click on Install to continue. It will take a while for Jenkins to download and install the plugins.

Jenkin installation step 3
Jenkin installation step 4

If any Plugin Installation Fails, Click on Retry. If it keeps failing, continue without installation of the plugin.

Next it will ask to create Admin User. Create an admin user and continue through further screens. Then click on Start using Jenkins to open the Jenkins user interface.

Jenkin installation step 5

GitHub Setup

GitHub supports either Username + Password or SSH. If Username + Password is supported, we can skip the SSH Steps below.

For letting Jenkins access repository and checkout the code to build, we need to create Public/Private Key Pair. We will use PuttyGen to generate the keys. Open PuttyGen and Click on Generate Key. Move your mouse randomly in the rectangular area and it will generate a Public/Private key pair. Save the Public/Private keys for further reference.

Putty installation

Once the Key is generated and saved, we need to add it in GitHub.

  • Go to GitHub Settings
  • Click on SSH and GPG Keys in the Menu
  • Click on New SSH Key button
  • Give Title of the Key as Jenkins
  • Copy and Paste the key generated by PuttyGen in the Key Text area and save the key

Add Credentials in Jenkins

  • Login to Jenkins
  • Click on Credentials in Left Menu
  • Click on Jenkins Store
  • Click on Global credentials on next screen
Jenkin GitHub
Jenkin GitHub 1

Setup GitHub credentials as below. Name it as GitHub, description as GitHub Key, enter your GitHub username and Copy and paste your Private Key.

Setup GitHub and MSBuild Paths

Download Git for Windows and install it. Now we will setup both Git and MSBuild paths in Jenkins.

  • Log into Jenkins
  • Click on Manage Jenkins from Left Menu
  • Click on Global Tool Configuration
Jenkin setup

4. Configure Git and MSBuild Paths. Path may vary on system based on the Installation location.

MSBuild – C:\Program Files (x86)\Microsoft Visual Studio\2019\MyProject\Current\Bin

Git – C:\Program Files (x86)\Git\bin\git.exe

By now we have configured everything we need to build our .NET Projects in Jenkins. Let’s start configuring our first Build Project.Project

Create Project

Click on New Item from the Jenkins Menu to create a new Project. Enter project name and select Freestyle project from the list and continue.

On next screen and add description of the project.

Check the checkbox “This project is parameterized”. Click on Add parameter and select Git Parameter

In source control management select Git. Enter your GitHub SSH Url. Select credentials from dropdown which were configured while setting up Jenkins.

We will accept branch to build as a parameter. We have already defined the parameter so let’s specify branch to build as ${branch}

Jenkin installation

Click on Add build step and select “Execute Windows Batch command” from the list. We will execute nuget.exe in shell to restore Nuget Packages for the solution. WORKSPACE variable holds the path of the Workspace folder where your code will be checked out from Git. In my case, my solution file is in SingleSolution directory on the root. So I will pass that as a parameter to nuget.

Click on Add build step and select “Build a Visual Studio project or solution using MSBuild” and execute MSBuild command line to build solution. In this step I am building Configuration “Base” for x64 Platform. We can also accept these values as a Parameter instead of hardcoding the values here.

/p:Configuration=Base /p:WarningLevel=0 /p:Platform=x64 /p:DeployOnBuild=true

As we are passing DeployOnBuild=true parameter to MSBuild, MSBuild will create a Deployment Package for each project at location “${WORKSPACE}\PROJECT_RELATIVE_PATH\obj\x64\Base\Package\PROJECT_NAME.zip”. Our next step will use this package and deploy it to an IIS Instance. In this case we will use local IIS Instance running on the same machine where Jenkins is running but we can also set it up to deploy on remote IIS Server.

Jenkin setup 3

Again click Add Build step and select “Execute Windows batch command”. Point MSDeploy to the Zip File path as follows:

"C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:sync -source:package="%WORKSPACE%\PROJECT_RELATIVE_PATH\obj\x64\Base\Package\PROJECT_NAME.zip" -dest:auto,computerName=localhost -allowUntrusted=true

If your Publish Profile is not configured properly, dest:auto will fail to resolve the correct IIS Application. In that case we will use following command by passing-dest:contentPath="yoursite.com"

"C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:sync -source:package="%WORKSPACE%\PROJECT_RELATIVE_PATH\obj\x64\Base\Package\PROJECT_NAME.zip" -dest:contentPath="yoursite.com",computerName=localhost -allowUntrusted=true

Alternatively, we can also pass the Sitename in -dest computerName argument as follows. If IIS Server requires authentication, Username and password can also be passed as parameters.

  "C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:sync -source:package="%WORKSPACE%\PROJECT_RELATIVE_PATH\obj\x64\Base\Package\PROJECT_NAME.zip" -allowUntrusted=true -dest:auto,computerName="https://localhost:8172/MSDeploy.axd?site=yoursite.com", 
username="MACHINENAME\username", 
password="Pa$$w0rd", 
authtype="Basic",
includeAcls="False"

OR

"C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:sync -source:package="%WORKSPACE%\PROJECT_RELATIVE_PATH\obj\x64\Base\Package\PROJECT_NAME.zip" -dest:auto,computerName=localhost -allowUntrusted=true -setParam:name="IIS Web Application Name",value="yoursite.com"

If want to deploy Windows Services or other projects. While deploying we need to stop the service first, so we can write simple batch commands to stop the service, copy the output to the location and start the service.

net stop YOUR_SERVICE_NAME
rmdir "YOUR_SERVICE_PATH" /q /s
mkdir "YOUR_SERVICE_PATH" 

xcopy "%WORKSPACE%\PROJECT_RELATIVE_PATH\obj\x64\Base\Package\PackageTmp\bin" "YOUR_SERVICE_PATH" /s/h/e/k/f/c

net start YOUR_SERVICE_NAME

Jenkins allows customize this Continuous Deployment setup by adding variables and controlling the flow.

Congrats we have created Continues Deployment for projects.

Leave a Reply

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