azure-devops - 按参数改变 azure devops 管道代理

标签 azure-devops azure-pipelines-build-task

我想使用一个参数来使管道能够在专用代理或天蓝色主机上运行之间进行切换

池: 名称:默认

池: 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/

相关文章:

azure - 在Azure应用程序服务中切换docker镜像

azure-devops - 在 Devops 部署期间添加 Azure Web 应用服务自定义域

azure - Azure 管道上的 CI 触发器

docker - VSTS docker撰写任务

azure-pipelines - 作为 Visual Studio Team Services 构建任务的一部分,如何在 PowerShell 脚本中执行 SQL?

azure-devops - 如果 yaml 中的前一阶段部署条件设置为 false,则阻止部署

docker - Azure Devops : Docker Build task image name starts with ***/

azure-devops - Visual Studio 团队服务部署/构建证书错误

node.js - NG Build 在 Azure Pipelines 中失败,出现 NPM 错误 134

node.js - Azure DevOps 扩展中的 Node Cipheriv 警告 - 发布任务