Azure管道在解析管道YAML(唯一作业名称)时遇到错误 [英] Azure pipelines encountered error(s) while parsing pipeline YAML (Unique Job Name)

查看:51
本文介绍了Azure管道在解析管道YAML(唯一作业名称)时遇到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my azure devops project, I have created a pipeline using template. This is the main yaml files for my build pipeline

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)'

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

When I am trying to run the pipeline I have the following error:

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 的管道中使用的模板的示例:

I don't really understand why. This is an example of templates I am using in the pipeline named 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

职位名称在做什么?

推荐答案

Azure Devops支持将一步的输出变量作为下一步的输入传递.请参阅此票证:

Azure Devops supports passing output variable from one step as next step's inputs. See this ticket:

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

We can name a step like this:

  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

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

The name must be unique so that we can use format $(referencename.variablename) to access output variable from specific step.

该错误表明模板中的某些步骤具有相同的名称 version !并且不支持此功能.关于发生此问题的原因:

The error indicates that some steps in your templates have the same name version! And this is not supported. About why this issue occurs:

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

1.You called the same template several times, this is the main cause of your issue.

处理管道,因此,如果您的 fetch-tag 模板有一个名为 version 的步骤,则最终消耗的azure-pipeline.yml将是:

Devops expand templates first when processing the pipeline, so if your fetch-tag template has one step named version, the final expended azure-pipeline.yml would be:

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.您可能还需要检查不同模板中是否存在具有相同名称的步骤.

2.You may also need to check if there're steps with same name in your different templates.

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

We can call same template more than once in one pipeline, but we can't call same templates in which there's named step. Cause the pipeline will expand the templates more than once and the final pipeline would contain many steps with same name. And this is not supported since the name should be unique.

解决方案:

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

1.Remove the name element of your steps if you don't need to use output variables mentioned above.

2.或者,您可以制作 fetch-tag.yml 的多个副本,并将其命名为fetch-tag-api.yml,fetch-tag-front.yml和fetch-tag-strapi.yml.将这三个文件中的referenceName version 重命名为version1,version2或其他名称.然后,您可以使用以下命令运行管道:

2.Or you can make several copies of fetch-tag.yml and name them fetch-tag-api.yml, fetch-tag-front.yml and fetch-tag-strapi.yml. Rename the referenceName version in these three files to version1, version2 or what. Then you can run the pipeline with:

    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

这篇关于Azure管道在解析管道YAML(唯一作业名称)时遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆