You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置YAML流水线,按Agent可用性优先执行对应系统构建任务?

恢复误删的Microsoft托管Agent
  • 登录Azure DevOps组织,进入目标项目
  • 点击左下角「项目设置」,在左侧「管道」分类下选择「代理池」
  • 若仅删除了「Azure Pipelines」池内的代理:
    • 进入「Azure Pipelines」池,点击顶部「添加代理」按钮
    • 在弹窗中选择「Microsoft托管的代理」,挑选所需虚拟机镜像(如Windows-latest、Ubuntu-latest),设置代理数量后点击「添加」
  • 若误删了整个「Azure Pipelines」代理池:
    • 点击「新建代理池」,类型选择「Microsoft托管」,输入池名称(推荐用默认的「Azure Pipelines」)
    • 配置好池权限后保存,再按上述步骤添加托管代理
实现流水线的Agent选择与任务执行逻辑

根据你的需求,可通过Azure Pipelines YAML实现「优先Windows Agent、不可用时 fallback 到Linux、两者都不可用则等待Windows」的逻辑,同时根据Agent系统执行对应构建任务:

方案1:使用Fallback池自动切换

该方案会优先尝试分配Windows Agent,无可用时自动分配Linux Agent,若两者都不可用则等待Windows Agent:

pool:
  # 指定你的自建代理池名称
  name: '你的自建代理池名称'
  # 优先匹配Windows Agent
  demands:
    - Agent.OS -equals Windows_NT
  # 无Windows Agent时,fallback到Linux Agent
  fallbackPool:
    name: '你的自建代理池名称'
    demands:
      - Agent.OS -equals Linux

jobs:
- job: Build
  displayName: '根据Agent系统执行构建'
  steps:
  # 仅在Windows Agent上执行Windows构建任务
  - task: windowsBuild@1
    displayName: '执行Windows构建'
    condition: eq(variables['Agent.OS'], 'Windows_NT')
  # 仅在Linux Agent上执行Linux构建任务
  - task: linuxBuild@1
    displayName: '执行Linux构建'
    condition: eq(variables['Agent.OS'], 'Linux')

方案2:使用多Job实现严格等待逻辑

若需要更严格的「Linux仅在Windows Agent完全不可用时启动,两者都不可用时持续等待Windows」,可采用双Job结构:

jobs:
- job: WindowsBuild
  displayName: 'Windows构建'
  pool:
    name: '你的自建代理池名称'
    demands:
      - Agent.OS -equals Windows_NT
  # 设置超长超时,确保一直等待Windows Agent(1440分钟=24小时)
  timeoutInMinutes: 1440
  steps:
  - task: windowsBuild@1
    displayName: '执行Windows构建任务'

- job: LinuxBuild
  displayName: 'Linux构建'
  # 依赖Windows构建Job
  dependsOn: WindowsBuild
  # 仅当Windows Job因Agent不可用失败时才启动
  condition: failed('WindowsBuild') and contains(variables['Agent.JobStatus'], 'AgentUnavailable')
  pool:
    name: '你的自建代理池名称'
    demands:
      - Agent.OS -equals Linux
  steps:
  - task: linuxBuild@1
    displayName: '执行Linux构建任务'

注意:将上述代码中的你的自建代理池名称替换为实际的代理池名称,windowsBuild@1、linuxBuild@1替换为你定义的实际任务ID或名称。

内容的提问来源于stack exchange,提问作者Masuma Ismael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 08:55:15