【问题标题】:Azure pipelines encountered error(s) while parsing pipeline YAML (Unique Job Name)Azure 管道在解析管道 YAML(唯一作业名称)时遇到错误
【发布时间】:2020-08-12 07:45:52
【问题描述】:

在我的 azure devops 项目中,我使用模板创建了一个管道。这是我的构建管道的主要 yaml 文件

name: Test-$(Date:yyyyMMdd)$(Rev:.r)

resources:
  repositories:
    - repository: api
      type: git
      name: porject/api
      ref: master
    - repository: front
      type: git
      name: project/front
      ref: master
    - repository: strapi
      type: git
      name: project/strapi
      ref: master

trigger:
  branches:
    include:
      - master

pool:
  vmImage: 'ubuntu-latest'
  workspace:
    clean: all

variables:
- name: workingDir
  value: project
- name: tfVersion
  value: 0.12.29
- name: backendServiceGCP
  value: test
- name: backendGCPBucketName
  value: test
- name: tfpath
  value: test
- name: env
  value: dev

stages:
- stage: Terraform  
  jobs:
  - job: Build
    displayName: Build Terraform Infra
    steps:
      # Set and Export env var for api version to deploy
      - template: templates/fetch-tag.yml
        parameters:
          repo: 'api'
          envVar: TERRAFORM_API_TAG

      # Set and Export env var for front version to deploy
      - template: templates/fetch-tag.yml
        parameters:
          repo: 'front'
          envVar: TERRAFORM_FRONT_TAG

      # Set and Export env var for strapi version to deploy
      - template: templates/fetch-tag.yml
        parameters:
          repo: 'strapi'
          envVar: TERRAFORM_STRAPI_TAG

      # Init Terraform providers
      - template: templates/tf-init.yml
        parameters:
          backendServiceGCP: '$(backendServiceGCP)'
          backendGCPBucketName: '$(backendGCPBucketName)'
          workingDir: '$(workingDir)'
          variableFilePath: $(buildSubscription)-common.tfvars

      # Plan Terraform Infra to Deploy
      - template: templates/tf-plan.yml
        parameters:
          backendServiceGCP: '$(backendServiceGCP)'
          workingDir: '$(workingDir)'
          variableFilePath: $(buildSubscription)-common.tfvars

      # Publish Public Artifact with Terraform ressources to deploy
      - template: templates/publish-artifact.yml
        parameters:
          tfpath: '$(tfpath)'

当我尝试运行管道时,出现以下错误:

Encountered error(s) while parsing pipeline YAML:
Job Build: The step name version appears more than once. Step names must be unique within a job.
Job Build: The step name version appears more than once. Step names must be unique within a job.
Job Build: The step name version appears more than once. Step names must be unique within a job.
Job Build: The step name version appears more than once. Step names must be unique within a job.
Job Build: The step name version appears more than once. Step names must be unique within a job.

我真的不明白为什么。 这是我在名为publish-artifact.yml 的管道中使用的模板示例:

parameters:
  tfPath: ''

steps:
- task: CopyFiles@2
  inputs:
    sourceFolder: ${{ parameters.tfpath }}
    contents: |
      tfplan
      **/*.tf
      **/*.tfvars
      **/*.json
      !**/.terraform
      **/*.sh
    targetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: $(Build.ArtifactStagingDirectory)
    artifactName: tf

我的工作名称有什么问题?

【问题讨论】:

  • 我认为您必须为主要 yml 的每个步骤命名

标签: azure azure-devops azure-pipelines devops build-pipeline


【解决方案1】:

Azure Devops 支持将一个步骤的输出变量作为下一步的输入传递。见this ticket

我们可以这样命名一个步骤:

  steps:
  - script: echo test
    name: ScriptName

  - task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Write-Host '##vso[task.setvariable variable=xxx;isOutput=true;]xxx'
  name: PSName

name 必须是唯一的,以便我们可以使用格式 $(referencename.variablename) 从特定步骤访问输出变量。

该错误表明您的模板中的某些步骤具有相同的名称version!这不受支持。 关于为什么会出现这个问题:

1.您多次调用同一个模板,这是您问题的主要原因。

processing the pipeline 时,Devops 会首先扩展模板,因此如果您的fetch-tag 模板有一个名为version 的步骤,则最终消耗的 azure-pipeline.yml 将是:

stages:
- stage: Terraform  
  jobs:
  - job: Build
    displayName: Build Terraform Infra
      steps:
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: |
            Write-Host "Hello World"
        name: version
      ...
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: |
            Write-Host "Hello World"
        name: version
      ...
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: |
            Write-Host "Hello World"
        name: version
      ...

2.您可能还需要检查不同模板中是否存在同名步骤。

我们可以在一个管道中多次调用同一个模板,但不能调用有命名步骤的同一个模板。因为管道将多次扩展模板,最终管道将包含许多具有相同名称的步骤。由于名称应该是唯一的,因此不支持此操作。

解决方案:

1.如果您不需要使用上述输出变量,请删除步骤中的name 元素。

2.或者您可以制作多个fetch-tag.yml 的副本,并将它们命名为 fetch-tag-api.yml、fetch-tag-front.yml 和 fetch-tag-strapi.yml。将这三个文件中的referenceNameversion重命名为version1、version2之类的。然后你可以运行管道:

    steps:
      # Set and Export env var for api version to deploy
      - template: templates/fetch-tag-api.yml
        parameters:
          repo: 'api'
          envVar: TERRAFORM_API_TAG

      # Set and Export env var for front version to deploy
      - template: templates/fetch-tag-front.yml
        parameters:
          repo: 'front'
          envVar: TERRAFORM_FRONT_TAG

      # Set and Export env var for strapi version to deploy
      - template: templates/fetch-tag-strapi.yml
        parameters:
          repo: 'strapi'
          envVar: TERRAFORM_STRAPI_TAG

【讨论】:

  • 确实,我使用动态变量 (repo) 重命名了 referenceName 版本,它运行良好。谢谢您的回答。你拯救了我的一天
猜你喜欢
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 2020-03-21
  • 2020-11-01
  • 2022-01-12
  • 2020-09-15
  • 1970-01-01
相关资源
最近更新 更多