Azure Documentation

CI/CD for Containers with Azure DevOps

This tutorial guides you through setting up a robust Continuous Integration and Continuous Delivery (CI/CD) pipeline for containerized applications using Azure DevOps and Azure Container Registry.

Prerequisites

Step 1: Set up Azure Container Registry (ACR)

Azure Container Registry is a managed, private Docker registry service that stores and manages private Docker container images. It's essential for securely storing your container images.

  1. Go to the Azure portal and create a new "Container Registry".
  2. Configure the registry name, resource group, and SKU (Basic, Standard, Premium).
  3. Once created, navigate to your ACR instance and copy the "Login server" name. You'll need this later.

Step 2: Create an Azure DevOps Project and Pipeline

We'll use Azure Repos for source control and Azure Pipelines for the CI/CD automation.

  1. In your Azure DevOps organization, create a new project.
  2. If your code is not already in Azure Repos, you can import it or push it from a local Git repository.
  3. Navigate to "Pipelines" and click "New pipeline".
  4. Select your repository location (e.g., Azure Repos Git).
  5. Choose "Starter pipeline". Azure DevOps will try to detect your application type.

Step 3: Configure the CI Pipeline (Build)

This pipeline will build your Docker image, tag it, and push it to Azure Container Registry.

azure-pipelines.yml


trigger:
- main

variables:
  # Replace with your ACR name
  azureContainerRegistry: 'youracrname.azurecr.io'
  dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
  tag: '$(Build.BuildId)'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- stage: Build
  displayName: Build and push stage
  jobs:
  - job: Build
    displayName: Build and push job
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(azureContainerRegistry)
        tags: |
          $(tag)
          latest
    - task: AzureContainerRegistry@0
      inputs:
        action: 'Login'
        azureSubscriptionEndpoint: 'YourAzureServiceConnection' # Replace with your Service Connection name
        #acrName: '$(azureContainerRegistry)' # Optional if using Service Connection
            

Explanation:

Step 4: Configure the CD Pipeline (Release)

This pipeline will deploy your container image to an Azure service, such as Azure Kubernetes Service (AKS) or Azure App Service.

For this example, we'll outline deployment to Azure App Service (Web App for Containers).

  1. In Azure DevOps, navigate to "Pipelines" -> "Releases".
  2. Click "New pipeline".
  3. Select the "Azure App Service deployment" template.
  4. Artifacts: Add an artifact. Select "Build" as the source type, choose your CI pipeline, and select the "latest" build.
  5. Stage: Configure the stage to deploy to your Azure App Service.
    • Select "Azure App Service".
    • Choose your Azure subscription and App Service name (you'll need to create this in Azure first).
    • Under "Package or folder", select "Docker Container Registry".
    • Specify the Image name (e.g., youracrname.azurecr.io/your-image-name:latest).
    • Deployment Method: "Web App on Linux" or "Web App on Windows" depending on your App Service configuration.

Your release pipeline will automatically trigger after a successful CI build, pulling the latest container image and deploying it to your Azure App Service.

Note: Ensure your Azure Service Connection in Azure DevOps has the necessary permissions to push to Azure Container Registry and deploy to your Azure App Service. For AKS deployments, you would typically use a Kubernetes manifest or Helm chart and a Kubernetes deployment task.

Best Practices

By following these steps, you can establish an automated CI/CD workflow that builds, tests, and deploys your containerized applications to Azure efficiently and reliably.