Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Azure DevOps管道模板中使用变量_Azure_Azure Devops_Yaml_Azure Devops Pipelines - Fatal编程技术网

在Azure DevOps管道模板中使用变量

在Azure DevOps管道模板中使用变量,azure,azure-devops,yaml,azure-devops-pipelines,Azure,Azure Devops,Yaml,Azure Devops Pipelines,我们有一组Azure DevOps管道模板,可以跨多个存储库重复使用。因此,我们希望有一个包含所有模板变量的文件 回购结构如下所示 template repo ├── template-1.yml ├── template-2.yml └── variables.yml project repo ├── ... └── azure-pipelines.yml ... variables: foo: bar resources: repositories:

我们有一组Azure DevOps管道模板,可以跨多个存储库重复使用。因此,我们希望有一个包含所有模板变量的文件

回购结构如下所示

template repo
  ├── template-1.yml
  ├── template-2.yml
  └── variables.yml

project repo
  ├── ...
  └── azure-pipelines.yml
...
variables:
  foo: bar
resources:
  repositories:
    - repository: build-scripts
      type: git
      name: project-name/build-scripts

steps:
  ...
  - template: template-1.yml@build-scripts
    
variables.yml
如下所示

template repo
  ├── template-1.yml
  ├── template-2.yml
  └── variables.yml

project repo
  ├── ...
  └── azure-pipelines.yml
...
variables:
  foo: bar
resources:
  repositories:
    - repository: build-scripts
      type: git
      name: project-name/build-scripts

steps:
  ...
  - template: template-1.yml@build-scripts
    
template-1.yml
中,我们导入
变量.yml
,如中所述

在azure pipelines.yml中,我们使用的模板如下

template repo
  ├── template-1.yml
  ├── template-2.yml
  └── variables.yml

project repo
  ├── ...
  └── azure-pipelines.yml
...
variables:
  foo: bar
resources:
  repositories:
    - repository: build-scripts
      type: git
      name: project-name/build-scripts

steps:
  ...
  - template: template-1.yml@build-scripts
    
当我们现在尝试运行管道时,会收到以下错误消息:

template-1.yml@build-scripts (Line: 10, Col: 1): Unexpected value 'variables'

问题是因为您在步骤范围中使用了变量模板。而
变量
在该级别根本不存在。这应该适合您:

资源:
存储库:
-存储库:构建脚本
类型:git
名称:项目名称/生成脚本
变量:
-模板:模板-1。yml@build-剧本
步骤:
...
这可以在任何可能使用变量的地方使用。例如,你可以这样使用它:

作业:
-工作:我的工作
时间:10分钟
变量:
-模板:模板-1.yml#模板参考
游泳池:
vmImage:'ubuntu-16.04'
步骤:
-脚本:echo我最喜欢的蔬菜是${{variables.favoriteVeggie}。

如果模板文件只有
变量,可以参考Krzysztof Madej的答案

如果模板文件同时包含如下所示的
变量
步骤
,则它只能由用户使用

或者您可以在一个阶段中编写它们,如下所示

# File: template-1.yml
stages:
- stage: {stage}
  variables: ...
  jobs:
  - job: {job}
    steps: ...
然后将其作为单独的阶段插入

# azure-pipelines.yml
stages:
- stage: ...
- template: template-1.yml
这种方法可以帮助别人,所以我决定把它贴在这里

对于这种情况,还有一种“变通方法”,可能有点“脏”,因为每次执行模板时都需要显式指定参数,如果要传递很多参数,这不是一个好主意。(实际上,有一种方法,请阅读下面的改进版本)但是,如果您确实想要或没有那么多参数,这应该可以:

逻辑是:变量模板中包含所有参数,如
模板/vars.yml

variables:
  - name: myVar1
    value: MyValue1
  - name: myVar2
    value: MyValue2
由于变量中包含了所需的所有内容,所以可能不需要将变量导入模板本身,因为模板将在管道中执行,这将导入变量,您可以显式替换它,如下面的示例所示:

模板/my template setup env.yml
的内容(其中没有变量):

my azure pipeline.yml
的内容(使用导入变量模板):

改进版

但是,如果您的参数和变量在所有管道和模板中都具有唯一的命名,则在使用模板时不必显式指定它是安全的,这也会起作用:

my azure pipeline.yml的编辑和缩短版本(如果模板中的变量和参数名称相同):

templates/my template setup env.yml
则应如下所示:

steps:
  - script: |
      echo "$(myVar2)" # not myVar3, since myVar3 is not in initial variables file templates/vars.yml

或者您还需要将剩余变量(在第一种情况下是myVar3)添加到
模板/vars.yml
文件中。

不幸的是,它不起作用。我需要步骤模板中的变量,因为我希望在另一个管道的job.steps部分中包含步骤。因此,我的要求是,我可以访问在变量模板中定义的步骤模板中的变量。您可以在变量可用的任何级别使用变量模板。我的编辑对你有帮助吗?