2

I'm trying to figure out the syntax for specifying demands for a pool in a yaml template in Azure DevOps using parameters

So I have a template like this:

parameters:
  pool: ''
  demands: ''

jobs:
- job: 'Job1'
  pool:
    name: ${{parameters.pool}}
    demands: ${{parameters.demands}}

And calling it like:

- template: 'shared_pipeline.yml'
  parameters:
    pool: 'poolname'
    demands: >-
      FPGA -equals True
      CI -equals True

I've tried many different combinations like exchanging >- with |-

Changing demands: ${{parameters.demands}} to

demands: |
  - ${{parameters.demands}}

Since the original syntax for multiple demands is:

demands: |
  - FPGA -equals True
  - CI -equals True

But all the time I either get an error saying the syntax is wrong:

job_templates/shared_pipeline.yml (Line: 10, Col: 14): Invalid demand 'FPGA -equals True CI -equals True'. The demand should be in the format '<NAME>' to test existence, and '<NAME> -equals <VALUE>' to test for a specific value. For example, 'VISUALSTUDIO' or 'agent.os -equals Windows_NT'

or:

No agent found in pool pool which satisfies the specified demands:
     FPGA -equals True CI -equals True

And I've triple checked that there is and agent that has these capabilities.

It seems to me like the parameters are not getting.. expanded? correctly, like they're still on one line instead of one line each.

I've tried looking at the Azure DevOps documentation, but there are no examples of this kind of usage. I've tried googling, but couldn't find a solution

DHummel
  • 132
  • 1
  • 12

2 Answers2

3

So I actually gave up on this, but today I encountered the same-ish issue for dependsOn

There is a solution. Instead of calling the template like this:

- template: 'shared_pipeline.yml'
  parameters:
    pool: 'poolname'
    demands: >-
      FPGA -equals True
      CI -equals True

You call it like this:

- template: 'shared_pipeline.yml'
  parameters:
    pool: 'poolname'
    demands: ['FPGA -equals True', 'CI -equals True']

This is not documented anywhere in the documentation for Azure Pipelines.

Also the template should be updated to:

parameters:
  pool: ''
  demands: []

jobs:
- job: 'Job1'
  pool:
    name: ${{parameters.pool}}
    demands: ${{parameters.demands}}

To handle the case where demands is not given as an input argument. demands can be empty, but it should be an empty array and not an empty string. The same goes for dependsOn

DHummel
  • 132
  • 1
  • 12
0

Parameter can represent simple one-line string, I don't think this would be easy(or possible?) to make it represent two-line string.

Also, it's hard to prove that your parameter can be expanded into correct yaml format. The yaml pipeline can't run if your second demand doesn't have the same indentation as first demand. Instead I recommend like reconfigure your structure in this way:

#shared_pipeline.yml
parameters:
  pool: ''
  demand1: ''
  demand2: ''

jobs:
- job: 'Job1'
  pool:
    name: ${{parameters.pool}}
    demands:
    - ${{parameters.demand1}}
    - ${{parameters.demand2}}
  steps:
    - script: echo Hello, world!
      displayName: 'Test'

And:

#azure-pipelines.yml
jobs:
- template: 'shared_pipeline.yml'
  parameters:
    pool: 'default'
    demand1: 'FPGA -equals True'
    demand2: 'CI -equals True'

This would work well and meet most of your needs if you can confirm you've set the capabilities:

enter image description here

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • The reason I was looking for this way to do it, is because `${{parameters.demands}}` not always contains a fixed amount of demands. The template is used by more than one pipeline – DHummel Aug 14 '20 at 06:35
  • I understand your meanings, however the Devops yaml pipeline doesn't support this behavior. For multiple demands, the `-` is **necessary** in the yaml syntax! If you put two demands into one parameter, the pipeline will think these two demands as one demand or even throw format error. So if you need to use two demands to specify agent, you have to use two `-` in template file. That's what my workaround above shows. – LoLance Aug 14 '20 at 09:37
  • Yaml schema in azure devops doesn't support two-line string which contains two `-`. It's different from [this one](https://stackoverflow.com/questions/58823445/multiline-string-in-azure-pipelines), your scenario is much more complex with `-`. – LoLance Aug 14 '20 at 09:39
  • Thank you for your help. Ultimately we had to make the agent capabilities a lot more specific, so we always pick the correct one and thus can specify just _one_ `demands`. – DHummel Aug 17 '20 at 13:07
  • @Dakimaz Glad to know you've resolved this issue yourself, you can consider [adding it as answer](https://stackoverflow.com/help/self-answer) and [accept it](https://meta.stackexchange.com/a/5235/515442). So it would be helpful for other members who get the same issue to find the solution easily. Have a nice day:) – LoLance Aug 18 '20 at 09:30