Search code examples
if-statementbuildyamlazure-pipelinesequals

Conditions in yaml pipeline like "eq" and "ne" is not working


I have created a pipeline build variable "svctag" and assigned its value to blank (empty string). Now trying to execute below script inside a job in yaml file. Every time it prints "svctag is not blank". Not sure what I am doing wrong. Please help.

- ${{ if eq('$(svctag)', '') }}:
   - script: echo 'svctag is blank'
- ${{ if ne('$(svctag)', '') }}:
   - script: echo 'svctag is not blank'

Solution

  • As I know, this format is only applied in YAML template. Now, the issue is the variable called method you are using is not correct.

    You should use the format variables['svctag'] instead of $(svctag) to access the variable which declared previously while you using YAML.

    So, you should change your script as

      - ${{ if eq(variables['svctag'], '') }}:
        - script: echo it is true 
      - ${{ if ne(variables['svctag'], '') }}:
        - script: echo it is false
    

    Here is the output on my side.

    enter image description here


    Another solution is define svctag under parameters:

    parameters:
      svctag: '' 
    

    Then use parameters with step:

      steps:
      - ${{ if eq(parameters.svctag, '') }}:
        - script: echo it is true
      - ${{ if ne(parameters.svctag, '') }}:
        - script: echo it is false
    

    Note: Please use parameters.svctag.