Search code examples
azure-devopsyaml

Azure Pipeline Does not execute conditional logic correctly


I want to add conditional logic to only run a step if a certain repository has been included (this is a generic yaml script).

To achieve this, I have added a variable:

variables:
  templateRepositoryRef: $[ resources.repositories['templates'].ref ]

And added the following check:

steps:
- ${{ if contains(variables['templateRepositoryRef'], 'refs') }}:
    - script: |
        echo templateRepositoryRef contains refs
        echo templateRepositoryRef: $(templateRepositoryRef)
- ${{ else }}:
     - script: |
        echo templateRepositoryRef does not contain refs
        echo templateRepositoryRef: $(templateRepositoryRef)

The idea being that if the repository resource with the alias templates is contained, this will be a non-empty string.

The output of this is:

templateRepositoryRef does not contain refs
templateRepositoryRef: refs/heads/feature/jira-1234
Finishing: CmdLine

In what world does this make sense? Why is the condition behaving the opposite of how I would expect it to.


Solution

  • I reproduced this issue and do not know why the if/else expressions aren't behaving as expected.

    As a workaround I was able to use step conditions to get the desired behavior:

    variables:
      templateRepositoryRef: $[ resources.repositories['templates'].ref ]
      ContainsRefs: $[contains(resources.repositories['templates'].ref, 'refs')]
    
    steps:
    - script: |
        echo templateRepositoryRef contains refs
        echo templateRepositoryRef: $(templateRepositoryRef)
        echo ContainsRefs: $(ContainsRefs)
      condition: eq(variables['ContainsRefs'],True)
    
    - script: |
        echo templateRepositoryRef does not contain refs
        echo templateRepositoryRef: $(templateRepositoryRef)
        echo ContainsRefs: $(ContainsRefs)
      condition: eq(variables['ContainsRefs'],False)