skip to Main Content

I want to pass a variable called publish defined in the pipeline definition UI into a template called echo.yml from the main pipeline.yml then in the template use the value of the variable as a condition on a task running or not.

So far no success.

Is this actually possible ?

Simplified example below, the real use case is to control whether or not a publish task executes.

echo.yml:

parameters:
- name: 'desc'
  type: string
  default: none
- name: 'publish'
  type: string
  default: 'false'

steps:
- task: Bash@3
  condition: ${{ parameters.publish  }}
  inputs:
    targetType: 'inline'
    script: |
      echo desc is ${{ parameters.desc }}
      echo publish is ${{ parameters.publish }}


pipeline.yml:

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      # Write your commands here
      echo SBN IS $(Build.SourceBranchName)
      echo SB IS $(Build.SourceBranch)

- template: echo.yml
  parameters:
    desc: 'my description'
    publish: ${{ variables.publish }}

2

Answers


  1. Given that you want to run a task or not based on a parameter, you can use a template expression ${{ if ... }} instead of using a condition.

    Example:

    # echo.yml
    
    parameters:
      - name: desc
        type: string
        default: none
        
      - name: publish
        type: boolean    <---------- change to boolean
        default: false
    
    steps:
    - ${{ if parameters.publish }}:
      - task: Bash@3
        inputs:
          targetType: 'inline'
          script: |
            echo desc is ${{ parameters.desc }}
            echo publish is ${{ parameters.publish }}
    
    # other tasks here
    

    Using a condition:

    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          echo desc is ${{ parameters.desc }}
          echo publish is ${{ parameters.publish }}
      condition: and(succeeded(), eq('${{ parameters.publish }}', 'true'))
    

    Note:

    • parameter publish can be changed to boolean, which will be displayed as a checkbox when queuing a new build.
    Login or Signup to reply.
  2. If you define the variable in pipeline definition UI, it cannot get the value as parameter ${{ variables.publish }} for template.

    Instead, you can define the variable in main yaml directly,it works then.

    trigger: none
    
    variables:
    - name: publish
      value: false           # define variable in main yaml
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          # Write your commands here
          echo SBN IS $(Build.SourceBranchName)
          echo SB IS $(Build.SourceBranch)
    
    - template: echo.yml
      parameters:
        desc: 'my description'
        publish: ${{ variables.publish }}
    

    enter image description here

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