From Jenkins, how do I get a list of the currently running jobs in JSON?

前端 未结 6 1810
执笔经年
执笔经年 2020-12-07 17:41

I can find out just about everything about my Jenkins server via the Remote API, but not the list of currently running jobs.

This,

http://my-jenkins         


        
相关标签:
6条回答
  • 2020-12-07 18:21

    I have a view defined using View Job Filters Plugin that filters just currently running jobs, then you can use /api/json on the view page to see just the jobs that are running. I also have one for aborted, unstable, etc.

    UPDATE

    Select Edit ViewJob FiltersAdd Job Filter   ▼Build Statuses Filter
      Build Statuses:   ☑ Currently Building
      Match Type:   Exclude Unmatched - ...

    0 讨论(0)
  • 2020-12-07 18:23

    Bit of a hack but I think you can infer what jobs are currently running by looking at the color key in the job objects when you do a GET at /jenkins/api/json?pretty=true. If the 'ball' icon for a given job in Jenkins is animated, we know it's running.

    Have a look at the array of job objects in the JSON response:

    {
     ...
      "jobs" : [
        {
          "name" : "Test Job 1",
          "url" : "http://localhost:8000/jenkins/job/Test%20Job%201/",
          "color" : "blue"
        },
        {
          "name" : "Test Job 2",
          "url" : "http://localhost:8000/jenkins/job/Test%20Job%202/",
          "color" : "blue_anime"
        }
      ...
    }
    

    In this case "color" : "blue_anime" indicates that the job is currently running, and "color" : "blue" indicates that the job is not running.

    Hope this helps.

    0 讨论(0)
  • You can do this with the jenkins tree api, using an endpoint like this:

    http://<host>/api/json?tree=jobs[name,lastBuild[building,timestamp]]
    

    You can see what attributes from lastBuild you can use if you access <job-endpoint>/lastBuild/api/json.

    0 讨论(0)
  • 2020-12-07 18:29

    Marshal the output and filter for "building: true" from the following call to json api on a job with tree to filter out the extraneous stuff (hope this helps):

    http://jenkins.<myCompany>.com/job/<myJob>/api/json?pretty=true&depth=2&tree=builds[builtOn,changeSet,duration,timestamp,id,building,actions[causes[userId]]]
    

    will give you something like:

    {
      "builds" : [
    {
      "actions" : [
        {
    
        },
        {
          "causes" : [
            {
              "userId" : "cheeseinvert"
            }
          ]
        },
        {
    
        },
        {
    
        },
        {
    
        },
        {
    
        }
      ],
      "building" : true,
      "duration" : 0,
      "id" : "2013-05-07_13-20-49",
      "timestamp" : 1367958049745,
      "builtOn" : "serverA",
      "changeSet" : {
    
      }
    }, ...
    
    0 讨论(0)
  • 2020-12-07 18:37

    There is often confusion between jobs and builds in Jenkins, especially since jobs are often referred to as 'build jobs'.

    • Jobs (or 'build jobs' or 'projects') contain configuration that describes what to run and how to run it.
    • Builds are executions of a job. A build contains information about the start and end time, the status, logging, etc.

    See https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project for more information.

    If you want the jobs that are currently building (i.e. have one or more running builds), the fastest way is to use the REST API with XPath to filter on colors that end with _anime, like this:

    http://jenkins.example.com/api/xml?tree=jobs[name,url,color]&xpath=/hudson/job[ends-with(color/text(),%22_anime%22)]&wrapper=jobs
    

    will give you something like:

    <jobs>
      <job>
        <name>PRE_DB</name>
        <url>http://jenkins.example.com/job/my_first_job/</url>
        <color>blue_anime</color>
      </job>
      <job>
        <name>SDD_Seller_Dashboard</name>
        <url>http://jenkins.example.com/job/my_second_job/</url>
        <color>blue_anime</color>
      </job>
    </jobs>
    

    Jenkins uses the color field to indicate the status of the job, where the _anime suffix indicates that the job is currently building.

    Unfortunately, this won't give you any information on the actual running build. Multiple instances of the job maybe running at the same time, and the running build is not always the last one started.

    If you want to list all the running builds, you can also use the REST API to get a fast answer, like this:

    http://jenkins.example.com/computer/api/xml?tree=computer[executors[currentExecutable[url]],oneOffExecutors[currentExecutable[url]]]&xpath=//url&wrapper=builds
    

    Will give you something like:

    <builds>
      <url>http://jenkins.example.com/job/my_first_job/1412/</url>
      <url>http://jenkins.example.com/job/my_first_job/1414/</url>
      <url>http://jenkins.example.com/job/my_second_job/13126/</url>
    </builds>
    

    Here you see a list off all the currently running builds. You will need to parse the URL to separate the job name from the build number. Notice how my_first_job has two builds that are currently running.

    0 讨论(0)
  • 2020-12-07 18:40

    I had a similar problem where some pipeline builds get stuck in the building state after I restart jenkins (piepline jobs are supposed to be durable and resume but most of the time they get stuck indefinitely).

    These builds do not use an executor so the only way to find them is to open every job.

    All of the other answers seem to work when the project is considered building, i.e.: the last build is building. But they ignore past builds still building.

    The following query works for me and gives me all the currently running builds, i.e.: they do not have a result.

    http://localhost:8080/api/xml?tree=jobs[name,builds[fullDisplayName,id,number,timestamp,duration,result]]&xpath=/hudson/job/build[count(result)=0]&wrapper=builds

    0 讨论(0)
提交回复
热议问题