1
votes

So im trying to come up with a way to allow an easy selection of a given agent within the build pipeline just for debugging purposes. So far I have the below snippet. both work without the if snippets wrapped around but I was trying to do one or the other based on either params being set or inturn variables being set so that if it was in debug mode it would select and agent and if it wasnt then it would just use the pool to select an agent to run the builds against. So far no luck though.

variables:
  debugMode: 'false'

parameters:
- name: poolOption
  type: string
  default: 'ZupaDeploymentPool'
- name: debugMode
  type: string
  default: 'true'
- name: debugMachine
  type: string
  default: 'ZUPBUILD03'

trigger:
  batch: true
  branches:
    include:
    - master
  paths:
    exclude:
    - README.md

${{ if ne($(debugMode), 'false') }}:
  pool: ${{ parameters.poolOption }} 

${{ if ne($(debugMode), 'true') }}:
  pool:
    name: ${{ parameters.poolOption }}
    demands: 
    - Agent.Name -equals ${{ parameters.debugMachine }}

2

2 Answers

1
votes

I tested your YAML sample and made some modifications. You could try to set the "Expressions" under a stage and check if it could work as expected.

Here is an example, you could refer to it.

trigger:
- master


parameters:
- name: poolOption
  type: string
  default: 'Windows-latest'

- name: debugMode
  type: string
  default: false
  values:
    - true
    - false


- name: debugMachine
  type: string
  default: 'ubuntu-16.04'

stages:
- stage: A
  jobs:
  - job: testjob
    pool:
     ${{ if eq(parameters.debugmode, 'true') }}:
      vmImage: ${{ parameters.poolOption }}

     ${{ if eq(parameters.debugmode, 'false') }}:
      vmImage: ${{ parameters.debugMachine }}


    steps:
      - script : "echo Hello, world!"

Note: I am using Microsoft-hosted agents, so I am using the vmImage field.

You can specify specific self-hosted agents (name field) according to your needs.

Hope this helps.

2
votes

So after having a quick chat with kevin-lu-msft above lead me to this solution for handling selecting a specific agent in the pool.

parameters:
- name: debugMachine
  displayName: 'Run on selected agent:'
  type: string
  default: 'Auto Select From Pool'
  values:
  - 'Auto Select From Pool'
  - 'MACHINE01'
  - 'MACHINE02'
  - 'MACHINE03'
  - 'MACHINE04'
  - 'MACHINE05'

jobs:
-job: build

  # -----------------------------------------------------------------------------------------------
  # This is the pool selection section if a specific debug machine is passed in via the params 
  # It will select that specific one to run the build on. unfortunately azure doesnt let you pass 
  # vars or params to the process string in the demands which would have made this alot cleaner.
  pool:
    name: 'YOUAGENTSPOOLNAME'
    ${{ if ne(parameters.debugMachine, 'Auto Select From Pool') }}:
      ${{ if eq(parameters.debugMachine, 'MACHINE01') }}:
        demands:
        - Agent.Name -equals MACHINE01
      ${{ if eq(parameters.debugMachine, 'MACHINE02') }}:
        demands:
        - Agent.Name -equals MACHINE02
      ${{ if eq(parameters.debugMachine, 'MACHINE03') }}:
        demands:
        - Agent.Name -equals MACHINE03
      ${{ if eq(parameters.debugMachine, 'MACHINE04') }}:
        demands:
        - Agent.Name -equals MACHINE04
      ${{ if eq(parameters.debugMachine, 'MACHINE05') }}:
        demands:
        - Agent.Name -equals MACHINE05

  steps:
    - script: "echo finally it works"