这是我尝试有条件地插入模板的方式。根据要求,我想根据运行时提供的管道变量调用 fresh-deploy.yml 或 update.yml。用户可以将名为“freshInstall”的变量编辑为真或假。
主管道(入口点):
# azure-pipelines.yml
variables:
shouldUpdate: 'false'
jobs:
- job: TestJob
pool:
name: "Vyas' Local Machine"
steps:
- checkout: none
- template: ./testIf.yml
parameters:
freshInstall: $(freshInstall)
testif.yml:
# testIf.yml
parameters:
- name: freshInstall
type: string # Can't be boolean as runtime supplied variable values ARE strings
steps:
# set a preexisting variable valued 'false' to 'true'
- powershell: |
$shouldUpdate = 'true'
Write-Host "##vso[task.SetVariable variable=shouldUpdate]$shouldUpdate"
displayName: 'Set Should Update to $(shouldUpdate)'
# Check if the parameter 'freshInstall' is passed in correctly
- script: echo "Should freshInstall ${{ parameters['freshInstall'] }}"
displayName: 'Is Fresh Install? ${{ parameters.freshInstall }}'
# Should skip this
- ${{ if eq(parameters.freshInstall, 'true') }}:
- template: ./fresh-deploy.yml
# Shoud include this
- ${{ if eq(parameters.freshInstall, 'false') }}:
- template: ./update.yml
# Check variables vs parameters. Include as per value set
- ${{ if eq(variables.shouldUpdate, 'true') }}:
- template: ./update.yml
# Use all 3 syntaxes of variable access
- script: echo "shouldUpdate is variables['shouldUpdate']"
displayName: "Should Update? variables.shouldUpdate"
fresh-deploy.yml 的模拟文件:
# fresh-deploy.yml
steps:
script: echo 'Kick off fresh deploy!'
update.yml 的模拟文件:
# update.yml
steps:
script: echo 'Updating existing installation!'
关键问题:当变量“freshInstall”为假时,预期会插入 update.yml 模板并运行脚本。
很高兴知道:如果它是一个变量而不是一个参数,我也在检查我是否能以某种方式让它工作。如果我能指出我在变量显示上做错了什么,那就太好了。