【问题标题】:Azure Pipelines YAML: Unexpected value 'variables'Azure Pipelines YAML:意外的值“变量”
【发布时间】:2020-03-29 10:13:16
【问题描述】:

我使用 Azure Pipelines 作为 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 (Line: 7, Col: 1): Unexpected value 'variables'

为什么这不起作用?我做错了什么?

【问题讨论】:

标签: azure azure-devops azure-pipelines azure-pipelines-yaml


【解决方案1】:

管道中不能有参数,只能在模板引用中:

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:  
  ...

【讨论】:

  • 我可以拥有它们,因为我使用的是模板。请参阅:docs.microsoft.com/en-us/azure/devops/pipelines/process/… 整个管道(包括参数)在没有变量部分的情况下可以正常工作。
  • 好吧,在那种情况下你不能有变量,它们应该在舞台(或工作)下
  • 你是说不能在模板中使用变量?还是需要以不同的方式使用?你能提供一个我如何使用它们的例子吗?
  • 你可以在管道中有参数。我一直在使用它们,用于管道初始化时预期的输入。 docs.microsoft.com/en-us/azure/devops/pipelines/… '您可以在模板和管道中使用参数。'
  • 此功能(通过模板重用变量)在 Azure Devops Server 2019 上不存在,在更高版本中可用。请检查您的版本。
【解决方案2】:

这对我有用:

在你的父 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'

【讨论】:

    猜你喜欢
    • 2020-12-02
    • 1970-01-01
    • 2021-03-22
    • 2020-06-09
    • 1970-01-01
    • 2022-08-11
    • 2022-01-01
    • 2021-02-22
    • 2021-02-22
    相关资源
    最近更新 更多