我想使用一个参数来使管道能够在专用代理或天蓝色主机上运行之间进行切换
池: 名称:默认
和
池: vmImage:'ubuntu-最新'
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'
我尝试了各种变体,但不断收到错误“此上下文中不允许使用模板表达式”
我错过了什么吗?感觉这不应该那么难。
最佳答案
目前我们还不能使用这个脚本来帮助我们切换池。但我们有一个解决办法可以帮助我们。我们可以使用参数在运行时选择模板,引用doc 。我们可以在模板中设置不同的代理,然后在运行时选择温度。 因此,我这边创建了一个演示来帮助您了解:
主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
使用 self 代理的temp.yaml:
stages:
- stage:
pool: 'default'
jobs:
- job: READ
displayName: Reading Parameters
steps:
- powershell: echo "self-agent"
使用托管代理的 temp2.yaml:
stages:
- stage:
jobs:
- job: READ
displayName: Reading Parameters
pool:
# vmImage: windows-latest
vmImage: ubuntu-20.04
steps:
- powershell: echo "self-agent"
注意:您可以在作业和阶段中使用关键字“pool”。
关于azure-devops - 按参数改变 azure devops 管道代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66699209/