【问题标题】:Vary azure devops pipeline agent by parameter按参数改变 azure devops 管道代理
【发布时间】:2021-03-18 21:13:08
【问题描述】:

我想使用一个参数来启用管道在运行在专用代理或 Azure 主机之间的切换

池: 名称:默认

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

我尝试了各种变体,但不断收到错误“此上下文中不允许使用模板表达式”

我错过了什么吗?感觉这不应该那么难。

【问题讨论】:

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


    【解决方案1】:

    目前我们无法使用这个脚本来帮助我们切换池。但是我们有一个解决方法可以帮助我们。我们可以在运行时使用参数来选择模板,参考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
    

    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"   
    

    注意:您可以在作业和阶段中使用关键字“池”。

    【讨论】:

    • 感谢您,我正在探索类似的东西,但感觉通过参数管理池的方法非常冗长。这也意味着虽然实际上只有一个脚本步骤,但它需要在两个文件中维护。这可能是唯一的方法,但我觉得我可能错过了更明显的东西
    【解决方案2】:

    我发现将 pool 包装在 if 块中是可行的

    parameters:
    - name: vmImageName
      type: string
      default: 'ubuntu-latest'
    
    - name: agentPoolName
      type: string
      default: null
    
    jobs:  
      - job: Docker
        ${{ if ne(parameters.agentPoolName, 'null')}}:
          pool:
            name: ${{ parameters.agentPoolName }}
        ${{ if eq(parameters.agentPoolName, 'null')}}:
          pool:
            vmImage: ${{ parameters.vmImageName }}
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 2020-05-19
      • 2021-11-14
      • 2022-08-02
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      相关资源
      最近更新 更多