Determine if given job is currently running using Hudson/Jenkins API

前端 未结 5 935
忘掉有多难
忘掉有多难 2020-12-29 05:24

Is there an API to determine whether a given job is currently running or not?

Ideally, I\'d also like to be able to determine its estimated % complete and get the de

相关标签:
5条回答
  • 2020-12-29 05:56

    As gareth_bowles and Sagar said, using the Jenkins API is the way to know. If you put the depth to 1, you will see what you're looking for:

    http://host/job/project/lastBuild/api/xml?depth=1
    

    You will see there's a <building> tag to tell if that build is running

    ...
    <build>
      <action>
        <cause>
            <shortDescription>Started by user Zageyiff</shortDescription>
            <userId>Zageyiff</userId>
            <userName>Zageyiff</userName>
        </cause>
      </action>
      <building>true</building>
      <duration>0</duration>
      <estimatedDuration>-1</estimatedDuration>
      <fullDisplayName>Project #12</fullDisplayName>
      <id>2012-08-24_08-58-45</id>
      <keepLog>false</keepLog>
      <number>12</number>
      <timestamp>123456789</timestamp>
      <url>
            http://host/job/project/12
      </url>
      <builtOn>master</builtOn>
      <changeSet/>
      <mavenVersionUsed>3.0.3</mavenVersionUsed>
    </build>
    ...
    
    0 讨论(0)
  • 2020-12-29 05:56

    I'm using the Groovy plug-in, and run the following snippet as system:

    import hudson.model.*
    def version = build.buildVariableResolver.resolve("VERSION")
    println "VERSION=$version"
    def nextJobName = 'MY_NEXT_JOB'
    def nextJob = Hudson.instance.getItem(nextJobName)
    def running = nextJob.lastBuild.building
    if (running) {
       println "${nextJobName} is already running. Not launching"
    } else {
       println "${nextJobName} is not running. Launching..."
       def params = [
          new StringParameterValue('VERSION', version)
       ]
       nextJob.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params))
    }
    

    It works like a charm.

    0 讨论(0)
  • 2020-12-29 06:03

    As stated on the /api page of your build (chapter "Accessing Progressive Console Output"), you can poll the console output with a GET request by calling <url-to-job>/lastBuild/logText/progressiveText. To quote the API doc:

    If the response also contains the X-More-Data: true header, the server is indicating that the build is in progress

    And there you go. You can test this behaviour by simply calling the respective URL in your browser and then inspecting the response headers with your browser's developer tools (usually accessed by pressing F12). In Firefox, the respective tab is called "network analysis" (assuming my translation is correct, my browser is not set to English). In Chrome, navigate to the "Network" tab.

    This answer is based on Jenkins version 2.176.3.

    0 讨论(0)
  • 2020-12-29 06:06

    If you're comfortable with digging through the Jenkins Java API, you could write a system Groovy script to get this data. The Job class is the place to start.

    0 讨论(0)
  • 2020-12-29 06:14

    If you go to your job's page, and add "api" to the end of the URL, you'll get information on using the API.

    http://yourjenkins/job/job_name/api
    

    More information on using the Jenkins API:

    https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
    
    0 讨论(0)
提交回复
热议问题