azure-devopsazure-pipelines

Unexpected value 'condition'


I am trying to resolve my config issue with Azure DevOps Pipeline. I am using templates in my main pipeline, but there some cases that the next pipeline should work only if previous one was passed. That previous template passed result is based on the output from the log.

Short version of my pipeline:

   - deployment: Development
     <cutting off the rest of the code>
                - checkout: APP
                - script: |
                    ansible-playbook \
                      playbooks/installation.yml
                - template: templates/app_log.yml@Pipelines
                  parameters:
                    ssh: $(Endpoint)
                    port: $(Port)
                - template: templates/app_symlink.yml@Pipelines
                  condition: and(succeeded(), eq(variables['appLog.status'], 'Succeeded'))
                  parameters:
                    ssh: $(Endpoint)
                    port: $(Port)

templates/app_app.yml

parameters:
  - name: ssh
    type: string
    displayName: 'SSH'
  - name: port
    type: string
    displayName: 'Port'
  - name: name
    type: string
    default: 'appLog'
<cutting off the rest of the code>

When the app_log.yml finishing its job by checking the status of all tests it shows All tests passed which should be treat as trigger to create a symlink to the new version of app by path, however with my attempts of creating some if before the -template or as I showed in the above code it leads to the error each time (now it shows the error like in the title). On the other hand, even if one test failed it shouldn't create any symlink.


Solution

  • You cannot set conditions when using templates, so the following is invalid:

    - template: templates/app_symlink.yml@Pipelines
      condition: and(succeeded(), eq(variables['appLog.status'], 'Succeeded'))
      parameters:
        # ...
    

    The workaround is to add a condition parameter to the templates/app_symlink.yml template - example:

    # templates/app_symlink.yml
    
    parameters:
      - name: condition
        type: string
        displayName: 'Condition'
        default: ''
    
      # other parameters here
    
    steps:
      - script: |
          echo "Hello world"
        displayName: 'Hello world'
        ${{ if ne(parameters.condition, '') }}:
          condition: ${{ parameters.condition }}
    

    You'll have to do the same thing to all steps in the template that depend on the condition.

    Using the template:

    - template: templates/app_symlink.yml@Pipelines
      parameters:
        # ...
        condition: and(succeeded(), eq(variables['appLog.status'], 'Succeeded'))
        # ...
    

    Consider adding other parameters to your jobs/steps templates as well, in order to make them configurable and extensible such as timeoutInMinutes, retryCountOnTaskFailure, etc.