1

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: abc@example.com RecipientEmail: xyz@example.com

Ideal Output: Mail id after condition is - xyz@example.com

Current Output: Mail id after condition is - abc@example.com

Also In this I am getting a warning: "Duplicate Key" while defining the "EmailId" Variable condition.

Ankit Soni
  • 95
  • 2
  • 13
  • Are you sure that your RecipientEmail variable is defined in the API_Key variable groups? If it's not defined at the moment your pipeline is running, it will default to empty strings. https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch – Bruno May 12 '21 at 15:16
  • Hi @Bruno Yes I have define the variables in the variable group. Still I am getting this issue. And I am not getting the empty string output. I am getting "DefaultEmailId" variable value as a output in both the conditions. – Ankit Soni May 14 '21 at 04:21

1 Answers1

1

If DefaultEmailId: abc@example.com and RecipientEmail: xyz@example.com are set in the following variable group: API_Key, we can see the same issue. enter image description here

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)
Edward Han-MSFT
  • 2,879
  • 1
  • 4
  • 9
  • Hi @Edward-Han_MSFT . Is there any way we can do the same task via bash? – Ankit Soni May 18 '21 at 10:40
  • Yes, you can. Please refer to ```- bash: | echo "##vso[task.setvariable variable=EmailId]$(DefaultEmailId)"``` to modify the scripts. See: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-variables-in-scripts for details. – Edward Han-MSFT May 19 '21 at 01:25