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

如何在Azure-DevOps Pipeline的YAML文件中添加查询?

Azure DevOps查询写入YAML流水线的实现方法

支持将Azure DevOps查询整合到YAML流水线中,不过YAML本身无法直接定义查询结构,而是通过任务调用CLI或REST API来执行查询,具体实现方法如下:

方法一:使用Azure DevOps CLI任务

通过AzureCLI任务调用az boards query命令,既可以执行已保存的查询,也可以直接传入WIQL语句:

steps:
- task: AzureCLI@2
  displayName: '执行Azure DevOps工作项查询'
  inputs:
    azureSubscription: '你的Azure服务连接名称'
    scriptType: 'pscore'
    scriptLocation: 'inlineScript'
    inlineScript: |
      # 执行已保存的查询(替换为你的查询GUID和项目名)
      az boards query --id "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" --project "你的项目名称" --output table
      
      # 或者直接执行WIQL语句(按需修改查询逻辑)
      az boards query --wiql "SELECT [System.Id], [System.Title], [System.State] FROM WorkItems WHERE [System.State] != 'Closed'" --project "你的项目名称" --output json

方法二:使用PowerShell任务调用REST API

如果需要更灵活的结果处理,可以通过PowerShell调用Azure DevOps的REST API来执行查询:

steps:
- task: PowerShell@2
  displayName: '通过REST API获取查询结果'
  inputs:
    targetType: 'inline'
    script: |
      $orgUrl = "https://dev.azure.com/你的组织名称"
      $projectName = "你的项目名称"
      $queryId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # 已保存查询的GUID
      
      # 获取流水线内置的访问令牌
      $authToken = $env:SYSTEM_ACCESSTOKEN
      
      # 调用查询API
      $queryResult = Invoke-RestMethod -Uri "$orgUrl/$projectName/_apis/wit/queries/$queryId?api-version=7.1-preview.2" `
                                       -Headers @{Authorization = "Bearer $authToken"} `
                                       -Method Get
      
      # 输出工作项信息
      Write-Host "查询到的工作项:"
      $queryResult.workItems | ForEach-Object {
          Write-Host "ID: $($_.id) | 标题: $($_.fields.'System.Title') | 状态: $($_.fields.'System.State')"
      }
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)

注意事项

  • 确保流水线使用的服务账号拥有读取工作项和查询的权限
  • 已保存查询的GUID可以在Azure DevOps门户中查询详情页的URL里获取
  • WIQL语句可先在门户的查询编辑器中验证正确性,再复制到YAML中
  • 输出格式可根据需求调整(如--output参数支持table、json、csv等)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 18:45:28