CI/CD for .NET MVC Using Jenkins
CI/CD for .NET MVC Using Jenkins
Check out how Windows Visual Studio and Jenkins come together to create an effective CI/CD process for .NET applications.
Join the DZone community and get the full member experience.Join For Free
Jenkins provides us many ways to set up a CI/CD environment for almost any code language and source code repository. It is an open source automation server which can be used to automate tasks related to building, testing, and delivering or deploying software. It is also an essential tool that aids DevOps teams, which are getting more popular due to the increasing amount of projects we manage at a given period, to ensure their high-quality deliverables. Jenkins has plugins that integrate with third-party source-controllers like Github.
Before we proceed with actual building and deployment, we need to make sure we have build tools installed on the machine.
This can be done through Visual Studio Build Tools, available from the Visual Studio Installer.
Then we need to install build tools to build a MVC application. This can be done by clicking the Modify button for Visual Studio Build Tools and then selecting web development build tools.
If we didn't install these build tools, the
msbuild command would only work in the Developer Command Prompt.
We need to install plugins to use in the Jenkins.
Go to Manage Plugins.
Find and install the Github Plugin.
Find and install the MsBuild Plugin.
I have implemented CI/CD using Jenkins. First of all, you need to download Jenkins and there are a number of ways to use it. I am using it as a windows service on the machine. After starting the Jenkins service, you need to add your application and add the configuration. First, click on "New Item." Then, give the application an Item Name and select the "Freestyle Project" option and then click "OK."
Now for the configuration.
In the General section, you can enter the description of the item and discard the old builds, it means it will automatically discard builds.
In the Source Code section, I used a Git repository. Here we have to provide the URL of the repository and the branch name from which you want to deploy the code.
Build triggers can be added if you want the build to be triggered automatically.
In the build environment, we want to make sure we choose to clean the Jenkins workspace before the build starts.
When we commit the code in the repository we don't commit the packages, so first we have to restore the NuGet packages.
As you can see, the code is committed without NuGet dependencies.
The restore command from the NuGet CLI downloads and installs any packages missing from the packages folder.
Once the command is entered, you can see packages folder created with all NuGet dependencies.
Now we can actually execute MSBuild command.
This command requires that the MSBuild plugin be installed in Jenkins, path to the sln file and optionally we can also provide the
PackageFileName attribute in the command line with path and package name.
/t:clean;build;package /p:PackageFileName="C:\Program Files (x86)\Jenkins\workspace\HelpDesk_CI\HelpdeskMVC.zip"
Msdeploy commad can be used to deploy zip created in previous step to be deployed to IIS.
C:\"Program Files (x86)"\IIS\"Microsoft Web Deploy V3"\msdeploy.exe -verb:sync -source:package="HelpdeskMVC.zip" -dest:auto -setParam:name="IIS Web Application Name",value="Default Web Site/Helpdesk"
In order to deploy zip you need to install "Web Deployment Tool 2.1".
You can install this by right clicking on "Default Web Site" and then going to "Install Application from Gallery".
Then search for "Web Deployment Tool 2.1" in search box. In my case it is already installed.
Post Build/Deploy actions:
You can add various post build actions like sending email notifications, archiveing the deloyale artifact etc
I created a very basic CI/CD for my .Net MVC application. Jenkins gives us flexibility for adding more complex builds.
Opinions expressed by DZone contributors are their own.