4
votes

I am trying to execute an Azure Devops Job on a specific pool based on a condition. The goal is to switch between self-hosted agent and microsoft agent. Here is the configuration:

parameters:
  custom_agent: true

jobs:
  - job: Test
    displayName: Test job
  - ${{ if eq(parameters.custom_agent, true) }}:
    - pool:
      name: mypool
      demands:
        - agent.os -equals Linux
  - ${{ if eq(parameters.custom_agent, false) }}:
    - pool:
        vmImage: 'ubuntu-latest'
    steps:
      - task: npmAuthenticate@0

enter image description here

Any ideas ?

3
Hi, is there an update on this question? If the answer could give you some help, we could accept it as answer. In this case, others could directly find the useful solution. If not, please feel free to share your question here.Vito Liu

3 Answers

2
votes

Another apporach to conditionally select pools if you use non-vm pools:

variables:
- ${{ if eq(parameters.custom_agent, true) }}:
  - name: testJobPool
    value: mypool
- ${{ if eq(parameters.custom_agent, false) }}:
  - name: testJobPool
    value: mypool_second

jobs:
- job: Test
  displayName: Test job
  pool:
    name: $(testJobPool)
  steps:
    - task: npmAuthenticate@0

This has proved working.

1
votes

We can specify conditions under which a step, job, or stage will run. We can configure the jobs in the pipeline with different condition entries, and set demands based on those conditions.

A skeleton version looks like this:

parameters:
- name: custom_agent
  displayName: Pool Image
  type: boolean
  default: True

jobs:
  - job: selfhostedagent
    condition: eq(${{ parameters.custom_agent }}, True)
    displayName: 'self_hosted agent'
    pool:
      name: Default
      demands:
        - Agent.Name -equals WS-VITOL-01
    steps:
      - script: echo self_hosted agent

  - job: hostedagent
    condition: eq(${{ parameters.custom_agent }}, False)
    displayName: 'hosted agent'
    pool:
      vmImage: 'ubuntu-latest'
      
    steps:
      - script: echo hosted agent

Update1

In addition, we can configure task template, then use the template in the steps.

Result:

enter image description here

-1
votes

It looks like pool is not a valid property of a job type

Try switching your job to a deployment type:

jobs:

  - deployment: Test
  - ${{ if eq(parameters.custom_agent, true) }}:
     pool:
      name: mypool
      demands:
        agent.os -equals Linux
    strategy:
        runOnce:    
          deploy:
            steps: