I am trying to assign conditional value to an environment variable in Azure DevOps but it is not working. This is my pipeline code:
pr: none
trigger: none
stages:
- stage: Mail
variables:
- group: API_Key
jobs:
- job: Sending_Email
variables:
${{ if ne( variables['RecipientEmail'], '' ) }}:
EmailId: $(RecipientEmail)
${{ if eq( variables['RecipientEmail'], '' ) }}:
EmailId: $(DefaultEmailId)
steps:
- script: echo Mail id after condition is - $(EmailId)
Here I have taken 2 environment variable "RecipientEmail" and "DefaultEmailId" and on the basis of that I want to assign value to the new variable "EmailId". But it not working. In both the cases it is picking up the "DefaultEmailId" value.
For example: DefaultEmailId: [email protected] RecipientEmail: [email protected]
Ideal Output: Mail id after condition is - [email protected]
Current Output: Mail id after condition is - [email protected]
Also In this I am getting a warning: "Duplicate Key" while defining the "EmailId" Variable condition.
If DefaultEmailId: [email protected]
and RecipientEmail: [email protected]
are set in the following variable group: API_Key
, we can see the same issue.
After researching, we find that there is a suggestion ticket about it: https://developercommunity.visualstudio.com/t/azure-pipelines-yaml-set-variable-based-on-conditi/690246, you can vote and follow this ticket. You can also create a new suggestion ticket here. The product group will review these tickets regularly, and consider take it as roadmap.
As a workaround, you could set variables in scripts.
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
if ('$(RecipientEmail)' -eq '') {
Write-Host "##vso[task.setvariable variable=EmailId]$(DefaultEmailId)"
} else {
Write-Host "##vso[task.setvariable variable=EmailId]$(RecipientEmail)"
}
- script: |
echo Mail id after condition is - $(EmailId)