I'd like to use a parameter to enable the pipeline to switch between running on dedicated agents or an azure host by switching between
pool: name: Default
and
pool: vmImage: 'ubuntu-latest'
parameters:
- name: custom_agent
displayName: Use Custom Agent
type: boolean
default: true
pool:
${{ if eq(parameters.custom_agent, True) }}:
name: ${{ parameters.agent_pool }}
${{ if eq(parameters.custom_agent, False) }}:
vmImage: 'ubuntu-latest'
I've tried various variations but keep getting the error 'A template expression is not allowed in this context'
Am i missing something?, feels like this shouldn't be that hard.
At present, we can not use this script to help us switch the pool. But we have a work around to help us. We can use the Parameters to select a template at runtime, refer doc. We can set different agent in the template then select the temp at runtime. So on my side, I create a demo to help you know:
Main yaml:
parameters:
- name: custom_agent
displayName: Use Custom Agent
type: boolean
default: true
- name: image
type: string
default: default
resources:
repositories:
- repository: templates
type: git
name: Tech-Talk/template
trigger: none
stages:
- ${{if eq(parameters.custom_agent, True) }}:
- template: temp.yaml@templates
- ${{ if not(eq(parameters.custom_agent, True)) }}:
- template: temp2.yaml@templates
temp.yaml by using self-agent:
stages:
- stage:
pool: 'default'
jobs:
- job: READ
displayName: Reading Parameters
steps:
- powershell: echo "self-agent"
temp2.yaml by using the hosted agent:
stages:
- stage:
jobs:
- job: READ
displayName: Reading Parameters
pool:
# vmImage: windows-latest
vmImage: ubuntu-20.04
steps:
- powershell: echo "self-agent"
Note: You can use the key word 'pool' both in the job and stage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With