skip to Main Content

I have a condition to run a task in azure job template conditionally if value of parameter is not null, however the task runs even if value of parameter is null.

parameters:
- name: 'new_tag_value'
  default: ''
  type: string

  - task: Bash@3
      displayName: 'Update Image tag in helm chart'
      inputs:
        targetType: filePath
        filePath: '${{ parameters.bash_script_path_image_tag_update }}'
        arguments: '${{ parameters.new_tag_value }} ${{ parameters.svc_value_File }}'
      condition: and(succeeded(), ne('${{ parameters.new_tag_value }}', ''))

2

Answers


  1. When the parameter is null, the check compares null with an empty string. Null is not equal to an empty string, the ne expression therefore evaluates to true and executes.

    Login or Signup to reply.
  2. You can reference the sample below to update your YAML files to make the condition work as expected in the pipeline.

    The template YAML file: Steps_Temp/insert-step.yml
    parameters:
    - name: new_tag_value
      type: string
    
    steps:
    - task: Bash@3
      displayName: 'A template step'
      condition: and(succeeded(), ne('${{ parameters.new_tag_value }}', ' '))
      inputs:
        targetType: inline
        script: |
          echo "This is a template step."
          echo "new_tag_value = ${{ parameters.new_tag_value }}"
    
    The main YAML file: azure-pipelines.yml
    parameters:
    - name: MyTag
      type: string
      default: ' '
    
    stages:
    - stage: A
      displayName: 'Stage A'
      jobs:
      - job: A1
        displayName: 'Job A1'
        steps:
        - template: Steps_Temp/insert-step.yml
          parameters:
            new_tag_value: ${{ parameters.MyTag }}
    
    Result
    • When triggering a new run of the pipeline, if directly using the default value of MyTag and providing a new value, the Bash@3 task will be skipped.

    • When triggering a new run of the pipeline, if providing a new value which is not null or whitespace to MyTag, the Bash@3 task will run.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search