Azure Pipelines YAML:意外的值“变量”和意外的“阶段”

Str*_*hez 0 yaml azure-devops azure-pipelines

我已经非常接近我的模板和使用它的部署 yaml。但是我遇到了错误

意外值“变量”意外值“阶段”

我确信我的语法是错误的,但我一生都无法理解为什么。

这是我的模板的开始

#File: template.yml
parameters:
- name: repositoryName
  type: string
  default: ''

variables:
  tag: '$(Build.BuildId)'
  buildVmImage: 'ubuntu-latest'
  deployPool: 'deploy-pool'

stages:
- stage: Build
  jobs:
  - job: Build

    pool:
      vmImage: $(buildVmImage)

    steps:
    - checkout: self
      submodules: recursive
Run Code Online (Sandbox Code Playgroud)

这是使用它的部署 yaml

# Repo: Organization/Project
# File: azure-pipelines.yml
trigger:
- develop
- next
- master

resources:
  repositories:
    - repository: template
      type: git
      name: AzureDevOps/AzureDevOps

jobs:
- template: template.yml@template
  parameters:
    repositoryName: 'exampleName'
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激。我确定它就在我的鼻子前,但我已经挣扎了好几天,所以我认为是时候寻求帮助了。

Lan*_*SFT 6

意外的价值“阶段”

您为template.yml定义了一个模板stages,而您在jobselement下引用它。

多级流水线的正确格式是:

stages:
- stage: Build
  jobs:
  - job: Build
    pool:
      vmImage: $(buildVmImage)
    steps:
    - checkout: self
      submodules: recursive
  - job: Job2
    pool:
      vmImage: $(buildVmImage)
    steps:
    ...
- stage: Deploy
  jobs:
  - job: Deploy
    pool:
      vmImage: $(buildVmImage)
    steps:
    ...
Run Code Online (Sandbox Code Playgroud)

stage=>stage=>jobs=job,所以你不能stagesjobs元素下引用模板。改变

jobs:
- template: template.yml@template
Run Code Online (Sandbox Code Playgroud)

stages:
- template: template.yml@template
Run Code Online (Sandbox Code Playgroud)

会解决这个问题。

意外值“变量”

如果变量用于整个管道,请将其移动到azure-pipelines.yml

trigger:
- master

variables:
  tag: '$(Build.BuildId)'
  buildVmImage: 'ubuntu-latest'
  deployPool: 'deploy-pool'
Run Code Online (Sandbox Code Playgroud)

如果变量是针对某个特定阶段的,请将其移到阶段下:

stages:
- stage: Build
  variables:
    variable1: xxx
    variable2: xxx
  jobs:
  - job: Build
    pool:
      vmImage: $(buildVmImage)
    steps:
    - checkout: self
      submodules: recursive
Run Code Online (Sandbox Code Playgroud)

如果变量用于一项工作,请将它们移至特定工作:

stages:
- stage: Build
  jobs:
  - job: Build
    pool:
      vmImage: $(buildVmImage)
    variables:
      variable1: xxx
      variable2: xxx
    steps:
    - checkout: self
      submodules: recursive
Run Code Online (Sandbox Code Playgroud)

jobs元素不支持variables,而 job/stage 支持它。将变量移动到正确的范围可以解决这个问题。