Azure pipelines encountered error(s) while parsing pipeline YAML (Unique Job Name)

对着背影说爱祢 提交于 2021-02-11 12:33:07

问题


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.

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

What am I doing wrong with the job name ?


回答1:


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

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

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.You called the same template several times, this is the main cause of your issue.

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

Solution:

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

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


来源:https://stackoverflow.com/questions/63372045/azure-pipelines-encountered-errors-while-parsing-pipeline-yaml-unique-job-nam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!