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

如何通过Azure DevOps REST API获取指定项目已完成任务列表至Jenkinsfile?

How to Fetch "Done" Tasks from Azure DevOps Saved Query via REST API into Jenkinsfile

Got it, let's break this down step by step to get your completed tasks pulled into your Jenkins pipeline smoothly:

1. Grab Your Saved ADO Query's ID

First, you need the unique GUID of that "Done" tasks query you built in Azure DevOps. Here's how to find it:

  • Open your query in ADO
  • Look at the browser URL—you'll see a parameter like queryId=xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  • Copy that long GUID string; that's your query ID for the API call.

2. Use ADO REST API to Run the Saved Query

The API endpoint to execute your saved query is:

GET https://dev.azure.com/{your-org-name}/{your-project-name}/_apis/wit/queries/{query-id}?api-version=7.1-preview.2

This returns both the query definition and its matching work items. If you only care about the work items (not query metadata), you can also extract the WIQL from the saved query and run it directly with this POST endpoint:

POST https://dev.azure.com/{your-org-name}/{your-project-name}/_apis/wit/wiql?api-version=7.1-preview.2

With a JSON body like:

{
  "query": "SELECT [System.Id], [System.Title], [System.State] FROM WorkItems WHERE [System.State] = 'Done' AND [System.TeamProject] = '{your-project-name}'"
}

Sticking with the saved query is cleaner though—you can update the filter in ADO later without touching your Jenkinsfile.

Authentication Note

You'll need a Personal Access Token (PAT) from ADO with at least Work Items (Read) permission. Store this PAT in Jenkins as a Secret Text credential (never hardcode it!).

3. Implement This in Your Jenkinsfile

Here's a practical snippet that fetches the tasks, parses the results, and lets you use them in your pipeline:

pipeline {
  agent any
  environment {
    // Pull PAT from Jenkins credentials store
    ADO_PAT = credentials('ado-pat-secret') // Use your credential's ID here
    ORGANIZATION = 'your-org-name'
    PROJECT = 'your-project-name'
    QUERY_ID = 'your-saved-query-guid'
  }
  stages {
    stage('Fetch Done Tasks from ADO') {
      steps {
        script {
          // Call ADO REST API
          def apiUrl = "https://dev.azure.com/${ORGANIZATION}/${PROJECT}/_apis/wit/queries/${QUERY_ID}?api-version=7.1-preview.2"
          def response = httpRequest(
            url: apiUrl,
            authentication: 'ado-pat-secret',
            httpMode: 'GET',
            contentType: 'APPLICATION_JSON',
            acceptType: 'APPLICATION_JSON'
          )
          
          // Parse JSON response
          def queryData = new groovy.json.JsonSlurper().parseText(response.content)
          def doneTasks = queryData.workItems // This is your list of completed tasks
          
          // Process the tasks (customize this part to fit your pipeline needs)
          echo "Found ${doneTasks.size()} completed tasks:"
          doneTasks.each { task ->
            echo "Task ID: ${task.id} | Title: ${task.fields['System.Title']} | State: ${task.fields['System.State']}"
            // Example: Use task IDs to tag builds, generate release notes, etc.
          }
        }
      }
    }
  }
}

Key Details:

  • Make sure Jenkins has the HTTP Request Plugin installed (it powers the httpRequest step).
  • If your query returns lots of tasks, the API will paginate results. Check for a continuationToken in the response and make follow-up requests until there's no token left to get all items.
  • Adjust the fields you pull from task.fields based on what you need—like System.AssignedTo or System.CompletedDate.

Troubleshooting Quick Tips

  • Double-check your PAT permissions: Ensure it has access to your target project and the Work Items read scope.
  • Verify the query ID is correct—typos here will throw a 404 error.
  • Test the API call first in Postman or curl to confirm it returns the right tasks before adding it to Jenkins.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 00:02:30