如何通过Jenkins Rest API获取Pipeline(阶段)信息
Hey there! Since you already have the Pipeline plugin set up, let's walk through practical, tried-and-true ways to pull stage details using Jenkins' REST API. These methods have worked reliably for me across multiple projects, so they should fit your needs:
1. Use the Built-in Workflow API Endpoint
The Pipeline plugin provides a dedicated, straightforward endpoint for workflow details that includes all stage information. This is the go-to method for most basic use cases.
Endpoint Structure
http://<your-jenkins-url>/job/<job-name>/<build-number>/wfapi/describe
Example with curl
Swap in your actual Jenkins details here:
curl -u your-username:your-api-token "http://jenkins.yourdomain.com/job/MyDeployPipeline/45/wfapi/describe"
What You'll Get
The JSON response includes a stages array with critical details for each stage:
name: The stage's display namestatus: Execution status (e.g.,SUCCESS,FAILED,ABORTED)startTimeMillis/endTimeMillis: Timestamps for stage start and endid: Unique identifier for linking directly to the stage's logs
2. Custom Groovy Script via Script API
If you need more customized data—like filtering stages by status, extracting nested stage details, or aggregating metrics—use Jenkins' Script API to run a Groovy script that digs deeper into the Pipeline execution context.
Step 1: Create the Groovy Script
Save this as fetch-stages.groovy (adjust the job name and build number to match your setup):
// Target the desired job and build def job = Jenkins.instance.getItemByFullName("MyDeployPipeline") def build = job.getBuildByNumber(45) // Access the Pipeline's execution graph def flowGraph = build.getAction(org.jenkinsci.plugins.workflow.job.views.FlowGraphAction) def execution = flowGraph.getExecution() // Extract all Stage nodes from the execution def stageNodes = execution.getCurrentHeads().collectMany { head -> head.getParents().findAll { it instanceof org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode && it.getDisplayName().startsWith('Stage') } } // Print formatted, human-readable stage info stageNodes.each { stage -> println """ Stage Name: ${stage.getDisplayName()} Status: ${stage.getStatus()} Start Time: ${new Date(stage.getStartTimeMillis())} Direct Log URL: ${build.getUrl()}execution/node/${stage.getId()}/log/ """ }
Step 2: Execute via curl
curl -u your-username:your-api-token -d "script=$(cat fetch-stages.groovy)" "http://jenkins.yourdomain.com/scriptText"
Key Notes to Avoid Headaches
- Permissions: Ensure your user account (and its API token) has
Job/Readaccess to the target pipeline. You can generate an API token from your Jenkins user profile page under "API Token". - Version Compatibility: The
wfapiendpoint is stable in Jenkins LTS 2.200+ and Pipeline plugin 2.6+. If you're on an older version, you may need to adjust to legacyflowGraphendpoints. - Performance: When fetching stages for multiple builds, add small delays between API calls to avoid overwhelming your Jenkins instance.
If you're aiming for something specific—like monitoring failed stages, aggregating stage duration across builds, or integrating this data with another tool—feel free to share more details and I can help refine these approaches!
内容的提问来源于stack exchange,提问作者Vineet Kumar

