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

Azure DevOps Pipeline中全新虚拟机测试新版本的方案咨询

Azure DevOps 全新虚拟机测试流程优化方案

这种临时虚拟机隔离测试的配置在很多团队中都有应用,既能保证测试环境的纯净性,又能避免遗留状态影响后续测试。针对你的问题,给出以下实践方案:

一、更优镜像管理方案

相比Azure Marketplace自定义镜像或快照,**Azure Compute Gallery(原Shared Image Gallery)**是更推荐的选择:

  • 支持镜像版本管理,可迭代更新测试环境配置,无需重新创建完整镜像;
  • 支持跨区域共享,适合多地域测试场景;
  • 增量更新机制,减少存储占用和镜像部署时间;
  • 可配置复制策略,确保不同区域的镜像一致性。

二、核心:在新虚拟机上运行测试的实现步骤

要让测试任务定向到新创建的虚拟机,关键是将VM注册为Azure DevOps的临时Agent,并让流水线调度任务到该Agent:

  1. VM创建时注册Agent:
    在VM启动脚本中加入Agent安装和注册逻辑,将Agent加入专门的临时测试Agent Pool,并给Agent打上自定义标签(比如temp-test-vm)。示例Windows启动脚本:
    # 下载Azure DevOps Agent
    Invoke-WebRequest -Uri https://vstsagentpackage.azureedge.net/agent/2.220.5/vsts-agent-win-x64-2.220.5.zip -OutFile agent.zip
    Expand-Archive agent.zip -DestinationPath C:\agent
    
    # 注册到指定Pool,使用PAT认证(提前在Azure DevOps生成PAT并存储为流水线变量)
    C:\agent\config.cmd --unattended `
      --url https://dev.azure.com/你的组织名 `
      --auth pat `
      --token $(TestAgentPAT) `
      --pool 临时测试Agent池 `
      --agent $(hostname) `
      --replace `
      --runAsService `
      --work _work
    
  2. 等待Agent就绪:
    添加流水线任务,循环查询Agent状态,直到Agent显示为online。可使用Azure DevOps CLI实现:
    # 循环查询Agent状态
    agent_status=""
    while [ "$agent_status" != "online" ]; do
      agent_status=$(az devops agent list --pool-id 你的PoolID --agent-name $(hostname) --query "[0].status" -o tsv)
      echo "当前Agent状态:$agent_status,等待中..."
      sleep 30
    done
    
  3. 定向调度测试任务:
    在流水线中创建测试Job,通过demands指定Agent的标签或名称,确保任务跑在新创建的VM上:
    jobs:
    - job: RunTests
      displayName: 在临时VM上运行测试
      pool:
        name: 临时测试Agent池
        demands:
          - Agent.Name -equals $(hostname) # 或者用标签:Agent.TempTestVm -equals true
      steps:
        - script: |
            # 这里放你的测试执行命令,比如运行pytest、JUnit等
            pytest ./tests --junitxml=test-results.xml
          displayName: 执行测试
        - task: PublishTestResults@2
          displayName: 发布测试结果
          inputs:
            testResultsFormat: 'JUnit'
            testResultsFiles: '**/test-results.xml'
          condition: succeededOrFailed() # 不管测试成功失败都发布结果
    

三、完整精简流程

  1. 触发流水线:新版本构建完成后自动触发测试流水线(可通过构建流水线的后续任务或分支策略配置)。
  2. 创建临时VM:用ARM模板或Azure CLI任务,基于Azure Compute Gallery的自定义镜像创建VM,同时通过启动脚本注册Agent到专用Pool。
  3. 等待Agent上线:执行状态查询脚本,确认Agent就绪。
  4. 运行测试:调度测试任务到该Agent,执行测试并发布结果。
  5. 反馈结果:流水线自动同步测试成功/失败状态,可配置邮件、Teams通知。
  6. 清理资源:无论测试结果如何,执行清理步骤:删除VM、注销Agent、释放磁盘等关联资源(可通过Azure CLI或ARM模板删除资源组)。

额外优化建议

  • 使用Azure Spot VM:测试场景对VM可用性要求较低,Spot VM可大幅降低成本。
  • 流水线变量化:将组织名、Pool ID、PAT等配置设为流水线变量,方便维护。
  • 自动注销Agent:在清理步骤中,可远程执行Agent注销命令:C:\agent\config.cmd remove --unattended --token $(TestAgentPAT)(Windows),避免Agent池残留无效Agent。

内容的提问来源于stack exchange,提问作者Thomas Morgan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 23:48:15