如何:有条件地在Azure DevOps YAML中插入模板?

时间:2020-02-23 03:46:43

标签: azure-devops azure-devops-yaml

这是我尝试完成有条件地插入模板的方式。根据要求,我想根据运行时提供的管道变量调用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)

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'为false时,插入update.yml模板并运行脚本。

很高兴知道:我还在检查如果它是变量而不是参数,是否可以以某种方式使它起作用。如果可以指出变量显示的问题,那将是很好的。

结果如下: enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您的问题现已解决,因为现在允许运行时参数为布尔值。 查看Runtime Parameters。我有一个类似的用例,并且能够使用它实现它。