Skip to content
Karl Heinz Marbaise edited this page Nov 28, 2016 · 6 revisions

If you like to trigger a build and wait until the build has finished, can be accomplished by using the following code:

JenkinsServer js = new JenkinsServer(URI.create("http://localhost:10090/buildserver"), "admin", "admin");
JobWithDetails job = js.getJob("maven-test");
QueueReference queueRef = job.build(true);

System.out.println("Ref:" + queueRef.getQueueItemUrlPart());

job = js.getJob("maven-test");
QueueItem queueItem = js.getQueueItem(queueRef);
while (!queueItem.isCancelled() && job.isInQueue()) {
    System.out.println("In Queue " + job.isInQueue());
    Thread.sleep(200);
    job = js.getJob("maven-test");
    queueItem = js.getQueueItem(queueRef);
}
System.out.println("ended waiting.");

System.out.println("cancelled:" + queueItem.isCancelled());

if (queueItem.isCancelled()) {
    System.out.println("Job has been canceled.");
    return;
}

job = js.getJob("maven-test");
Build lastBuild = job.getLastBuild();

boolean isBuilding = lastBuild.details().isBuilding();
while (isBuilding) {
    System.out.println("Is building...(" + lastBuild.getNumber() + ")");
    Thread.sleep(200);
    isBuilding = lastBuild.details().isBuilding();
}

System.out.println("Finished.");
System.out.println(" Result: " + lastBuild.details().getResult());

But based on other requirements for a more convenient way we have a new class JenkinsTriggerHelper which offers that already. So in the end you can simply trigger a job and wait until it is finished by the following code:

JenkinsServer js = new JenkinsServer(URI.create("http://localhost:10090/buildserver"), "admin", "admin");

JenkinsTriggerHelper jth = new JenkinsTriggerHelper(js);

BuildWithDetails result = jth.triggerJobAndWaitUntilFinished("theJobYouWouldLikeToTrigger");
if (result.getResult().equals(BuildResult.CANCELLED)) {
 // Job execution has been cancelled.
}
// go further 

You would like to update the displayName and/or the description of a build. First we have an example to update the description:

JenkinsServer js = new JenkinsServer(URI.create("http://localhost:10090/buildserver"), "admin", "admin");

JobWithDetails job = js.getJob("maven-test");
job.getLastBuild().details().updateDescription("This is the new description.");

You can also update the displayName. This can simply be achieved by using the appropriate method.

JenkinsServer js = new JenkinsServer(URI.create("http://localhost:10090/buildserver"), "admin", "admin");

JobWithDetails job = js.getJob("maven-test");

Build buildByNumber = job.getBuildByNumber(11);
buildByNumber.details().updateDisplayName("This is DN for #11", true);
Clone this wiki locally