Azure DevOps YAML Pipeline set deploy environment based on variable

76 views Asked by At

I am trying to write an Azure DevOps YAML pipeline where I set the deployment environment dynamical in a predecessor job. Here is a conceptual example of what I am trying to do:

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: SetVariable
  jobs:
  - job: SetVariableJob
    steps:
    - script: |
        echo "##vso[task.setvariable variable=environmentName;isOutput=true]TestEnvironmentName"
      name: setvarStep

- stage: Deploy
  dependsOn: SetVariable
  variables:
    environmentName: $[ dependencies.SetVariable.outputs['SetVariableJob.setvarStep.environmentName'] ]
  jobs:
  - deployment: DeployWeb
    environment: $(environmentName)
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo Hello, $(environmentName)!

Something is wrong, as the echo Hello, $(environmentName)! just outputs: Hello, !

2

There are 2 answers

0
wenbo On BEST ANSWER

azure-pipelines.yml

pool:
  vmImage: 'ubuntu-latest'

variables:
- name: envname
  value: test

stages:
- stage: SetVariable
  jobs:
  - job: SetVariableJob
    steps:
    - script: |
        echo "##vso[task.setvariable variable=environmentName;isOutput=true]TestEnvironmentName"
      name: setvarStep

- stage: Deploy
  dependsOn: SetVariable
  variables:
    environmentName: $[ stageDependencies.SetVariable.SetVariableJob.outputs['setvarStep.environmentName'] ]
  jobs:
  - deployment: DeployWeb
    environment: ${{ variables.envname }}
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo Hello, $(environmentName)!

There are three things to pay attention to

dynamically setting variables using setvariable has different format-pattern in different scenarios

  1. across stages setvariable, link
  2. deployment job setvariable, docs have sample code
  3. deployment job environment name should be fixed during whole-yaml file compilation, so do not provide the value which generated during the process.
2
Jiawei Shi - MSFT On

You can't use environment: $(environmentName), as Macro syntax variables $(var) is runtime variable. environment only accept compile time variable or parameter.

Compile time variable(template expression variables (${{ variables.var }}) ):

variables:
- name: environmentName
  value: devTest 


- stage: Deploy
  dependsOn: SetVariable
  jobs:
  - deployment: DeployWeb
    environment: ${{ variables.environmentName}}
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo Hello

Parameter:

parameters:
  - name: environmentName
    type: string
    default: dev
    values:
      - dev
      - test


- stage: Deploy
  dependsOn: SetVariable
  jobs:
  - deployment: DeployWeb
    environment: 
      name: ${{ parameters.environmentName }}
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo Hello

In addition, if you want to use cross stage variable, you could use

 environmentName: $[ stageDependencies.SetVariable.SetVariableJob.outputs['setvarStep.environmentName'] ]

The correct format should be

stageDependencies.stageName.jobName.outputs['stepName.variableName']