azureazure-devops

Bash syntax for equality check of Azure template param containing any string


In an Azure Devops YAML pipeline I'm passing in a parameter which contains all manner of things - double and single quotes, newlines, etc. It'll vary on each pipeline run.

Inside the template, I need to do a very simple equality check of that value against a specificx string, "valueX" like in the example below:

 parameters:
   - name: strValue

 steps:
 - task: Bash@3
   displayName: 'Do stuff'
   inputs:
     targetType: inline
     script: |
       if [ "${{ parameters.strValue }}" == "valueX" ]; then 
          ...
       fi

But I can't hit on the right syntax. parameters.strValue gets actually evaluated, so the entire string is output within the bash script, causing syntax errors when it contains certain characters. I was hoping to do a comparison using the var name as a reference instead.

I tried using heredoc syntax in the bash script to store the evaluated parameter value into a var, but that doesn't appear to work within the azure-YAML setting - it can never find the closing EOF. Perhaps there's a much simpler solution to this?


Solution

  • This is a bash and interpolation parameter issue with with ADO pipelines. This should work using env to call the parameter

    parameters:
      - name: strValue
    
    steps:
    - task: Bash@3
      displayName: 'Do stuff'
      env:
        PARAMETER: '${{ parameters.strValue }}'
      inputs:
        targetType: inline
        script: |
          if [ "$PARAMETER" == "valueX" ]; then 
            echo "Matching tested and confirmed"
          fi
    

    I tested this and I can confirmed it worked.