Deploy software to Azure from AzureDevOps with YAML files

77 views Asked by At

I have a question about deploying to different environments (Test / Production) with one pipeline.

In this scenario I have a test and production environment. With the same configuration for deploying web application to Azure for two countries. In the current situation we have a release pipeline in the production and test with two stages. One stage for country A and one stage for country B.

What I did is as follows:

  1. I created an variable group (with Azure key vault).
  2. I created an deployment YAML file for my stages / steps.
  3. In that deployment YAML file I am reffering to the variables.yml file.
  4. My deployment YAML file has two stages.
    • First stage for country A
    • Second stage for country B
  5. Both of the stages are using the same steps but different description and CosmosDB container etc..

What I want to do is:

  1. Using one stage in the deployment YAML.
  2. Using two variables.yml, one for each country.
  3. Depending on wether it is country A or B, pointing to the correct variables.yml file so it can make use of the correct values.

The same goes for the production and test environment. depending on the environment, I want to be able to point to the correct YAML variables.yml.

How can I achieve this?

1

There are 1 answers

0
Alvin Zhao - MSFT On BEST ANSWER

Unclear what obstructed your design pipeline, however, based on your current requirements, the YAML pipeline structure was straightforward. Since you have two variables template .yml files, you can import diffrent templates in respective deployment jobs and each deployment can be configured to run against its corresponding environment.

# Your used to deploy web apps via release pipeline, so assumed there was already a pipeline resource that generated build artifacts. To use that build artifact from the pipeline resource below:
resources:
  pipelines:
  - pipeline: BuildWebApp
    project: TheProjectName
    source: ThePipelineName

stages:
- stage: Deploy
  displayName: Deploy to Azure Web Apps
  jobs:
  - deployment: DeploymentJobA
    displayName: Deploy to Production - Country A
    variables:
    - template: variablesA.yml
    environment: Prod
    strategy:
      runOnce:
        deploy:
          steps:
          - download: BuildWebApp
            displayName: Download artifacts for deployment from the pipeline resource
          - script: |
              echo "ARM Service Connetion for deployment is $(ARMSvcCnnName)"
              echo "Azure Web App name is $(AzureWebAppName)"
            displayName: Check the variable values in variables.yml
          - task: AzureRmWebAppDeployment@4
            inputs:
              ConnectionType: 'AzureRM'
              azureSubscription: '$(ARMSvcCnnName)'
              appType: 'webApp'
              WebAppName: '$(AzureWebAppName)'
              packageForLinux: '$(System.DefaultWorkingDirectory)/**/*.zip'
  
  - deployment: DeploymentJobB
    displayName: Deploy to Test - Country B
    variables:
    - template: variablesB.yml
    environment: Test
    strategy:
      runOnce:
        deploy:
          steps:
          - download: BuildWebApp
            displayName: Download artifacts for deployment from the pipeline resource
          - script: |
              echo "ARM Service Connetion for deployment is $(ARMSvcCnnName)"
              echo "Azure Web App name is $(AzureWebAppName)"
            displayName: Check the variable values in variables.yml
          - task: AzureRmWebAppDeployment@4
            inputs:
              ConnectionType: 'AzureRM'
              azureSubscription: '$(ARMSvcCnnName)'
              appType: 'webApp'
              WebAppName: '$(AzureWebAppName)'
              packageForLinux: '$(System.DefaultWorkingDirectory)/**/*.zip'

enter image description here

Even though the variable names in each template are the same, since the two templates variablesA.yml and variablesB.yml work for respective deployment job scope, the variable values are different during runtime.