1

I trigger a build called testAUT with parameter FRACTURE_NO = 15. I trigger the same build with the same parameter.

I want when that happens that the earlier build gets killed. How can I do that using Jenkins? I want to be able to end a build with the same parameter as the current build.

Chris Hansen
  • 7,813
  • 15
  • 81
  • 165

1 Answers1

1

If you're OK with using the Groovy Plugin and Jenkins Rest Api, here are the steps needed to do what you want.

  1. Install the Groovy Plugin.
  2. Go to the job's configure page.
  3. Add the 'fracture_no' build parameter with default value of 125.
  4. Enable 'Execute concurrent builds if necessary' in the General section
  5. Add a 'Execute Groovy script' build step as the first step in the job
  6. Add the following code to the groovy step

def parameterName = "fracture_no";
def jenkinsUrl = System.getenv('JENKINS_URL');
def buildNumber = System.getenv('BUILD_NUMBER').toInteger();
def jobUrl = jenkinsUrl + "job/" + System.getenv('JOB_NAME');
def buildNumberUrl = jobUrl + "/" + buildNumber;
def myParameter = System.getenv(parameterName);

def projectXml = new XmlSlurper().parseText(new URL(jobUrl + "/api/xml").getText());
projectXml.build.each {
  def previousBuildNumber = it.number.text().toInteger();
  if(previousBuildNumber < buildNumber)
  {
    def previousBuildNumberUrl = jobUrl + "/" + previousBuildNumber;
    def jobXml = new XmlSlurper().parseText(new URL(previousBuildNumberUrl + "/api/xml").getText());
    if(jobXml.building.text() == "true")
    {
      jobXml.action.parameter.each {
        if(it.name.text() == parameterName) {
          if(it.value.text() == myParameter) {
            def url = new URL(previousBuildNumberUrl + "/stop");
            def connection = url.openConnection();
            connection.setRequestMethod("POST");
            connection.connect();
            connection.content.text;
            println "Stopping " + previousBuildNumber;
          }
        }
      }
    }
  }
}

  1. Add another build step that takes a long time. If you just want to test, then add the following Windows Batch Command step that sleeps for 100 seconds.
    ping 127.0.0.1 -n 100 > nul
  2. Kick off the job twice using the default parameter value of 125 (or as many times as you want).
  3. Everything but the latest build should be stopped.
Daniel Omoto
  • 2,431
  • 17
  • 15
  • how would you change the code above to check for the build parameter to make sure its not the same as the current job? So if job 1 has a build 1 with build parameter fracture_no=125, the next build with build parameter fracture_no=125 should cancel the first build – Chris Hansen Aug 19 '16 at 05:34
  • It works, but I get the error Caught: java.io.IOException: Server returned HTTP response code: 403 for URL: http://sparkbuilder.corp.adobe.com:8080/job/FractureAUT/197/stop – Chris Hansen Aug 20 '16 at 16:22
  • Usually 403 means that the request was rejected due to permissions/security. If you remove any and all permissions from Jenkins, the above should work. Otherwise, you'll need to pass in the credentials and/or token necessary to perform the administrator (stop) actions during the POST request. – Daniel Omoto Aug 21 '16 at 00:36
  • Probably the easiest way to do that would be to use curl. Are you getting an error regarding a crumb missing? If so, I'd check out this [answer](http://stackoverflow.com/questions/28577551/how-to-disable-a-jenkins-job-via-curl). – Daniel Omoto Aug 21 '16 at 00:49
  • I don't get number 7. What's the purpose there? – Chris Hansen Aug 21 '16 at 23:03
  • #7 was just for testing purposes. Without a build step that takes a while, it would be hard to test it against different parameters to make sure only the jobs running an identical parameter are canceled (i.e. by the time you get done editing parameters the old job will have already finished). I'm assuming you have something already that takes awhile to complete, so you can skip it and just execute your own code to verify. – Daniel Omoto Aug 22 '16 at 00:14