In some cases, you may want to change the value of an Azure Pipeline variable depending on the environment data. When I was looking for a way to do this, I couldn’t find many examples, so I’ll describe my solution here, maybe it will help you too.
Normally, a condition is prepared as follows.
variables:
- name: AZ_IS_MAIN_DEPLOYMENT
value: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
Here, the Variable AZ_IS_MAIN_DEPLOYMENT
gets a true
or false
, depending on the branch. In later steps you use the variable to control tasks, steps and so on by set the condition
value. But in my underlying use case, I needed a condition where the value of my variable gets an production
or dev
value (Btw, it was for the deployment slot of a Static Web App 😉)
My solution is to set a variable value by use the key word if
in a kind of attribute on top of the variable value.
variables:
- name: AZ_DEPLOYMENT_ENV_NAME
${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
value: production
${{ else }}:
value: dev
Very important is the indentation of value
here. It will not work, if it’s not set.
I’ve not tested a else if
condition yet. Feel free to do so and let me know if it works.