DEV Community

Cover image for Blue-Green Deployment with Azure
Mo
Mo

Posted on

Blue-Green Deployment with Azure

Hello, everyone! In this article, we’re diving into Blue-Green Deployment, a brilliant technique for rolling out updates without breaking a sweat—or your app.

Here’s what we’ll cover:

  1. What is Blue-Green Deployment?
  2. The Pros and Cons—because no approach is perfect.
  3. When Should You Use It?
  4. Step-by-Step Demo with Azure—complete with automation tips to make your life easier.

So, grab a cuppa ☕ and let’s get started! 🚀


What is Blue-Green Deployment? 🤔

Imagine you’ve got two versions of your application running—one is "Blue" (your live environment), and the other is "Green" (your shiny new version).

Instead of directly updating the live version and risking downtime, you deploy changes to the Green environment first. Once you’re sure everything’s working perfectly, you simply swap traffic from Blue to Green. Users won’t notice a thing—except the new features! 🎉

Think of it like swapping stage sets in a play between scenes—seamless and smooth. 🎭


The Pros and Cons 🛠️

Pros:

  1. Zero Downtime: No 'under maintenance' messages—just uninterrupted service.
  2. Easy Rollbacks: Found a bug? Flip back to Blue and fix it.
  3. Production-like Testing: Test your new version in a real-world environment.
  4. Better User Experience: Smooth updates = happy users.

Cons:

  1. Higher Costs: You’re running two environments, which can double infrastructure costs.
  2. Complex Setup: Managing two environments isn’t simple—it requires planning and good tools.
  3. Data Synchronisation: Handling databases and real-time data can get tricky.
  4. Testing Overheads: Blue and Green must be identical apart from the new features.

When Should You Use It? 🎯

Blue-Green Deployment shines when downtime isn’t an option—think e-commerce sites, banking systems, or services that need constant availability.

If you’re working on a smaller project or have a tight budget, the added complexity and cost might not be worth it.


Demo with Azure 🖥️

Let’s put theory into practice! I’ll guide you through setting up Blue-Green Deployment using Azure.


Step 1: Create a Web App 🌐

  1. Set up a resource group called BlueGreenResource.
  2. Create a web app named BlueGreenWebApp. Use .NET 8, deploy to UK South, and choose the Standard S1 pricing tier.
  3. Disable App Insights (optional for simple setups).

Step 2: Publish the First Version ✅

  • Use Visual Studio to deploy your app to the live environment.
  • This is version 1, which users can now see. 🎉

Step 3: Add a Staging Slot 🛠️

  • Add a staging slot to your web app. It’ll have its own domain (e.g., staging.bluegreenwebapp.azurewebsites.net).
  • Publish version 2 to this staging slot. Users won’t see this version yet, but it’s ready for testing.

Step 4: Swap the Slots 🔄

  • Once QA approves, use the Swap button in Azure to swap the staging slot with the live slot. Now, users are on version 2! 🎊

Step 5: Clean Up 🧹

  • Delete the staging slot to save costs.

Automate the Process with Azure DevOps 🤖

To avoid manual effort, we’ll set up a CI/CD pipeline in Azure DevOps:

Build Pipeline (YAML File):

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    command: build
    projects: '**/*.csproj'
    arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
  inputs:
    command: publish
    publishWebProjects: True
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
    zipAfterPublish: True

- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Build.ArtifactStagingDirectory)' 
    artifactName: 'deploymentapp-artifact'
Enter fullscreen mode Exit fullscreen mode

This pipeline triggers on changes to the master branch. It builds the app, restores NuGet packages, and creates deployment artifacts.


Release Pipeline:

  1. Create a Staging Slot: Use this script:
   az webapp deployment slot create --name $(webAppName) --resource-group $(resourceGroupName) --slot $(slotName)
Enter fullscreen mode Exit fullscreen mode
  1. Deploy to Staging:

    Set the staging slot as the target in the pipeline.

  2. Swap Slots:

    Use the Azure App Service Manage task to swap environments.

  3. Delete Staging Slot:

    Clean up with this script:

   az webapp deployment slot delete --name $(webAppName) --resource-group $(resourceGroupName) --slot $(slotName)
Enter fullscreen mode Exit fullscreen mode

Testing the Setup 🚀

Push changes to the master branch, and watch the magic happen:

  1. Build triggers automatically.
  2. Deployment to the staging slot occurs.
  3. QA approves the changes.
  4. Slots are swapped, and the staging slot is deleted.

Final Thoughts 💡

Blue-Green Deployment makes updates safer, faster, and smoother, especially for critical systems. While it’s not without challenges, automation tools like Azure DevOps make it easier to manage.

Have you tried Blue-Green Deployment? Share your experiences in the comments! 👇 Don’t forget to like, share, and bookmark this article for future reference. 😊

Top comments (0)