Erik
Erik

Reputation: 3208

azure template not allowing jobs keyword

bare with me as this is my 2nd week really diving into pipelines on Azure but.. I saw an example where I can create a template that has a jobs: section..

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#job-stage-and-step-templates-with-parameters

so I can properly segment things in the template.. however when I goto do this it's complaining

yaml snippet

# File: template_process_folder.yaml

parameters: 
- name: folder_name
  default: YOURFOLDER

jobs:
- job: ProcessFolder
  pool:
    vmImage: ubuntu-latest
  displayName: "Processing Folder"
  steps:
    - bash: |
        blah blah blah

Error

error line X, Col 1... Unexpected value 'jobs'

Anyone have a idea where what I am doing wrong? I've tried stages with it.. just 1 job.. etc.. only thing that works for me is simple steps only in templates

Edit: Could it be how I am calling my template? Is there a way to still loop through these array values but at the job level? I have not seen that in any doc as of now

how I call the template

- stage: Build
  displayName: "Build"
  jobs:
    - job: "Execute_Tests"
      displayName: "Run Tests"
      steps:
        - ${{each p in parameters.mylistoffolders}}:
            - template: template_process_folder.yaml
              parameters:
                folder_name: ${{p}}

I think this is the problem.. and now am removing the template call from the steps area and bumping it up to the jobs area successfully.. but now my error is complaining that job names must be unique.. I figured just pass the folder name but it contains - which are not allowed.. now trying to execute a replace call but unsuccessfully

new template job entry

jobs:
- job: ProcessFolder_${{ replace(${{parameters.folder_name}}, '-', '_') }}

error I get

unclosed function: 'replace'. Located at position 1 within expression replace(${{paramet....

Upvotes: 0

Views: 1823

Answers (3)

lylam
lylam

Reputation: 46

By "moving the template call from the steps area up to the jobs area", you have almost solved the issue yourself. To avoid the issue of unique job name, you just need to remove the name that you assigned to the job. If you need it to display a name, use the suggestion from "Edward Han-MSFT" with displayName attribute.

Upvotes: 1

Edward Han-MSFT
Edward Han-MSFT

Reputation: 3195

Testing in my side and I can see the same error Unclosed function: 'replace'. Located at position 1 within expression: replace(${{parameters.folder_name. It seems that this expression doesn't work here.

As a workaround, you could use keyword displayname so it can resolve error that job names must be unique by using displayName: ProcessFolder_${{parameters.folder_name}}.

BTW, the following yaml is for your reference.

template_process_folder.yml

# File: template_process_folder.yml

parameters: 
- name: folder_name
  type: string
  default: YOURFOLDER

jobs:
- job: 
  pool:
    vmImage: ubuntu-latest
  displayName: ProcessFolder_${{parameters.folder_name}}
  steps:
    - bash: |
        echo ${{ parameters.folder_name }}

template_stage.yml

# File: template_stage.yml

parameters:
- name: mylistoffolders  # defaults for any parameters that aren't specified
  type: object
  default:
  - one
  - two

stages:
- stage: Build
  displayName: "Build"
  jobs:
  - ${{each p in parameters.mylistoffolders}}:
    - template: template_process_folder.yml
      parameters:
        folder_name: ${{p}}

azure-pipelines.yml

pool:
  vmImage: ubuntu-latest

stages:
- template: template_stage.yml
  parameters:
    mylistoffolders:
    - test1-hi
    - test2-bye

Upvotes: 1

Bhargavi Annadevara
Bhargavi Annadevara

Reputation: 5512

It is true that the YAML pipeline syntax is not as straightforward as the Classic Editor experience. :)

But the best way to get started here is to first understand the structure of a pipeline, to know what are the must-have components vs what can be skipped.

As explained in the YAML schema reference document, the hierarchy looks like this:

Pipeline Structure

Note that your YAML definition may not need to include all of them as simple pipelines don't require all of these levels.

You can quickly experiment building a new pipeline with the available quickstart guide to see this in action, and then advance to learning how to configure your YAML pipeline for your specific use-case.

The schema reference article linked above contains an example for every pipeline component accompanied by an example yaml snippet that should help make the usage clearer.

P.S.: Indentation is the one of the most important aspects to be wary of when working with YAML definitions!

Upvotes: 0

Related Questions