Jenkins with .NET application
In this post we are going to explore Creating Pipeline with .NET application in Jenkins, in Windows.
Start Jenkins
- Start command prompt and cd to the directory where the jenkins.war file is there (in my case C:\Jenkins) and run the command:
> java -jar jenkins.war
- Start your browser with Jenkins url (http://localhost:8080) then the following Jenkins UI is displayed:
Create Pipeline for .NET
- Install .NET SDK and all .NET development Build Tools etc. related from Jenkins Dashboard: Manage Jenkins: Plugins and the select Available Plugins. By selecting the checkboxes and pressing to Install button in the upper right.
2. Create a new Item from the Jenkins Dashboard by pressing New Item, give a name and select Pipeline and then press to OK button as following:
3. Configure this pipeline for this new item, by adding description, select Github project and give project url : https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master. Under Pipeline: Definition : Pipeline script from SCM. SCM: choose Git and for Repository url: https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master.git for Credentials : Credentials Github repo for Branch: */Master for Script path : jenkinsfile at least press to Save button, then Item (job ) is created with name productmicroservice .
Create Jenkinsfile in the Git Repository
Create Jenkins file in the root of repository ( for me: https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master) and the Jenkinsfile is as follow:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
echo 'Checking out the code'
checkout scm
}
}
stage('Build and Test') {
steps {
echo 'Starting Build and Test'
script {
def targetDirectory = 'C:\\Temp\\Deployment\\ProductMicroservice'
bat "dotnet restore ProductMicroservice/ProductMicroservice/ProductMicroservice.csproj"
bat "dotnet build ProductMicroservice/ProductMicroservice/ProductMicroservice.csproj"
bat "dotnet test ProductMicroservicesTest/ProductMicroservicesTest.csproj"
bat "dotnet publish ProductMicroservice/ProductMicroservice/ProductMicroservice.csproj -c Release -o ${targetDirectory}"
}
}
}
stage('Deploy') {
steps {
echo 'Starting Deploy'
script {
def targetDirectory = 'C:\\Temp\\Deployment\\ProductMicroservice'
// bat "mkdir ${targetDirectory}"
bat "xcopy /s /y .\\ProductMicroservice\\ProductMicroservice\\bin\\Release\\net8.0\\net8.0\\* ${targetDirectory}"
// bat "cd ${targetDirectory}"
// bat "dotnet ProductMicroservice.dll" // Assuming this is the command to start your application
}
}
}
}
post {
success {
echo 'Build successful - Add any additional success steps here'
}
failure {
echo 'Build failed - Add any additional failure steps here'
}
}
}
Description of Jenkins file:
Begins with Pipeline {, and ends in the end of file ,}
In the line 4. Stages { ends in the line 38 ,}.
In the Deploy stage line 29 the argetDirectory is defined as follow
targetDirectory = 'C<span class="token punctuation">:</span>\\Temp\\Deployment\\ProductMicroservice'
This means that the Artefact shall be stored in the this directory.
Find the Jenkins file in My Github
Run the Pipeline
In the Jenkins Dashboard press to the Build Now then begins to run the Pipeline for productmicroservice as shown in the following image:
As shown in the figure above the stage view shows for the stages which are defined in the Jenkinsfile. And as we see Build and test , Deploy are succeed.
Show the log (Console Output)
Back to the Dashboard and press to the Build History as shown in the following figure:
Under the dropdown of productmicroservice press to the Console Output as shown as following:
Then shows the Console Output with green OK and the log is as follow:
Started by user Mehrdad Zandi
Obtained Jenkinsfile from git https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master.git
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\Users\default1\.jenkins\workspace\productmicroservice
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
using credential c736be13-f2fb-4bd7-9159-37f10a3c7a37
Cloning the remote Git repository
Cloning repository https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master.git
> C:\Program Files\Git\bin\git.exe init C:\Users\default1\.jenkins\workspace\productmicroservice # timeout=10
Fetching upstream changes from https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master.git
> C:\Program Files\Git\bin\git.exe --version # timeout=10
> git --version # 'git version 2.30.0.windows.1'
using GIT_ASKPASS to set credentials Credentials for Github repo
> C:\Program Files\Git\bin\git.exe fetch --tags --force --progress -- https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> C:\Program Files\Git\bin\git.exe config remote.origin.url https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master.git # timeout=10
> C:\Program Files\Git\bin\git.exe config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
> C:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
Checking out Revision 3d77b0caaa3bd07a31eacc9db945515aee89cd08 (refs/remotes/origin/master)
> C:\Program Files\Git\bin\git.exe config core.sparsecheckout # timeout=10
> C:\Program Files\Git\bin\git.exe checkout -f 3d77b0caaa3bd07a31eacc9db945515aee89cd08 # timeout=10
Commit message: "Update Jenkinsfile"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] echo
Checking out the code
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
using credential c736be13-f2fb-4bd7-9159-37f10a3c7a37
> C:\Program Files\Git\bin\git.exe rev-parse --resolve-git-dir C:\Users\default1\.jenkins\workspace\productmicroservice\.git # timeout=10
Fetching changes from the remote Git repository
> C:\Program Files\Git\bin\git.exe config remote.origin.url https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master.git # timeout=10
Fetching upstream changes from https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master.git
> C:\Program Files\Git\bin\git.exe --version # timeout=10
> git --version # 'git version 2.30.0.windows.1'
using GIT_ASKPASS to set credentials Credentials for Github repo
> C:\Program Files\Git\bin\git.exe fetch --tags --force --progress -- https://github.com/mehzan07/ProductMicroservice-DotNET-Core-Master.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> C:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
Checking out Revision 3d77b0caaa3bd07a31eacc9db945515aee89cd08 (refs/remotes/origin/master)
> C:\Program Files\Git\bin\git.exe config core.sparsecheckout # timeout=10
> C:\Program Files\Git\bin\git.exe checkout -f 3d77b0caaa3bd07a31eacc9db945515aee89cd08 # timeout=10
Commit message: "Update Jenkinsfile"
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build and Test)
[Pipeline] echo
Starting Build and Test
[Pipeline] script
[Pipeline] {
[Pipeline] bat
C:\Users\default1\.jenkins\workspace\productmicroservice>dotnet restore ProductMicroservice/ProductMicroservice/ProductMicroservice.csproj
Determining projects to restore...
Restored C:\Users\default1\.jenkins\workspace\productmicroservice\ProductMicroservice\ProductMicroservice\ProductMicroservice.csproj (in 1.26 sec).
[Pipeline] bat
C:\Users\default1\.jenkins\workspace\productmicroservice>dotnet build ProductMicroservice/ProductMicroservice/ProductMicroservice.csproj
MSBuild version 17.8.3+195e7f5a3 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
ProductMicroservice -> C:\Users\default1\.jenkins\workspace\productmicroservice\ProductMicroservice\ProductMicroservice\bin\Debug\net8.0\net8.0\ProductMicroservice.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:06.38
[Pipeline] bat
C:\Users\default1\.jenkins\workspace\productmicroservice>dotnet test ProductMicroservicesTest/ProductMicroservicesTest.csproj
Determining projects to restore...
Restored C:\Users\default1\.jenkins\workspace\productmicroservice\ProductMicroservicesTest\ProductMicroservicesTest.csproj (in 1.47 sec).
1 of 2 projects are up-to-date for restore.
ProductMicroservice -> C:\Users\default1\.jenkins\workspace\productmicroservice\ProductMicroservice\ProductMicroservice\bin\Debug\net8.0\net8.0\ProductMicroservice.dll
ProductMicroservicesTest -> C:\Users\default1\.jenkins\workspace\productmicroservice\ProductMicroservicesTest\bin\Debug\net8.0\ProductMicroservicesTest.dll
Test run for C:\Users\default1\.jenkins\workspace\productmicroservice\ProductMicroservicesTest\bin\Debug\net8.0\ProductMicroservicesTest.dll (.NETCoreApp,Version=v8.0)
Microsoft (R) Test Execution Command Line Tool Version 17.8.0 (x64)
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Passed! - Failed: 0, Passed: 6, Skipped: 0, Total: 6, Duration: 9 ms - ProductMicroservicesTest.dll (net8.0)
[Pipeline] bat
C:\Users\default1\.jenkins\workspace\productmicroservice>dotnet publish ProductMicroservice/ProductMicroservice/ProductMicroservice.csproj -c Release -o C:\Temp\Deployment\ProductMicroservice
MSBuild version 17.8.3+195e7f5a3 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
ProductMicroservice -> C:\Users\default1\.jenkins\workspace\productmicroservice\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\ProductMicroservice.dll
ProductMicroservice -> C:\Temp\Deployment\ProductMicroservice\
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] echo
Starting Deploy
[Pipeline] script
[Pipeline] {
[Pipeline] bat
C:\Users\default1\.jenkins\workspace\productmicroservice>xcopy /s /y .\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\* C:\Temp\Deployment\ProductMicroservice
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\appsettings.Development.json
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\appsettings.json
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\AutoMapper.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\AutoMapper.Extensions.Microsoft.DependencyInjection.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Humanizer.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\libman.json
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\MediatR.Contracts.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\MediatR.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\MediatR.Extensions.Microsoft.DependencyInjection.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.Data.SqlClient.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.EntityFrameworkCore.Abstractions.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.EntityFrameworkCore.Design.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.EntityFrameworkCore.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.EntityFrameworkCore.Relational.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.EntityFrameworkCore.SqlServer.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.Identity.Client.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.IdentityModel.JsonWebTokens.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.IdentityModel.Logging.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.IdentityModel.Protocols.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.IdentityModel.Protocols.OpenIdConnect.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.IdentityModel.Tokens.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.OpenApi.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Microsoft.Win32.SystemEvents.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Newtonsoft.Json.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\ProductMicroservice.deps.json
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\ProductMicroservice.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\ProductMicroservice.exe
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\ProductMicroservice.pdb
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\ProductMicroservice.runtimeconfig.json
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\ProductMicroservice.xml
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\RabbitMQ.Client.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Swashbuckle.AspNetCore.Swagger.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Swashbuckle.AspNetCore.SwaggerGen.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\Swashbuckle.AspNetCore.SwaggerUI.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\System.Configuration.ConfigurationManager.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\System.Drawing.Common.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\System.IdentityModel.Tokens.Jwt.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\System.Runtime.Caching.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\System.Security.Cryptography.ProtectedData.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\System.Security.Permissions.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\System.Windows.Extensions.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\unix\lib\netcoreapp3.1\Microsoft.Data.SqlClient.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win\lib\net8.0\Microsoft.Win32.SystemEvents.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win\lib\netcoreapp3.0\System.Windows.Extensions.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win\lib\netcoreapp3.1\Microsoft.Data.SqlClient.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win\lib\netstandard2.0\System.Runtime.Caching.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win\lib\netstandard2.0\System.Security.Cryptography.ProtectedData.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win-arm\native\Microsoft.Data.SqlClient.SNI.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win-arm\native\Microsoft.Data.SqlClient.SNI.pdb
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win-arm64\native\Microsoft.Data.SqlClient.SNI.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win-arm64\native\Microsoft.Data.SqlClient.SNI.pdb
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win-x64\native\Microsoft.Data.SqlClient.SNI.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win-x64\native\Microsoft.Data.SqlClient.SNI.pdb
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win-x86\native\Microsoft.Data.SqlClient.SNI.dll
.\ProductMicroservice\ProductMicroservice\bin\Release\net8.0\net8.0\runtimes\win-x86\native\Microsoft.Data.SqlClient.SNI.pdb
55 File(s) copied
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Build successful - Add any additional success steps here
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
C:\Users\default1\.jenkins\workspace\productmicroservice@tmp\jfrog\1\.jfrog deleted
Finished: SUCCESS
from the log you can find the detail of each stage and in the end : Finished: Success. In case any error in the Jeninsfile of wrong configuration you can find what was wrong, you can corret the Jeninskfile and run Pipeline again.
Start of Deploy (artefact)
Go to the your targetDirectroy
targetDirectory = 'C<span class="token punctuation">:</span>\\Temp\\Deployment\\ProductMicroservice'
Start Command prompt and cd to this directory and run the command
dotnet productmicroservice.dll
Start web browser with http://localhost:5000 , then the Swagger UI to the productmicroservice is displayed as follow:
Check the domain IP address (e.g. PC that you have deployed) in the appsettings.json file.
Run the Get or Post here is the result of Get Action:
Conclusion
In this post we have described how to start Jenkins, how to set up Pipeline for a .NET application and how to write the Jenkinsfile, and how to run the Pipeline to build, test and deploy the application, how to run locally the deployment.
This post is part of Jenkins- step by step