如何通过Azure DevOps REST API获取指定项目已完成任务列表至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
httpRequeststep). - If your query returns lots of tasks, the API will paginate results. Check for a
continuationTokenin the response and make follow-up requests until there's no token left to get all items. - Adjust the fields you pull from
task.fieldsbased on what you need—likeSystem.AssignedToorSystem.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

