【问题标题】:How to select a specific build agent from azure pipelines to run your build on?如何从 azure 管道中选择特定的构建代理来运行构建?
【发布时间】:2020-09-09 00:48:13
【问题描述】:

所以我试图想出一种方法来允许在构建管道中轻松选择给定代理,仅用于调试目的。到目前为止,我有以下 sn-p。两者都可以在没有 if sn-ps 环绕的情况下工作,但我试图根据设置的参数或设置的变量来做一个或另一个,这样如果它处于调试模式,它将选择和代理,如果不是那么它只会使用池来选择一个代理来运行构建。不过到目前为止还没有运气。

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 }}

【问题讨论】:

    标签: azure deployment yaml azure-pipelines


    【解决方案1】:

    因此,在与上面的 kevin-lu-msft 进行了快速聊天后,我将使用此解决方案来处理选择池中的特定代理。

    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"
    

    【讨论】:

      【解决方案2】:

      我测试了您的 YAML 示例并进行了一些修改。您可以尝试在舞台下设置“表达式”并检查它是否可以按预期工作。

      这是一个例子,你可以参考。

      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!"
      

      注意:我使用的是 Microsoft 托管的代理,因此我使用的是 vmImage 字段。

      您可以根据需要specify specific self-hosted agentsname字段)。

      希望这会有所帮助。

      【讨论】:

      • 这看起来很有希望,我早上起床时会看看。感谢您查看此内容。如果我不能让它工作,会回复你,但建议非常明确,谢谢。
      • 如果您有任何更新,请随时告诉我。我会继续帮助你的。
      • 非常感谢上面的解决方案不起作用,但我有一些东西可以做,我将把它发布在这个线程的答案部分。但如果没有您的建议,我将找不到此解决方案,再次感谢。
      • 嗨@ChrisMarshall 也许您可以与我们分享解决方案。然后你可以accept your answer。在这种情况下,其他人可以直接找到有用的解决方案。
      猜你喜欢
      • 2022-08-14
      • 2020-06-05
      • 2021-08-03
      • 2020-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2020-12-04
      • 2023-03-09
      相关资源
      最近更新 更多