44

I am currently evaluating Azure Pipelines with a small POC and I think I have hit a limitation but wanted to know if anyone had a workaround.

Here is the key parts for what I am trying to do.

azure-pipelines.yml

variables:
  - name: FavouriteSportsTeam
    value: "Houston Rockets"
jobs:
  - template: Build1.yml
    parameters:
      SportsTeam: $(FavouriteSportsTeam)
  - template: Build2.yml
    parameters:
      SportsTeam: $(FavouriteSportsTeam)

Build1.yml

parameters:
  SportsTeam: "A Default Team"
jobs:
  - job: SportsTeamPrinter
    steps:
      - script: "echo ${{ parameters.SportsTeam }}"

Now when I attempt to run this, the variable passed from the azure-pipelines.yml file isn't expanded, and it's left as "$(FavouriteSportsTeam)"

Is it possible to pass an expanded variable as a parameter to another file?

riQQ
  • 9,878
  • 7
  • 49
  • 66
MAHDTech
  • 543
  • 1
  • 4
  • 8
  • have you managed to find the way and pass pipeline variable to the template parameter? I can't get it to work with variables, only constant are working like in the [MS help page](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#passing-parameters) – oleksa Jun 18 '19 at 12:41

4 Answers4

22

I had the same problem with a deployment job within a template where I tried to set the environment depending on a parameter. The template parameter would the receive a run-time variable $(Environment).

The issue was that run-time variables are not yet available at the time the value pass to environment is interpreted. Solution was to not pass the variable in run-time syntax but use the expression syntax ${{ variables.environment }}:

deploy-appservice.yml

parameters:
- name: environment # don't pass run-time variables

jobs:
- deployment: DeployAppService
  environment: ${{ parameters.environment }}
  strategy: [...]

azure-pipelines.yml

- stage: QA
  variables: 
    Environment: QA
  jobs:
  - template: templates/deploy-appservice.yml
    parameters:
      environment: ${{ variables.environment }} # use expression syntax

If you wrongly pass a run-time variable $(Environment) this string is what the deployment job will try to name the Azure DevOps environment. I guess, since this is not a valid name, it will use Test as a fallback name, which then appears in the Environments menu.


I have written a more detailed blog post about managing deployment environments on my personal website.

officer
  • 2,080
  • 1
  • 20
  • 29
  • Reference: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#understand-variable-syntax In a pipeline, template expression variables (${{ variables.var }}) get processed at compile time, before runtime starts. Macro syntax variables ($(var)) get processed during runtime before a task runs. Runtime expressions ($[variables.var]) also get processed during runtime but are intended to be used with conditions and expressions. When you use a runtime expression, it must take up the entire right side of a definition. – ikhvjs Aug 21 '23 at 08:43
12

This works:

azure-pipelines.yml

variables:
  FavouriteSportsTeam: "Houston Rockets"
jobs:
  - template: Build1.yml
    parameters:
      SportsTeam: $(FavouriteSportsTeam)
  - template: Build2.yml
    parameters:
      SportsTeam: $(FavouriteSportsTeam)

Build1.yml

parameters:
  SportsTeam: "A Default Team"
jobs:
  - job: SportsTeamPrinter
    steps:
      - script: "echo ${{ parameters.SportsTeam }}"
Krzysztof Czelusniak
  • 1,217
  • 4
  • 16
  • 26
  • 5
    Just to confirm, this worked because of the way we changed the variable `FavouriteSportsTeam` was defined in `azure-pipelines.yml` ? – Greg Mar 30 '20 at 16:37
  • Important note, this only works for static variables like this case, they're declared in code, and static, not subject to change, so the compiler can actually access them using compile time syntax. But if your variables coming from: - Variable groups - Set in the UI - Set by another task There's currently no way to use them as inputs fortemplate parameters. You'll really need to use runtime syntax inside your template code to reference them. Annoying, but keep this in mind. This happens because paramters by themselves can only be referenced using compile time expressions. – Paulo Correia Jul 21 '23 at 11:37
2

I suppose that template parameters are evaluated before variables are initialized. That is why you are getting $(FavouriteSportsTeam) instead of variable value

I've tried to set template parameter value from the variable in different ways but no luck

In the template parameter value can be resolved with format or without (using the ${{ }} ) like

#template1
parameters:
  poolname: dev4-kyiv
  versionFile: ''

jobs:
  - job: versionJob 
    pool:
      name: ${{ parameters.poolname }}
    steps:
      - powershell: |
          Write-Host ("${{ parameters.versionFile }}")

or with local template variables

#template2
parameters:
  releaseFilePath: ''
  packageTags: '' 
jobs:
  - job: build
    variables:
      releaseNotesFile: '${{ parameters.releaseFilePath }}/releaseNotes.txt'
      tags: '${{ parameters.packageTags }}'

but I can not find the way how to set template parameter value using variable from the main script that uses template.

oleksa
  • 3,688
  • 1
  • 29
  • 54
0

yes, but you have to use format() function:

Inline: |
  ${{ format('$Tags = (Get-AzureRmResourceGroup -Name {0}).Tags
  $Tags.Version = "$(Build.BuildNumber)"
  $Tags.FailedVersion = "-"; Set-AzureRmResourceGroup -Name {0} -Tag $Tags',
        parameters.resourceGroupName ) }}

here's an example. you can also make it multi line for better readability.

https://github.com/Microsoft/azure-pipelines-agent/issues/1686

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • So are you suggesting I use the format function for passing the template the variable or on the template to receive as a parameter. I tried modifying the template like this with no success. `${{ format(${0} parameters.SportsTeam) }}"` – MAHDTech Jan 19 '19 at 00:56
  • you forgot the `''`. there is no other way (except format function) – 4c74356b41 Jan 19 '19 at 06:50