Jenkins CI/CD to deploy an ASP.NET Core application
By: Hansika Wanniarachchi
Tags: Net Core, Cicd, Pipeline, Jenkins

M odern software moves fast and demands more from developers than ever. Tools and concepts around CICD help developers deliver value faster and more transparently.

First of all let me introduce what is CI/CD is in brief, Are you committing all your application code in a single repository? 🤔 Once you have all your code and changes going to the same place, you can run some processes on that repository every time something changes. That process could be included, Run automatic code quality scans like SonarQube on it and generate a report of how well your latest changes adhere to good coding practices, Build the code and run any automated tests that you might have written to make sure your changes didn’t break any functionality. Simply that process is known as Continuous Integration on the other hand Continuous deployment is much like other things in software, is to automate the deployment process, Make it so that your code gets automatically deployed to wherever you or your users can get to it.

What is Jenkins Pipeline?

Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins.

There are so many ways to implement CI/CD in Jenkins such as Blue Ocean, FreeStyle project, and Declarative Pipeline. In this article, I will explain how to automate the integration and deployment process using Jenkins with Declarative Pipeline. I have tried to explain all steps explicitly with .net core application.

Jenkins Pipeline Workflow

We will not go deep into Jenkins Pipeline as it is out of the scope of the current post, but you can find all the details for the Pipeline here.

I hope you are a little bit aware of Jenkins Pipeline now and I hope you have your basic setup done for Jenkins. You can download Jenkins here.

Create a Jenkins Pipeline

Open Jenkins and click on New Item:

In the next window, select the pipeline option and give your pipeline a name:

Our pipeline is ready, now we should complete the pipeline configuration. Let us add different stages that will be required to run our CI and then will come back to the pipeline once we will finalize our Pipeline code.

In Build Trigger stage, tick on build periodically and state a time period that you would like to rebuild the pipeline. For example, If you schedule the period as H**** then your pipeline will run in every one hour time.

Now move on to the configuration pipeline stage and please make sure you have added the GitHub plugin and MSBuild plugin. After setting all the plugins set the path to MSBuild as globe tool configuration because its essential to run .net core application.

Pipeline script

In the configuration pipeline stage select pipeline definition as Pipeline script. Under the pipeline script set the environment and triggers as a script.

pipeline{

agent any



environment {

dotnet ='C:\\Program Files (x86)\\dotnet\\'

}



triggers {

pollSCM 'H * * * *'

}

}

Checkout

The first step for our CI would be to check out the code from Git. For that, our pipeline code would be as below:

tages{

stage('Checkout') {

steps {

git credentialsId: 'Give Your Credential ID', url: 'https://github.com/YourAcc/YourRepoName.git/', branch: 'Branch on which you want to set the CI'

}

}

Once done, the project would be checked out\cloned on the agent which you have specified

Restore Packages

The next step would be the restoring of the packages of the application. Let us run the restore command, which will restore the packages if they are not available on your Windows agent:

stage('Restore packages'){

steps{

bat "dotnet restore YourProjectPath\\Your_Project.csproj"

}

}

Clean

The next step is to clean the solution.

stage('Clean'){

steps{

bat "dotnet clean YourProjectPath\\Your_Project.csproj"

}

}

Build

The next step is building the solution. We will build our solution on the agent using dotnet CLI commands. That will build the solution. It will put dlls and other built files under bin\Debug

etcoreapp2.x

stage('Build'){

steps{

bat "dotnet build YourProjectPath\\Your_Project.csproj --configuration Release"

}

}

In this stage, we can add SonarQube to automate the code quality performance testing.

Automate Testing

The next step will be building the unit test and integration testing on your project. That will build test cases of solution.

stage('Test: Unit Test'){

steps {

bat "dotnet test YourProjectPath\\UnitTest_Project.csproj"

}

}



stage('Test: Integration Test'){

steps {

bat "dotnet test ProjectPath\\IntegrateTest_Project.csproj"

}

}

Publish

Let us add the publish command to publish our packages from Jenkins.

stage('Publish'){

steps{

bat "dotnet publish YourProjectPath\\Your_Project.csproj "

}

}

We can send publish notes to the relevant email addresses as the most effective way to publish the project. After every post, you will receive an email regarding your deployment.

post{

always{

emailext body: "${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}

More info at: ${env.BUILD_URL}", recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}"

}

}

Once the code is added, go back to the Jenkins UI and click on Build Now.

This will run the Jenkins job which will do all the things required for CI:

Checkout -> Restore -> Clean -> Build -> Test-> Publish

Full Stage view of Jenkins Pipeline

Status of the last build will shows in the dashboard if your deployment fails more than 3 times it indicate in red color and for successful building it indicates in Blue. In Jenkins Dashboard it shows weather conditions it defined the number of failures in your project building.

I hope this helps you. 🤗

Subscribe to FAUN topics and get your weekly curated email of the must-read tech stories, news, and tutorials 🗞️

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬