Azure DevOps Yaml 管道中使用的 YAML 文件中的双大括号“{{}}”意味着什么?

Viv*_*Dev 7 azure-devops azure-pipelines azure-pipelines-yaml

最近正在研究 Azure DevOps 模板。并遇到了这种双花括号语法。只是想知道如何在 Azure DevOps 中使用双花括号。

看过一些关于相同内容的帖子。

  1. 将条件值分配给 Azure DevOps 中的变量

  2. Yaml 文件中的大括号

  3. 如果是在 Azure DevOps 中

  4. Azure DevOps 中的条件变量赋值

可以说,我在组中定义了一个变量,如下所示。

在 Azure DevOps 中定义变量组

我们还可以在 yaml 文件中定义变量,如下所示。

variables:
- name: BuildConfiguration
  value: 'Release'

- name: finalBuildArtifactName
  value: 'TempFolderName'
Run Code Online (Sandbox Code Playgroud)

什么时候应该使用双大括号语法?

我有关于并使用以下方法来引用变量的想法。

  1. 变量['MyVar']
  2. 变量.MyVar

与上述两个相比,我们可以用双花括号完成什么?

让 yaml 管道中的东西正常工作非常困难。进行更改、签入、运行管道、查看结果并再次返回。这非常耗时,而且至少可以说并不顺利。

Krz*_*tof 10

${{}}语法用于表达式。更何况还有一个$[ <expression> ]。区别如下:

# Two examples of expressions used to define variables
# The first one, a, is evaluated when the YAML file is compiled into a plan.
# The second one, b, is evaluated at runtime.
# Note the syntax ${{}} for compile time and $[] for runtime expressions.
variables:
  a: ${{ <expression> }}
  b: $[ <expression> ]

Run Code Online (Sandbox Code Playgroud)

请在此处查看文档。

正如你可能在这里看到的

steps:
- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Pipeline.Workspace)'
    ${{ if eq(variables['Build.SourceBranchName'], 'main') }}:
      artifact: 'prod'
    ${{ else }}:
      artifact: 'dev'
    publishLocation: 'pipeline'
Run Code Online (Sandbox Code Playgroud)

您可以使用variables['Build.SourceBranchName']语法来访问变量。但,

variables:
  - name: foo
    value: fabrikam # triggers else condition

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: echo "start"
- ${{ if eq(variables.foo, 'adaptum') }}:
  - script: echo "this is adaptum"
- ${{ elseif eq(variables.foo, 'contoso') }}:
  - script: echo "this is contoso"
- ${{ else }}:
  - script: echo "the value is not adaptum or contoso"
Run Code Online (Sandbox Code Playgroud)

您也可以使用variables.foo.

表达式通常用于条件评估、动态步骤/作业/阶段配置等。

  • 天啊,我根本不知道编译时和运行时的概念!了解更多。谢谢 (2认同)