0

We are trying to run a task conditionally, but the condition always seems to be true, even if it shouldn't. We have 3 files that are passing a parameter from the devops variables down to each file:

api1.yml

stages:
  - template: api2.yml
    parameters:
      myParam: $(MyParam)

api2.yml

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

stages:
    jobs:
      - deployment: Web
        environment:
          resourceType: VirtualMachine
        displayName: 'IIS Deployment'
        strategy:
          runOnce:
            deploy:
              steps:
                - download: none
                - download: release
                  artifact: Api
                  displayName: 'Download Api Artifact'
               
                - template: api3.yml
                  parameters:
                    myParam: ${{ parameters.myParam}}

api3.yml

parameters:
  - name: myParam
    type: string 
    default: ''
 
steps:
  - ${{ if ne('${{ parameters.myParam}}', '') }}:
    - task: PowerShell@2
      displayName: Do something .${{ parameters.myParam}}.
      continueOnError: true
      inputs:
        targetType: 'inline'
        script: |
          echo 'Test'

The value for "$(MyParam)" comes from the devops library variables, and has no value set (Just empty). Also the output from the display name of api3.yml shows "Do something .." which indicates, that the value is an empty string. I have also tried to change the if condition:

 ${{ if ne(parameters.myParam, '') }}:
 ${{ if parameters.myParam }}:
 ${{ if and(ne(parameters.myParam, ''), parameters.myParam) }}:
 ${{ if gt(length(parameters.myParam), 1) }}:

But none of it works. Especially the gt (greater than) check is confusing, because somehow there must be a string with at least 1 character. I guess somehow the syntax is wrong for the if statement.

How can we make this if check work?

1 Answer 1

0

Ok I think the problem is, that $() variables are not available during yaml compilation, so it cannot work. What I should use here is the "condition:" parameter inside the task instead of the if-statement and access the variable with "$()" or "variables[]" directly instead of passing it as a parameter.

condition: ne(variables['myParam'], '')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.