2

I have an Azure DevOps Deployment YAML Pipeline that creates an Azure App Service and deploys code to it. The actual pipeline is more complex, but I am simplifying it for this question.

Currently my pipeline can successfully deploy to a specific Azure Subscription (Service Connector) with resource names defined in variables.

I need to parametrize the pipeline so that it can deploy to several different environments (means Azure Subscriptions) using multiple Service Connectors. Each environment has a different Azure Resource naming convention.

Is there any way to read the value of pipeline variables from an XML or JSON file? This way I can have multiple config files for each environment and store them as part of my repository.

Is this a right approach for multi-environment deployment pipeline configuration?

Allan Xu
  • 7,998
  • 11
  • 51
  • 122
  • you can use variables, variables can be defined in a file and having a file per envrionment – Thomas May 30 '22 at 07:22
  • In general the way you should handle multi evironment deployments is through stages. With stages you will first deploy to your QA then DEV and then Production environment. You can define dependencies between then and also approvals. Also you can read data from JSON as you indicated and store this information on a variable in order to use in your YAML file. – GeralexGR May 30 '22 at 07:50
  • @Thomas, That is what I am looking for but I am not able to find a streamlined way of doing it in Azure Pipelines. Would you be able to share a URL to the term I can search internet? – Allan Xu May 30 '22 at 19:48
  • These 2 link would be helpful: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#specify-variables and https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#variable-reuse. You can define variables in a file and use a different file for each stage. – Thomas May 31 '22 at 20:13

2 Answers2

5

You can use variable templates. There is another interesting link: Learn more about variable reuse with templates.

Here I have this flat folder structure (for the clarity of the sample):

.
|  deploy-app.job.yaml
|  deploy-app.pipeline.yaml
|  variables.dev.yaml
|  variables.prod.yaml

So here we're trying to run the reusable job deploy-app.job.yaml with different variable sets.

I've defined some variables in each variable.{env}.yaml files

# variables.dev.yaml
variables:
  vmImage: ubuntu-20.04
  serviceConnection: dev-service-connection

# variables.prod.yaml
variables:
  vmImage: ubuntu-20.04
  serviceConnection: prod-service-connection

The deploy-app.job.yaml file accepts a parameter that allow to inject a variable template:

# deploy-app.job.yaml
parameters:
- name: envVariablesTemplate
  type: string

jobs:
- deployment: deploy
  variables:
  # Inject the verianle template here
  - template: ${{ parameters.envVariablesTemplate }}
  pool:
    # Use the variable from the template
    vmImage: ${{ variables.vmImage }}
  strategy:
    runOnce:
      deploy:
        steps:
        - task: AzureCLI@2
          displayName: Hello from azure cli
          inputs:
            # Use the variable from the template
            azureSubscription: ${{ variables.serviceConnection }}
            scriptType: pscore
            scriptLocation: inlineScript
            inlineScript: echo 'Hello from azure cli'

In the main pipeline, I can create different stages and inject the desired vairables:

# deploy-app.pipeline..yaml
stages:
- stage: dev
  condition: succeeded()
  jobs:
  - template: ./deploy-app.job.yaml
    parameters:
      envVariablesTemplate: ./variables.dev.yaml

- stage: prod
  dependsOn: dev
  condition: succeeded()
  jobs:
  - template: ./deploy-app.job.yaml
    parameters:
      envVariablesTemplate: ./variables.prod.yaml

Based on your needs, you can add multiple variable templates, having a naming convention etc. Really up to you and depends on the complexity of your pipelines.

Thomas
  • 24,234
  • 6
  • 81
  • 125
0

By using XML transformation, we can perform the operation. Check the below link to get the complete steps.

https://www.dragonspears.com/blog/how-to-handle-continuous-deployment-across-multiple-environments

  1. Create different stages.

enter image description here

  1. Create build transformation using XML transformer

enter image description here

  1. Another option is to utilize ‘XML variable substitution.’ Sensitive information can be stored and secured within Azure Pipelines vs. in plain text transformation files.

enter image description here

  1. Continuous Deployment

enter image description here

The same steps mentioned here are available in the link mentioned above.

Sairam Tadepalli
  • 1,563
  • 1
  • 3
  • 11
  • Thank you for help. To my understanding, "XML variable substitution" is to update an app service's XML config file not pipeline variables. Am I correct? I am looking for reading the values of pipeline variables (or parameters) from an XML or JSON file. – Allan Xu May 30 '22 at 20:31