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

Posted

技术标签:

【中文标题】如何:在 Azure DevOps YAML 中有条件地插入模板?【英文标题】:Howto: Insert a template conditionally in Azure DevOps YAML? 【发布时间】:2020-02-23 03:46:43 【问题描述】:

这是我尝试有条件地插入模板的方法。根据要求,我想根据运行时提供的管道变量调用 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' 为 false 时,预期会插入 update.yml 模板并运行脚本。

很高兴知道:如果它是一个变量而不是一个参数,我也在检查我是否能以某种方式让它工作。如果我能指出我在变量显示方面做错了什么,那就太好了。

结果如下:

【问题讨论】:

我确实找到了这个链接,它讨论了它为什么不起作用:运行时间与编译时间。但是,不是那么清楚developercommunity.visualstudio.com/content/problem/653819/… 在编译时,'Set Should Update to false' 还没有运行,这意味着变量shouldUpdate 的值仍然是'false'。将Should Update? 任务更改为 - script: echo "shouldUpdate is $(shouldUpdate)" displayName: "Should Update? $(shouldUpdate)" 它将显示“shouldUpdate is true” Finishing: Should Update? false,这可能更容易理解。 @YangShen-MSFT:那么以下应该可以工作,但不能:- $[ if eq($ parameters.freshInstall , 'true') ]: - template: ./新鲜部署.yml 不,先生,它必须是编译时,将其更改为$[ <expression> ] 不会改变这个表达式必须在编译时的事实。你会遇到错误:Unexpected Value. @YangShen-MSFT:如果您能提供一种解决方法,说明如何使用队列时间变量值完成条件插入,那就太好了。谢谢! 【参考方案1】:

这个解决方案现在对我有用:

- $ if eq(parameters.generateSwaggerFiles, true) :
    - template: generate-swagger-files.yaml
      parameters:
        appName: 'example'
        swaggerVersions:
          - v1
          - v2

查看文档here。

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于如何:在 Azure DevOps YAML 中有条件地插入模板?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 azure devops 中解析 yaml 文件

Azure Devops:是不是可以将 yaml 模板嵌套在另一个 yaml 模板中?

Azure DevOps Pipelines - 仅在上一次运行成功时运行 YAML 管道

为啥我的多阶段完整 yaml azure devops 管道在移动到模板时会中断?

如何在 Azure DevOps yaml 管道中设置 ENVIRONMENT 状态

如何在 azure devops YAML 管道中将单个代理用于多个作业/阶段