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

Azure DevOps YAML流水线中如何将AZ CLI任务作为测试运行并上报结果

实现方案

两个需求均可通过Azure DevOps YAML原生能力实现,具体配置方法如下:

1. 测试任务失败不中断流水线

  • 给AZ CLI任务添加continueOnError: true属性,就算AZ CLI命令返回非0错误码,流水线也会继续执行后续步骤,不会直接标记整个流水线失败。
  • 你也可以选择在脚本内自行捕获退出码和输出,完全自主控制任务状态,避免任务本身被标记为失败,示例配置如下:
- task: AzureCLI@2
  continueOnError: true # 核心配置:任务异常时继续执行流水线
  inputs:
    azureSubscription: '替换为你的Azure服务连接名称'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      # 执行AZ CLI命令,合并标准错误到输出,同时捕获退出码
      output=$(az acr repository show --name <ACR-name> --image <Image-name>:<Incorrect-version> --query "name" --output tsv 2>&1)
      exit_code=$?
      # 将测试结果存入流水线变量,供后续步骤使用
      echo "##vso[task.setvariable variable=acr_test_output]$output"
      echo "##vso[task.setvariable variable=acr_test_exit_code]$exit_code"
      # 可选:如果不需要任务本身显示失败,主动返回0退出码即可
      # exit 0
  name: ACR镜像存在性测试

2. 测试结果发布到Test Result标签页

你可以将测试结果生成为JUnit格式的XML文件,再使用官方PublishTestResults任务发布,即可在Test Result标签页查看测试执行状态、错误详情等信息,示例配置如下:

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      # 生成JUnit格式的测试结果文件
      cat > acr-test-result.xml << EOF
      <testsuites>
        <testsuite name="ACR业务测试" tests="1" failures="$( [ $(acr_test_exit_code) -eq 0 ] && echo 0 || echo 1 )">
          <testcase classname="镜像校验" name="指定版本镜像存在性检查">
      EOF
      if [ $(acr_test_exit_code) -ne 0 ]; then
        cat >> acr-test-result.xml << EOF
            <failure message="镜像查询失败">$(acr_test_output)</failure>
        EOF
      fi
      cat >> acr-test-result.xml << EOF
          </testcase>
        </testsuite>
      </testsuites>
      EOF

- task: PublishTestResults@2
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: 'acr-test-result.xml'
    testRunTitle: 'ACR镜像校验测试结果'
    failTaskOnFailedTests: false # 设为false则测试失败时该任务也不会报错中断流水线

内容的提问来源于stack exchange,提问作者Tejas Nagchandi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 12:45:02