microsoft / azure-pipelines-yaml

Azure Pipelines YAML examples, templates, and community interaction
MIT License
1.19k stars 924 forks source link

Dynamic display name with variables doesn't work on template. #574

Closed chyin6 closed 1 year ago

chyin6 commented 1 year ago

I'm using YAML stage template and trying to make my stage display name dynamic. But the display name is variable itself not the variable value like:

publish $(packageType) pkgs

I made a minimal pipeline to reproduce it:

parameters:
  - name: distro
    displayName: "Linux distro"
    default: "Ubuntu18.04"
    values:
      - "RHEL7"
      - "RHEL8"
      - "Ubuntu18.04"
      - "Ubuntu20.04"
  - name: pool
    displayName: "pool"
    type: string

resources:
  repositories:
    - repository: self
      clean: true
      ref: "main"

variables:
  - name: packageType
    ${{ if eq(parameters.distro, 'RHEL7') }}:
      value: "rpm"
    ${{ elseif eq(parameters.distro, 'RHEL8') }}:
      value: "rpm"
    ${{ elseif eq(parameters.distro, 'Ubuntu18.04') }}:
      value: "deb"
    ${{ elseif eq(parameters.distro, 'Ubuntu20.04') }}:
      value: "deb"

pool:
  name: ${{ parameters.pool }}

stages:
  - template: templates/display_name.yml
    parameters:
      packageType: $(packageType)

And the template is:

parameters:
  - name: packageType
    type: string
    displayName: "deb or rpm"

stages:
  - stage: publish
    displayName: "publish ${{ parameters.packageType }} pkgs"
    jobs:
      - job: "publish"
        displayName: "publish ${{ parameters.packageType }} pkgs"
        container:
          image: ubuntu:18.04
        steps:
          - bash: echo "publish ${{ parameters.packageType }} pkgs"
            displayName: "Log"
wesmacdonald commented 1 year ago

Good morning,

You need to use the following template expression syntax for packageType, this will process it at compile time and the value will be displayed

stages:
  - template: templates/display_name.yml
    parameters:
      packageType: ${{ variables.packageType }}

Cheers, Wes

chyin6 commented 1 year ago

Thank you Wes for your help. It works.