Continuous Integration/Continuous Deployment (CI/CD) with Jenkins in .NET Microservices: Product and Order Services

DotNet Full Stack Dev
3 min readAug 6, 2024

--

Continuous Integration and Continuous Deployment (CI/CD) are critical practices in modern software development, especially in microservices architectures. They ensure that code changes are continuously tested, integrated, and deployed, providing faster feedback and reducing the time to market. This blog will guide you through setting up a CI/CD pipeline using Jenkins for two .NET microservices: Product and Order services.

Embark on a journey of continuous learning and exploration with DotNet-FullStack-Dev. Uncover more by visiting our https://dotnet-fullstack-dev.blogspot.com reach out for further information.

Introduction to CI/CD and Jenkins

Continuous Integration (CI): A practice where developers frequently integrate their code into a shared repository. Each integration is automatically verified by running tests, allowing teams to detect issues early.

Continuous Deployment (CD): An extension of CI that automates the deployment of applications to production environments, ensuring that every change passes through the CI pipeline and is deployed without manual intervention.

Jenkins: An open-source automation server that facilitates CI/CD by automating the building, testing, and deployment processes.

Setting Up Jenkins

1. Install Jenkins:

  • Download and install Jenkins from the official website.
  • Run Jenkins and complete the initial setup.

2. Configure Jenkins for .NET:

  • Install necessary plugins: Git, Pipeline, Docker, and MSBuild.
  • Configure Jenkins to use .NET Core SDK. Go to Manage Jenkins > Global Tool Configuration and add .NET Core.

Creating Jenkins Pipeline for Product and Order Services

1. Jenkins Pipeline Script (Jenkinsfile):

A Jenkins pipeline script, called a Jenkinsfile, defines the CI/CD steps. Here's a sample Jenkinsfile for building, testing, and deploying the Product and Order services:

pipeline {
agent any
environment {
DOCKER_CREDENTIAL_ID = 'dockerhub-credentials'
DOCKER_IMAGE = 'your-dockerhub-username/microservices'
}
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-repo/microservices.git'
}
}
stage('Build') {
steps {
script {
def productDir = 'ProductService'
def orderDir = 'OrderService'
dir(productDir) {
sh 'dotnet restore'
sh 'dotnet build --configuration Release'
}
dir(orderDir) {
sh 'dotnet restore'
sh 'dotnet build --configuration Release'
}
}
}
}
stage('Test') {
steps {
script {
def productDir = 'ProductService'
def orderDir = 'OrderService'
dir(productDir) {
sh 'dotnet test --configuration Release'
}
dir(orderDir) {
sh 'dotnet test --configuration Release'
}
}
}
}
stage('Docker Build & Push') {
steps {
script {
docker.withRegistry('', DOCKER_CREDENTIAL_ID) {
def productImage = docker.build("${DOCKER_IMAGE}:product-${env.BUILD_NUMBER}", './ProductService')
def orderImage = docker.build("${DOCKER_IMAGE}:order-${env.BUILD_NUMBER}", './OrderService')
productImage.push()
orderImage.push()
}
}
}
}
stage('Deploy') {
steps {
script {
// Deploy to Kubernetes or other orchestration services
}
}
}
}
post {
always {
cleanWs()
}
}
}g

Detailed Explanation

Stage 1: Checkout

  • Retrieves the latest code from the Git repository.

Stage 2: Build

  • Restores dependencies and builds the projects for both Product and Order services.

Stage 3: Test

  • Runs unit tests for both services to ensure that the code changes don’t break existing functionality.

Stage 4: Docker Build & Push

  • Builds Docker images for both services and pushes them to a Docker registry (e.g., Docker Hub).

Stage 5: Deploy

  • Deploys the services to a production environment. In a real-world scenario, this might involve pushing the Docker images to a Kubernetes cluster or another container orchestration platform.

Setting Up Jenkins Job

1. Create a New Job:

  • In Jenkins, create a new pipeline job.
  • Under the “Pipeline” section, select “Pipeline script from SCM” and specify the repository and branch containing your Jenkinsfile.

2. Configure Webhooks:

  • Set up webhooks in your Git repository to trigger Jenkins builds automatically on code changes.

Deployment Strategies

1. Blue-Green Deployment:

  • Keep two production environments (blue and green). Deploy new versions to the green environment while keeping the blue environment live. Switch traffic to green after successful deployment.

2. Canary Releases:

  • Gradually roll out new versions to a small subset of users before full deployment.

Conclusion

Implementing CI/CD with Jenkins in .NET microservices streamlines the development and deployment processes, reducing manual efforts and increasing reliability. By automating builds, tests, and deployments, teams can focus on delivering features faster and with greater confidence. The Product and Order services example demonstrates how to set up a basic CI/CD pipeline, but the same principles can be applied to larger, more complex microservices architectures.

Jenkins, combined with Docker, provides a powerful setup for building and deploying microservices. The use of Docker images ensures that applications run consistently across different environments, further enhancing the robustness of the deployment process. As your CI/CD pipeline evolves, you can incorporate more advanced techniques such as automated rollbacks, monitoring, and scaling strategies to handle real-world production challenges.

You may also like : https://medium.com/@siva.veeravarapu/ci-cd-pipelines-in-devops-a-comprehensive-guide-5a57d94be276

--

--

DotNet Full Stack Dev
DotNet Full Stack Dev

Written by DotNet Full Stack Dev

Join me to master .NET Full Stack Development & boost your skills by 1% daily with insights, examples, and techniques! https://dotnet-fullstack-dev.blogspot.com

Responses (1)