Reputation: 733
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?
Upvotes: 63
Views: 52729
Reputation: 2448
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 }}
:
parameters:
- name: environment # don't pass run-time variables
jobs:
- deployment: DeployAppService
environment: ${{ parameters.environment }}
strategy: [...]
- 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.
Upvotes: 33
Reputation: 1317
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 }}"
Upvotes: 19
Reputation: 4007
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.
Upvotes: 3
Reputation: 72151
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
Upvotes: 0