com.offbytwo.jenkins.JenkinsTriggerHelper Maven / Gradle / Ivy
The newest version!
package com.offbytwo.jenkins;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import com.offbytwo.jenkins.model.Build;
import com.offbytwo.jenkins.model.BuildResult;
import com.offbytwo.jenkins.model.BuildWithDetails;
import com.offbytwo.jenkins.model.JobWithDetails;
import com.offbytwo.jenkins.model.QueueItem;
import com.offbytwo.jenkins.model.QueueReference;
/**
* collection of convenient methods which use methods from {@link JenkinsServer}
* etc.
*
* @author Karl Heinz Marbaise
*
*/
public class JenkinsTriggerHelper {
private final JenkinsServer server;
private final Long retryInterval;
private static final Long DEFAULT_RETRY_INTERVAL = 200L;
public JenkinsTriggerHelper(JenkinsServer server) {
this.server = server;
this.retryInterval = DEFAULT_RETRY_INTERVAL;
}
public JenkinsTriggerHelper(JenkinsServer server, Long retryInterval) {
this.server = server;
this.retryInterval = retryInterval;
}
/**
* This method will trigger a build of the given job and will wait until the
* builds is ended or if the build has been cancelled.
*
* @param jobName The name of the job which should be triggered.
* @return In case of an cancelled job you will get
* {@link BuildWithDetails#getResult()}
* {@link BuildResult#CANCELLED}. So you have to check first if the
* build result is {@code CANCELLED}.
* @throws IOException in case of errors.
* @throws InterruptedException In case of interrupts.
*/
public BuildWithDetails triggerJobAndWaitUntilFinished(String jobName) throws IOException, InterruptedException {
return triggerJobAndWaitUntilFinished(jobName, false);
}
/**
* This method will trigger a build of the given job and will wait until the
* builds is ended or if the build has been cancelled.
*
* @param jobName The name of the job which should be triggered.
* @param params the job parameters
* @return In case of an cancelled job you will get
* {@link BuildWithDetails#getResult()}
* {@link BuildResult#CANCELLED}. So you have to check first if the
* build result is {@code CANCELLED}.
* @throws IOException in case of errors.
* @throws InterruptedException In case of interrupts.
*/
public BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, Map params)
throws IOException, InterruptedException {
return triggerJobAndWaitUntilFinished(jobName, params, false);
}
/**
* This method will trigger a build of the given job and will wait until the
* builds is ended or if the build has been cancelled.
*
* @param jobName The name of the job which should be triggered.
* @param params the job parameters
* @param crumbFlag set to true
or false
.
* @return In case of an cancelled job you will get
* {@link BuildWithDetails#getResult()}
* {@link BuildResult#CANCELLED}. So you have to check first if the
* build result is {@code CANCELLED}.
* @throws IOException in case of errors.
* @throws InterruptedException In case of interrupts.
*/
public BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, Map params,
boolean crumbFlag) throws IOException, InterruptedException {
JobWithDetails job = this.server.getJob(jobName);
QueueReference queueRef = job.build(params, crumbFlag);
return triggerJobAndWaitUntilFinished(jobName, queueRef);
}
/**
* This method will trigger a build of the given job and will wait until the
* builds is ended or if the build has been cancelled.
*
* @param jobName The name of the job which should be triggered.
* @param params the job parameters
* @param fileParams the job file parameters
* @param crumbFlag set to true
or false
.
* @return In case of an cancelled job you will get
* {@link BuildWithDetails#getResult()}
* {@link BuildResult#CANCELLED}. So you have to check first if the
* build result is {@code CANCELLED}.
* @throws IOException in case of errors.
* @throws InterruptedException In case of interrupts.
*/
public BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, Map params,
Map fileParams,
boolean crumbFlag) throws IOException, InterruptedException {
JobWithDetails job = this.server.getJob(jobName);
QueueReference queueRef = job.build(params, fileParams, crumbFlag);
return triggerJobAndWaitUntilFinished(jobName, queueRef);
}
/**
* This method will trigger a build of the given job and will wait until the
* builds is ended or if the build has been cancelled.
*
* @param jobName The name of the job which should be triggered.
* @param params the job parameters
* @param fileParams the job file parameters
* @return In case of an cancelled job you will get
* {@link BuildWithDetails#getResult()}
* {@link BuildResult#CANCELLED}. So you have to check first if the
* build result is {@code CANCELLED}.
* @throws IOException in case of errors.
* @throws InterruptedException In case of interrupts.
*/
public BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, Map params,
Map fileParams) throws IOException, InterruptedException {
return triggerJobAndWaitUntilFinished(jobName, params, fileParams, false);
}
/**
* This method will trigger a build of the given job and will wait until the
* builds is ended or if the build has been cancelled.
*
* @param jobName The name of the job which should be triggered.
* @param crumbFlag set to true
or false
.
* @return In case of an cancelled job you will get
* {@link BuildWithDetails#getResult()}
* {@link BuildResult#CANCELLED}. So you have to check first if the
* build result is {@code CANCELLED}.
* @throws IOException in case of errors.
* @throws InterruptedException In case of interrupts.
*/
public BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, boolean crumbFlag)
throws IOException, InterruptedException {
JobWithDetails job = this.server.getJob(jobName);
QueueReference queueRef = job.build(crumbFlag);
return triggerJobAndWaitUntilFinished(jobName, queueRef);
}
/**
* @param jobName The name of the job.
* @param queueRef {@link QueueReference}
* @return In case of an cancelled job you will get
* {@link BuildWithDetails#getResult()}
* {@link BuildResult#CANCELLED}. So you have to check first if the
* build result is {@code CANCELLED}.
* @throws IOException in case of errors.
* @throws InterruptedException In case of interrupts.
*/
private BuildWithDetails triggerJobAndWaitUntilFinished(String jobName, QueueReference queueRef)
throws IOException, InterruptedException {
JobWithDetails job = this.server.getJob(jobName);
QueueItem queueItem = this.server.getQueueItem(queueRef);
while (!queueItem.isCancelled() && job.isInQueue()) {
Thread.sleep(retryInterval);
job = this.server.getJob(jobName);
queueItem = this.server.getQueueItem(queueRef);
}
Build build = server.getBuild(queueItem);
if (queueItem.isCancelled()) {
return build.details();
}
boolean isBuilding = build.details().isBuilding();
while (isBuilding) {
Thread.sleep(retryInterval);
isBuilding = build.details().isBuilding();
}
return build.details();
}
}