变量定义

使用名称/值对定义变量。

引用此定义的定义:管道stage.stagejobs.jobjobs.deployment

实现

执行 说明
变量:字符串字典 使用名称/值对定义变量。
变量:变量列表 按名称、变量组或在模板中定义变量。

注解

variables 关键字使用两种语法形式:变量列表和映射(字符串字典)。

在映射语法中,所有键都是变量名称,其值是变量值。 若要使用变量模板,必须使用列表语法。 列表语法要求指定是提及变量(name)、变量组(group)还是模板(template)。

不能在同一个变量节中使用列表和映射变量,但在使用列表语法时,可以合并 namegrouptemplate

可以在管道、阶段或作业级别指定变量。

变量:字符串字典

使用名称/值对定义变量。

variables:
  string: string # Name/value pairs

没有。

例子

对于一组简单的硬编码变量,请使用以下映射语法:

variables: { string: string }

在不同范围内定义的变量:

variables:      # pipeline-level
  MY_VAR: 'my value'
  ANOTHER_VAR: 'another value'

stages:
- stage: Build
  variables:    # stage-level
    STAGE_VAR: 'that happened'

  jobs:
  - job: FirstJob
    variables:  # job-level
      JOB_VAR: 'a job var'
    steps:
    - script: echo $(MY_VAR) $(STAGE_VAR) $(JOB_VAR)

变量:变量列表

按名称、变量组或在模板中定义变量。

variables: [ name | group | template ] # Define variables by name, variable group, or in a template.

列表类型

类型 说明
variables.name 使用名称和完整语法定义变量。
variables.group 引用变量组中的变量。
variables.template 在模板中定义变量。

例子

若要包含变量组,请切换到以下序列语法:

variables:
- name: string  # name of a variable
  value: string # value of the variable
- group: string # name of a variable group

可以重复 name/value 对和 group

变量也可以设置为只读,以 增强安全性

variables:
- name: myReadOnlyVar
  value: myValue
  readonly: true

还可以从模板 包含变量。

序列语法:

variables:
- name: MY_VARIABLE           # hard-coded value
  value: some value
- group: my-variable-group-1  # variable group
- group: my-variable-group-2  # another variable group

另请参阅