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

如何通过Jenkins Rest API获取Pipeline(阶段)信息

Fetching Jenkins Pipeline Stage Info via REST API

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 name
  • status: Execution status (e.g., SUCCESS, FAILED, ABORTED)
  • startTimeMillis/endTimeMillis: Timestamps for stage start and end
  • id: 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/Read access to the target pipeline. You can generate an API token from your Jenkins user profile page under "API Token".
  • Version Compatibility: The wfapi endpoint 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 legacy flowGraph endpoints.
  • 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:04:15