Azure管道YAML:意外值“变量”

pu3pd22g  于 2023-01-05  发布在  其他
关注(0)|答案(3)|浏览(157)

我正在将Azure管道用作Azure DevOps的一部分。我正在尝试在模板文件中定义变量,因为我需要多次使用同一个值。
这是我的stage-template.yml

parameters:
 - name: param1
   type: string
 - name: param2
   type: string

variables:
  var1: path/${{ parameters.param2 }}/to-my-file.yml

stages:
 - stage: Deploy${{ parameters.param2 }}
   displayName: Deploy ${{ parameters.param1 }}
   jobs:  
    ...

当尝试使用此管道时,我收到一条错误消息:
/stage-template. yml(行:7,第1列):意外值"变量"
为什么不管用?我做错什么了?

zpgglvta

zpgglvta1#

不能在作为阶段、作业或步骤模板包括的模板中包含variables(即,包含在管道中的stagesjobssteps元素之下)。只能在变量模板中使用variables
遗憾的是,文档对此并不十分清楚。

包括后台文件模板

# pipeline-using-stage-template.yml

stages:
- stage: stage1
[...]
# stage template reference, no 'variables' element allowed in stage-template.yml
- template: stage-template.yml

包括变量模板

# pipeline-using-var-template.yml

variables:
# variable template reference, only variables allowed inside the template
- template: variables.yml

steps:
- script: echo A step.

如果使用模板在管线中包括变量,则所包括的模板只能用于定义变量。
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#variable-reuse

3qpi33ja

3qpi33ja2#

管道中不能有参数,只能在templateReferences:

name: string  # build numbering format
resources:
  pipelines: [ pipelineResource ]
  containers: [ containerResource ]
  repositories: [ repositoryResource ]
variables: # several syntaxes, see specific section
trigger: trigger
pr: pr
stages: [ stage | templateReference ]

如果你想在模板中使用变量,你必须使用正确的语法:

parameters:
 - name: param1
   type: string
 - name: param2
   type: string

stages:
- stage: Deploy${{ parameters.param2 }}
  displayName: Deploy ${{ parameters.param1 }}
  variables:
    var1: path/${{ parameters.param2 }}/to-my-file.yml
  jobs:  
  ...
brvekthn

brvekthn3#

这对我很有效:
在您的父级yaml中:

stages:
- stage: stage1
  displayName: 'stage from parent'
  jobs:
  - template: template1.yml  
    parameters:
        param1: 'somevalueforparam1'

内部模板1:

parameters:  
    param1: ''  
    param2: ''    
jobs:
- job: job1  
  workspace:    
    clean: all  
    displayName: 'Install job'  
   pool:    
      name: 'yourpool'  
  variables:
   - name: var1     
     value: 'value1'

相关问题