Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 467
Cookbook
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:
JenkinsServerjs = newJenkinsServer(URI.create("http://localhost:10090/buildserver"), "admin", "admin");
JobWithDetailsjob = js.getJob("maven-test");
QueueReferencequeueRef = job.build(true);
System.out.println("Ref:" + queueRef.getQueueItemUrlPart());
job = js.getJob("maven-test");
QueueItemqueueItem = 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");
BuildlastBuild = job.getLastBuild();
booleanisBuilding = 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:
JenkinsServerjs = newJenkinsServer(URI.create("http://localhost:10090/buildserver"), "admin", "admin");
JenkinsTriggerHelperjth = newJenkinsTriggerHelper(js);
BuildWithDetailsresult = 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:
JenkinsServerjs = newJenkinsServer(URI.create("http://localhost:10090/buildserver"), "admin", "admin");
JobWithDetailsjob = 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.
JenkinsServerjs = newJenkinsServer(URI.create("http://localhost:10090/buildserver"), "admin", "admin");
JobWithDetailsjob = js.getJob("maven-test");
BuildbuildByNumber = job.getBuildByNumber(11);
buildByNumber.details().updateDisplayName("This is DN for #11", true);