Home > Software engineering >  Sharing templates across repositories in Azure devops
Sharing templates across repositories in Azure devops

Time:10-25

I have a Template repository specifically created to have all the template files. For eg: (1)analysis_stage_template:

parameters:
- name: yesNo # name of the parameter; required
  type: boolean # data type of the parameter; required
  default: false
- name: firstValue # name of the parameter; required
  type: number # data type of the parameter; required
  default: 5
- name: secondValue # name of the parameter; required
  type: number # data type of the parameter; required
  default: 10

jobs:
  job: FirstStageOfAnalysis
  steps:
    script: |
      echo we are in the first stage of analysis
      echo value of the parameter passed for the first stage is : ${{parameters.firstValue}}
  
  job: SecondStageOfAnalysis
  steps:
    script: |
      echo we are in the second stage of analysis
      echo value of the parameter passed for the first stage is : ${{parameters.secondValue}}

(2)build_stage_template:

parameters:
- name: firstValue # name of the parameter; required
  type: number # data type of the parameter; required
  default: 5
- name: secondValue # name of the parameter; required
  type: number # data type of the parameter; required
  default: 10

jobs:
  job: FirstStageOfBuild
  steps:
    script: |
      echo we are in the first stage of build
      echo value of the parameter passed for the first stage is : ${{parameters.firstValue}}
  
  job: SecondStageOfBuild
  steps:
    script: |
      echo we are in the second stage of build
      echo value of the parameter passed for the first stage is : ${{parameters.secondValue}}

I have a main project repository, from where I want to call these templates and use them. The pipeline file is as shown:

trigger: none

resources:
 repositories:
   - repository: templates
     name: Software Development/TemplateRepository
     type: git

stages:
  - stage: Analysis
    jobs:
      - template: analysis_stage_template.yml@templates
        parameters:
          firstValue : 1
          secondValue : 2
    
  - stage: Build
    jobs:
      - template: build_stage_template.yml@templates
        parameters:
          firstValue : 1
          secondValue : 2

But when I run this manually, I get an error saying :

Error message

What am I doing wrong here? Any suggestions?

CodePudding user response:

The cause of the issue is that the YAML format has issues in template files.

Here are two points:

1.When you define the job in YAML, you need to follow the following the format:

jobs:
- job: MyJob
  steps:
  - script: echo My first job

In your case, you missed the - before the job:.

2.When you define the task in YAML, you need to add - before the task name.

For example: - script: echo My first job

Refer to the following YAML sample:

analysis_stage_template

parameters:
- name: yesNo # name of the parameter; required
  type: boolean # data type of the parameter; required
  default: false
- name: firstValue # name of the parameter; required
  type: number # data type of the parameter; required
  default: 5
- name: secondValue # name of the parameter; required
  type: number # data type of the parameter; required
  default: 10



jobs:
 - job: FirstStageOfAnalysis
   steps:
    - script: |
       echo we are in the first stage of analysis
       echo value of the parameter passed for the first stage is : ${{parameters.firstValue}}
  
 - job: SecondStageOfAnalysis
   steps:
    - script: |
       echo we are in the second stage of analysis
       echo value of the parameter passed for the first stage is : ${{parameters.secondValue}}

build_stage_template:

parameters:
- name: firstValue # name of the parameter; required
  type: number # data type of the parameter; required
  default: 5
- name: secondValue # name of the parameter; required
  type: number # data type of the parameter; required
  default: 10

jobs:
 - job: FirstStageOfBuild
   steps:
   - script: |
      echo we are in the first stage of build
      echo value of the parameter passed for the first stage is : ${{parameters.firstValue}}
  
 - job: SecondStageOfBuild
   steps:
   - script: |
      echo we are in the second stage of build
      echo value of the parameter passed for the first stage is : ${{parameters.secondValue}}

Here is the doc about YAML Schema.

  • Related