CI/CD in Azure DevOps for .NET Microservices Product Order Services

In a microservices architecture, automating the deployment process ensures that changes can be delivered quickly and consistently. Azure DevOps provides a comprehensive set of tools to manage the lifecycle of software development, from code repositorie…


This content originally appeared on DEV Community and was authored by DotNet Full Stack Dev

In a microservices architecture, automating the deployment process ensures that changes can be delivered quickly and consistently. Azure DevOps provides a comprehensive set of tools to manage the lifecycle of software development, from code repositories to automated pipelines. This guide will walk through setting up a CI/CD pipeline for two .NET microservices, Product and Order services, demonstrating how to build, test, and deploy them using Azure DevOps.

Setting Up Azure DevOps

  1. Create an Azure DevOps Organization: Visit Azure DevOps, sign in, and create an organization.
  2. Create a Project: Within your organization, create a new project to manage your microservices' repositories and pipelines.

Configuring Azure Repos

  1. Import Repositories: Import your existing Git repositories for the Product and Order services into Azure Repos.

Creating the CI/CD Pipeline

Pipeline Overview

The pipeline consists of several stages, including building, testing, and deploying both microservices. We'll define the pipeline using YAML.

Pipeline Configuration (azure-pipelines.yml)

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'
  dockerRegistryServiceConnection: 'dockerhub-connection'
  dockerRegistry: 'your-dockerhub-username'
  productServiceImageName: 'product-service'
  orderServiceImageName: 'order-service'

stages:
- stage: Build
  jobs:
  - job: BuildProductService
    displayName: 'Build Product Service'
    steps:
    - task: UseDotNet@2
      inputs:
        packageType: 'sdk'
        version: '6.x'
    - script: dotnet build --configuration $(buildConfiguration) src/ProductService/ProductService.csproj
      displayName: 'Build Product Service'

  - job: BuildOrderService
    displayName: 'Build Order Service'
    steps:
    - task: UseDotNet@2
      inputs:
        packageType: 'sdk'
        version: '6.x'
    - script: dotnet build --configuration $(buildConfiguration) src/OrderService/OrderService.csproj
      displayName: 'Build Order Service'

- stage: Test
  dependsOn: Build
  jobs:
  - job: TestProductService
    displayName: 'Test Product Service'
    steps:
    - script: dotnet test --configuration $(buildConfiguration) src/ProductService/ProductService.Tests/ProductService.Tests.csproj
      displayName: 'Test Product Service'

  - job: TestOrderService
    displayName: 'Test Order Service'
    steps:
    - script: dotnet test --configuration $(buildConfiguration) src/OrderService/OrderService.Tests/OrderService.Tests.csproj
      displayName: 'Test Order Service'

- stage: DockerBuildAndPush
  dependsOn: Test
  jobs:
  - job: BuildAndPushProductService
    displayName: 'Docker Build and Push Product Service'
    steps:
    - task: Docker@2
      inputs:
        containerRegistry: $(dockerRegistryServiceConnection)
        repository: $(dockerRegistry)/$(productServiceImageName)
        command: 'buildAndPush'
        Dockerfile: '**/ProductService/Dockerfile'
        tags: |
          $(Build.BuildId)
          latest

  - job: BuildAndPushOrderService
    displayName: 'Docker Build and Push Order Service'
    steps:
    - task: Docker@2
      inputs:
        containerRegistry: $(dockerRegistryServiceConnection)
        repository: $(dockerRegistry)/$(orderServiceImageName)
        command: 'buildAndPush'
        Dockerfile: '**/OrderService/Dockerfile'
        tags: |
          $(Build.BuildId)
          latest

- stage: Deploy
  dependsOn: DockerBuildAndPush
  jobs:
  - deployment: DeployProductService
    displayName: 'Deploy Product Service'
    environment: 'product-service-production'
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo 'Deploying Product Service to production...'

  - deployment: DeployOrderService
    displayName: 'Deploy Order Service'
    environment: 'order-service-production'
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo 'Deploying Order Service to production...'

Explanation

  1. Trigger: The pipeline triggers commits to the main branch.
  2. Variables
    • buildConfiguration: Specifies the build configuration (Release).
    • dockerRegistryServiceConnection: Azure DevOps service connection for Docker registry.
    • dockerRegistry: Docker registry username.
    • productServiceImageName and orderServiceImageName: Docker image names for services.
  3. Stages
    • Build: Compiles the Product and Order services.
    • Test: Run unit tests for both services.
    • DockerBuildAndPush: Builds Docker images and pushes them to a registry.
    • Deploy: Deploys the services to a production environment.

Setting Up Azure DevOps Pipeline

  1. Create a Pipeline: Navigate to Pipelines in Azure DevOps, create a new pipeline, and select the repository.
  2. YAML Configuration: Choose YAML and use the provided azure-pipelines.yml file.
  3. Service Connections: Set up a Docker registry service connection and store credentials securely.

Deployment Strategies

  1. Blue-Green Deployment: Deploy updates to a staging environment before switching traffic.
  2. Canary Releases: Gradually roll out changes to a subset of users.

Conclusion

In this guide, we demonstrated setting up a CI/CD pipeline for Product and Order microservices using Azure DevOps. By leveraging Azure Pipelines, Docker, and strategic deployment methods, we can automate the build, test, and deploy processes, ensuring consistent and reliable software delivery. This setup not only improves efficiency but also reduces the risk of errors in production. As organizations embrace microservices, adopting robust CI/CD practices becomes crucial for maintaining high-quality and scalable systems.


This content originally appeared on DEV Community and was authored by DotNet Full Stack Dev


Print Share Comment Cite Upload Translate Updates
APA

DotNet Full Stack Dev | Sciencx (2024-10-12T00:04:45+00:00) CI/CD in Azure DevOps for .NET Microservices Product Order Services. Retrieved from https://www.scien.cx/2024/10/12/ci-cd-in-azure-devops-for-net-microservices-product-order-services/

MLA
" » CI/CD in Azure DevOps for .NET Microservices Product Order Services." DotNet Full Stack Dev | Sciencx - Saturday October 12, 2024, https://www.scien.cx/2024/10/12/ci-cd-in-azure-devops-for-net-microservices-product-order-services/
HARVARD
DotNet Full Stack Dev | Sciencx Saturday October 12, 2024 » CI/CD in Azure DevOps for .NET Microservices Product Order Services., viewed ,<https://www.scien.cx/2024/10/12/ci-cd-in-azure-devops-for-net-microservices-product-order-services/>
VANCOUVER
DotNet Full Stack Dev | Sciencx - » CI/CD in Azure DevOps for .NET Microservices Product Order Services. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/12/ci-cd-in-azure-devops-for-net-microservices-product-order-services/
CHICAGO
" » CI/CD in Azure DevOps for .NET Microservices Product Order Services." DotNet Full Stack Dev | Sciencx - Accessed . https://www.scien.cx/2024/10/12/ci-cd-in-azure-devops-for-net-microservices-product-order-services/
IEEE
" » CI/CD in Azure DevOps for .NET Microservices Product Order Services." DotNet Full Stack Dev | Sciencx [Online]. Available: https://www.scien.cx/2024/10/12/ci-cd-in-azure-devops-for-net-microservices-product-order-services/. [Accessed: ]
rf:citation
» CI/CD in Azure DevOps for .NET Microservices Product Order Services | DotNet Full Stack Dev | Sciencx | https://www.scien.cx/2024/10/12/ci-cd-in-azure-devops-for-net-microservices-product-order-services/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.