If you're OK with using the Groovy Plugin and Jenkins Rest Api, here are the steps needed to do what you want.
- Install the Groovy Plugin.
- Go to the job's configure page.
- Add the 'fracture_no' build parameter with default value of 125.
- Enable 'Execute concurrent builds if necessary' in the General section
- Add a 'Execute Groovy script' build step as the first step in the job
- 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;
}
}
}
}
}
}
- 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
- Kick off the job twice using the default parameter value of 125 (or as many times as you want).
- Everything but the latest build should be stopped.