简体   繁体   English

如果在 azure 管道问题中使用变量而不是参数

[英]If with variables not parameters in azure pipeline problem

I am putting following code in azure pipeline:我将以下代码放入 azure 管道中:

- ${{ if eq(variables.enableAddonVM, true) }}:     
  - template: ../task-templates/addonVM.yml
    parameters:
      serviceConnectionName: ${{ variables.serviceConnectionName }}
      customerResourceGroup: $(resourceGroupName)
      location: ${{ variables.location }} 

Following are my observations:以下是我的观察:

  1. The template get skipped.模板被跳过。 I checked the value by printing it in task before it.我通过在它之前的任务中打印它来检查值。 It is showing true.它是真实的。 Still it gets skipped.它仍然被跳过。

  2. I also applied condition next to template as shown below我还在模板旁边应用了条件,如下所示

    • template: ../task-templates/addonVM.yml模板:../task-templates/addonVM.yml

      condition: But still it get failed.条件:但仍然失败。 We were not able to use condition after template.我们无法在模板后使用条件

  3. I also checked addonVM.yml after removing condition.删除条件后,我还检查了 addonVM.yml。 addonVM.yml was working propely. addonVM.yml 工作正常。

So, My final question is how can insert variables within if condition?所以,我的最后一个问题是如何在 if 条件下插入变量? Is there some way to get rid of this problem?有什么办法可以解决这个问题吗?

If with variables not parameters in azure pipeline problem如果在 azure 管道问题中使用变量而不是参数

It depends on whether the variable enableAddonVM already existed before compilation.这取决于变量enableAddonVM在编译之前是否已经存在。

Just as jessehouwing pointed that the template variables ${{ variables.var }} are processed at compile time :正如 jessehouwing 指出模板变量${{ variables.var }}编译时处理:

Check the document Template expression syntax for some more details.查看文档模板表达式语法以获取更多详细信息。

So, if the variable enableAddonVM is generated at previous task, it could not be grabbed at compile time.因此,如果变量enableAddonVM是在先前的任务中生成的,则无法在编译时获取它。 That is because this variable is generated at runtime.那是因为这个变量是在运行时生成的。

To resolve this issue, you could use condition , like:要解决此问题,您可以使用condition ,例如:

  steps: 
    - template: ../task-templates/addonVM.yml
      parameters:
        serviceConnectionName: ${{ variables.serviceConnectionName }}
        customerResourceGroup: $(resourceGroupName)
        location: ${{ variables.location }} 
  condition: eq(variables.enableAddonVM, true) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM