这里是我尝试有条件地插入模板的方法。根据运行时提供的管道变量,我想要调用fresh-deploy.yml或update.yml。用户可以编辑名为“freshInstall”的变量,将其设置为true或false。
主管道(入口点):
主管道(入口点):
# azure-pipelines.yml
variables:
shouldUpdate: 'false'
jobs:
- job: TestJob
pool:
name: "Vyas' Local Machine"
steps:
- checkout: none
- template: ./testIf.yml
parameters:
freshInstall: $(freshInstall)
测试if.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”为false时插入update.yml模板并运行脚本。
值得注意的是:我还在检查是否可以将其作为变量而不是参数来使其工作。如果能指出我在变量显示方面做错了什么,那就太好了。
shouldUpdate
的值仍为“false”。将“Should Update?”任务更改为-脚本:echo“shouldUpdate is $(shouldUpdate)” displayName:“Should Update?$(shouldUpdate)”,它将显示“shouldUpdate is true”。 完成:Should Update? false,这可能更容易理解。 - Yang Shen - MSFT$[ <expression> ]
并不能改变该表达式必须在编译时的事实。您将会遇到错误:Unexpected Value。 - Yang Shen - MSFT