Continuous Integration and Deployment Tools for .Net Application Using Jenkins File
This post describes how to dockerise the dotnet application using continuous integration and continuous deployment tool Jenkins. However, before trying this we need to set up Jenkins pipeline. Through that, we can deploy the .net application automatically. Depending on this, we need some docker images for building the dotnet project.
To get the docker images we will be using docker registry with just a few configurations.
Basic Configuration to Auto-Deploy .Net application
- We need to install Docker container with docker registry.
- We need a ‘msbuild’ folder. As per the project requirement, we must have msbuild.exe file existing on the server.
Pre-requisites:
1. Jenkinsfile in the source code.
2. Dockerfile in the source code.
3. We need ‘msbuild’ folder to build the dotnet application.
Steps to auto-deploy dot net application using Jenkins server –
- Setup Jenkins pipeline settings.
- Commit the code on the Git/Bitbucket server.
- Code must include DockerFile and Jenkinsfile.
Explanation of Jenkinsfile and Dockerfile commands:
1.Dockerfile explanation with commands-
Step1: To build the dotnet application we need docker image of aspnet. We have to pull the image of aspnet from docker registry.
command: FROM microsoft/aspnet
Explanation: # The `FROM` instruction specifies the base image. You can actually do this by just extending the `microsoft/aspnet` image.
Step2: We have to write the code to copy the site you published earlier into the container.
Here “yourprojectpublish” is the folder name to be copied in inetpub directory
command : COPY ./bin/publish/_PublishedWebsites/yourprojectpublish /inetpub/wwwroot
2.Jenkinsfile explanation with commands-
step1: First we have to build the project using MSBuild. To build the project we need .sln file. Here yourproject.sln is the name of the solution file. We have to give configuration as debug/release mode. We have to assign the output directory path.
Syntax:
stage (‘Build’)
{
steps {
bat “msbuild.exe yourproject.sln /p:Configuration=Release /property:OutDir=bin/publish /p:Platform=\”Any CPU\” /p:ProductVersion=1.0.0.${env.BUILD_NUMBER}”
}
}
step2: Once we build the project using solution file, we have to build docker image for publishing the project. Here, “yourproject” is the name of folder where we have to publish and “nxglabs” is the docker registery username.
Syntax:
stage (‘BuildDockerImage’)
{
steps {
powershell ‘docker build -t nxglabs/yourproject .’
}
}
step3: Here we will push the publish folder on docker registry by providing credentials of docker registry id. Credentials can be created form the “manage credentials” section of Jenkins.
Syntax:
stage(‘PushDockerImage’)
{
steps {
withDockerRegistry([ credentialsId: “b1fee946-b93a-46e0-ac78-abb78aea7cc2”, url: “” ]) {
powershell ‘docker push nxglabs/yourproject’
}
}
}
steps4:We will publish the dockerimage by giving the port number. Here yourprojectcontainer is the the container used which has to be published automatically.
Syntax:
stage(‘RunContainer’)
{
steps {
script{
try{
powershell ‘docker rm -f yourprojectcontainer’
}
catch (err){
echo “system-check-flow failed”
}
}
withDockerRegistry([ credentialsId: “b1fee946-b93a-46e0-ac78-abb78aea7cc2”, url: “” ]) {
powershell ‘docker run –name yourprojectcontainer -d -p 1001:80 nxglabs/yourproject’
}
}
}
step5: Lastly we will archive the folder by giving the path of release folder.
Syntax:
stage (‘Archive’)
{
steps {
archive ‘bin/Release/**’
}
}
Output: We can get the output as below.
This article barely touches the surface of continuous integration and deployment tools for .Net application using Dockerfile and Jenkinsfile. You can check out other cases for “NXG Labs” in this guide.
Thanks for reading!!!