Implementing CI/CD with Jenkins Declarative Pipeline for .NET Core Applications
This article demonstrates how to set up a Jenkins Declarative Pipeline to automate the CI/CD workflow for a .NET Core project, covering stages such as checkout, restore, clean, build, test, publish, and email notifications.
Jenkins offers several ways to achieve CI/CD, including Blue Ocean, Freestyle projects, and Declarative Pipelines. This guide focuses on automating integration and deployment for a .NET Core application using a Declarative Pipeline.
In the pipeline configuration, select Pipeline script and define environment variables and triggers as shown below:
pipeline{
agent any
environment {
dotnet ='C:\Program Files (x86)\dotnet\'
}
triggers {
pollSCM 'H * * * *'
}
}The first CI step checks out the source code from Git:
stages{ 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' } }Next, restore the project's NuGet packages:
stage('Restore packages'){
steps{
bat "dotnet restore YourProjectPath\Your_Project.csproj"
}
}Clean the build output:
stage('Clean'){
steps{
bat "dotnet clean YourProjectPath\Your_Project.csproj"
}
}Build the project, producing DLLs in bin\Debug\netcoreapp2.x :
stage('Build'){
steps{
bat "dotnet build YourProjectPath\Your_Project.csproj --configuration Release"
}
}Optionally integrate SonarQube for code quality analysis, then add automated testing stages:
stage('Test: Unit Test'){
steps {
bat "dotnet test YourProjectPath\UnitTest_Project.csproj"
}
}
stage('Test: Integration Test'){
steps {
bat "dotnet test ProjectPath\IntegrateTest_Project.csproj"
}
}Finally, publish the compiled artifacts and send a notification email:
stage('Publish'){
steps{ bat "dotnet publish YourProjectPath\Your_Project.csproj "
}} post{
always{
emailext body: "${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}",
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}"
}
}The complete pipeline flow is: Checkout → Restore → Clean → Build → Test → Publish.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.