Skip to content

Azure DevOps ACR Deployment

Overview

This is a basic Azure DevOps pipeline written in YAML for deploying a generic Sample application using Azure Container Registry (ACR). The pipeline is designed as a starter template that you can customize to fit the specific needs of your project. It follows a Continuous Integration/Continuous Deployment (CI/CD) approach, automating the build and deployment process.

Pipeline Structure

Trigger

The pipeline is triggered to run whenever changes are pushed to the 'main' branch.

trigger:
  - main

Build Agent

The pipeline uses an Ubuntu-based build agent.

pool:
  vmImage: ubuntu-latest

Stages

The pipeline consists of two stages - "Dev" and "HigherEnv" - representing development and higher environments (Higher_Env).

Dev Stage

The Dev stage is configured to use a variable group named "DevEnvironment."

- stage: Dev
  variables:
    - group: DevEnvironment
  displayName: 'Dev'
  jobs:
    - deployment: Dev
      displayName: 'CI/CD Stage for Generic Sample image'
      environment: DevEnvironment
      strategy:
        runOnce:
          deploy:
            steps:
              # Steps for building and pushing the Docker image

HigherEnv Stage

The HigherEnv stage is configured to use a variable group named "HigherEnvVariables."

- stage: HigherEnv
  variables:
    - group: HigherEnvVariables
  displayName: 'Higher_Env'
  jobs:
    - deployment: Higher_Env
      displayName: 'CI/CD Stage for Generic Sample image'
      environment: Higher_Env
      strategy:
        runOnce:
          deploy:
            steps:
              # Steps for building and pushing the Docker image

Steps

Both stages follow a similar structure, with steps that include checking out the source code and building/pushing a Docker image.

steps:
  - checkout: self
  - task: Docker@2
    displayName: 'Build & Push docker image for generic Sample Application'
    inputs:
      repository: '$(GenericSampleImage)'
      command: buildAndPush
      DockerFIle: 'Dockerfile'
      containerRegistry: '$(ACRServiceConnection)'
      tags: |
        $(Build.BuildId)
        latest

Customization

To adapt this pipeline for your project, you can modify variables, stages, and steps according to your specific build and deployment requirements. For more customization options, refer to the official Azure DevOps YAML documentation.


Full Pipeline Code

# Define the trigger for the pipeline to run when changes are pushed to the 'main' branch

trigger:
  - main

# Define the VM image to use for the build agent
pool:
  vmImage: ubuntu-latest

# Define the stages of the pipeline
stages:
  # Stage for development environment
  - stage: Dev
    variables:
      # Define variable group for generic Dev environment
      - group: DevEnvironment
    displayName: 'Dev'
    jobs:
      # Define a deployment job for the Dev stage
      - deployment: Dev
        displayName: 'CI/CD Stage for Generic Sample image'
        environment: DevEnvironment
        strategy:
          runOnce:
            deploy:
              steps:
                # Check out the source code
                - checkout: self
                # Build and push the Docker image for the generic Sample Application
                - task: Docker@2
                  displayName: 'Build & Push docker image for generic Sample Application'
                  inputs:
                    repository: '$(GenericSampleImage)'
                    command: buildAndPush
                    DockerFIle: 'Dockerfile'
                    containerRegistry: '$(ACRServiceConnection)'
                    tags: |
                      $(Build.BuildId)
                      latest

  # Stage for higher environments (Higher_Env)
  - stage: HigherEnv
    variables:
      # Define variable group for generic Higher_Env environments
      - group: HigherEnvVariables
    displayName: 'Higher_Env'
    jobs:
      # Define a deployment job for the Higher_Env stage
      - deployment: Higher_Env
        displayName: 'CI/CD Stage for Generic Sample image'
        environment: Higher_Env
        strategy:
          runOnce:
            deploy:
              steps:
                # Check out the source code
                - checkout: self
                # Build and push the Docker image for generic Sample Application
                - task: Docker@2
                  displayName: 'Build & Push docker image for generic Sample Application'
                  inputs:
                    repository: '$(GenericSampleImage)'
                    command: buildAndPush
                    DockerFIle: 'Dockerfile'
                    containerRegistry: '$(ACRServiceConnection)'
                    tags: |
                      $(Build.BuildId)
                      latest