Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Remote trigger customization #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import hudson.FilePath;
import hudson.EnvVars;
import hudson.Launcher;
import hudson.console.ConsoleNote;
import hudson.Extension;
import hudson.util.CopyOnWriteList;
import hudson.util.ListBoxModel;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.Result;
import hudson.model.AbstractProject;
import hudson.tasks.Builder;
Expand All @@ -33,6 +35,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
Expand All @@ -55,7 +59,6 @@ public class RemoteBuildConfiguration extends Builder {
private final String token;
private final String remoteJenkinsName;
private final String job;

private final boolean shouldNotFailBuild;
private final int pollInterval;
private final int connectionRetryLimit = 5;
Expand Down Expand Up @@ -123,7 +126,8 @@ public RemoteBuildConfiguration(String remoteJenkinsName, boolean shouldNotFailB

// convert the String array into a List of Strings, and remove any empty entries
this.parameterList = new ArrayList<String>(Arrays.asList(params));



}

public RemoteBuildConfiguration(String remoteJenkinsName, boolean shouldNotFailBuild,
Expand Down Expand Up @@ -383,11 +387,11 @@ private String buildTriggerUrl(String job, String securityToken, Collection<Stri
triggerUrlString += buildTokenRootUrl;
triggerUrlString += getBuildTypeUrl(isRemoteJobParameterized);

this.addToQueryString("job=" + this.encodeValue(job));
this.addToQueryString("job=" + job);

} else {
triggerUrlString += "/job/";
triggerUrlString += this.encodeValue(job);
triggerUrlString += job;
triggerUrlString += getBuildTypeUrl(isRemoteJobParameterized);
}

Expand All @@ -407,7 +411,7 @@ private String buildTriggerUrl(String job, String securityToken, Collection<Stri
this.addToQueryString("delay=0");

triggerUrlString += "?" + this.getQueryString();

return triggerUrlString;
}

Expand All @@ -428,7 +432,7 @@ private String buildGetUrl(String job, String securityToken) {
String urlString = remoteServer.getAddress().toString();

urlString += "/job/";
urlString += this.encodeValue(job);
urlString += job;

// don't try to include a security token in the URL if none is provided
if (!securityToken.equals("")) {
Expand Down Expand Up @@ -461,39 +465,45 @@ private void failBuild(Exception e, BuildListener listener) throws IOException {
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException,
IOException, IllegalArgumentException {

String tempJobName=(this.getJob().trim().replace("/", "/job/"));
RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName());

String remoteJobURL=remoteServer.getAddress().toString() + "/job/" + tempJobName;
// Stores the status of the remote build
String buildStatusStr = "UNKNOWN";

if (remoteServer == null) {
this.failBuild(new Exception("No remote host is defined for this job."), listener);
return true;
}
String remoteServerURL = remoteServer.getAddress().toString();


List<String> cleanedParams = null;

if (this.getLoadParamsFromFile()) {

cleanedParams = loadExternalParameterFile(build);

} else {
// tokenize all variables and encode all variables, then build the fully-qualified trigger URL

cleanedParams = getCleanedParameters();

cleanedParams = replaceTokens(build, listener, cleanedParams);

}

String jobName = replaceToken(build, listener, this.getJob());

String jobName = replaceToken(build, listener, tempJobName);
String securityToken = replaceToken(build, listener, this.getToken());

boolean isRemoteParameterized = isRemoteJobParameterized(jobName, build, listener);
String triggerUrlString = this.buildTriggerUrl(jobName, securityToken, cleanedParams, isRemoteParameterized);


boolean isRemoteParameterized = isRemoteJobParameterized(tempJobName, build, listener);

String triggerUrlString = this.buildTriggerUrl(tempJobName, securityToken, cleanedParams, isRemoteParameterized);

// Trigger remote job
// print out some debugging information to the console

//listener.getLogger().println("URL: " + triggerUrlString);
listener.getLogger().println("Triggering this remote job: " + jobName);

listener.getLogger().println("Triggering this remote job: " + this.getJob());
// get the ID of the Next Job to run.
if (this.getPreventRemoteBuildQueue()) {
listener.getLogger().println("Checking that the remote job " + jobName + " is not currently building.");
Expand Down Expand Up @@ -525,13 +535,11 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
}

} else {
listener.getLogger().println("Not checking if the remote job " + jobName + " is building.");
listener.getLogger().println("Not checking if the remote job " + this.getJob() + " is building.");
}

String queryUrlString = this.buildGetUrl(jobName, securityToken);
String queryUrlString = this.buildGetUrl(tempJobName, securityToken);
queryUrlString += "/api/json/";

//listener.getLogger().println("Getting ID of next job to build. URL: " + queryUrlString);
JSONObject queryResponseObject = sendHTTPCall(queryUrlString, "GET", build, listener);
if (queryResponseObject == null ) {
//This should not happen as this page should return a JSON object
Expand All @@ -552,7 +560,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
foundIt: for (int tries = 3; tries > 0; tries--) {
for (int buildNumber : new SearchPattern(nextBuildNumber, 2)) {
listener.getLogger().println("Checking parameters of #" + buildNumber);
String validateUrlString = this.buildGetUrl(jobName, securityToken) + "/" + buildNumber + "/api/json/";
String validateUrlString = this.buildGetUrl(tempJobName, securityToken) + "/" + buildNumber + "/api/json/";
JSONObject validateResponse = sendHTTPCall(validateUrlString, "GET", build, listener);
if (validateResponse == null) {
listener.getLogger().println("Query failed.");
Expand Down Expand Up @@ -588,7 +596,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
BuildInfoExporterAction.addBuildInfoExporterAction(build, jobName, nextBuildNumber, Result.NOT_BUILT);

//Have to form the string ourselves, as we might not get a response from non-parameterized builds
String jobURL = remoteServerURL + "/job/" + this.encodeValue(jobName) + "/";
String jobURL = remoteServerURL + "/job/" + tempJobName + "/";

// This is only for Debug
// This output whether there is another job running on the remote host that this job had conflicted with.
Expand Down Expand Up @@ -631,6 +639,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
}

listener.getLogger().println("Remote build started!");
listener.getLogger().println("Triggered Remote Job : " + remoteJobURL.toString() + "/" + nextBuildNumber);
while (buildStatusStr.equals("running")) {
listener.getLogger().println("Waiting for remote build to finish.");
listener.getLogger().println("Waiting for " + this.pollInterval + " seconds until next poll.");
Expand Down Expand Up @@ -667,7 +676,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
} else {
listener.getLogger().println("Not blocking local job until remote job completes - fire and forget.");
}

return true;
}

Expand Down Expand Up @@ -715,7 +724,7 @@ public String getBuildStatus(String buildUrlString, AbstractBuild build, BuildLi
}

// print out some debugging information to the console
//listener.getLogger().println("Checking Status of this job: " + buildUrlString);

if (this.getOverrideAuth()) {
listener.getLogger().println(
"Using job-level defined credentails in place of those from remote Jenkins config ["
Expand Down Expand Up @@ -795,7 +804,6 @@ public String getConsoleOutput(String urlString, String requestType, AbstractBui
*/
public JSONObject sendHTTPCall(String urlString, String requestType, AbstractBuild build, BuildListener listener)
throws IOException {

return sendHTTPCall( urlString, requestType, build, listener, 1 );
}

Expand Down Expand Up @@ -1158,12 +1166,13 @@ private boolean isRemoteJobParameterized(String jobName, AbstractBuild build, Bu
//build the proper URL to inspect the remote job
RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName());
String remoteServerUrl = remoteServer.getAddress().toString();
remoteServerUrl += "/job/" + encodeValue(jobName);
remoteServerUrl += "/job/" + jobName;
remoteServerUrl += "/api/json";

try {

JSONObject response = sendHTTPCall(remoteServerUrl, "GET", build, listener);

if(response.getJSONArray("actions").size() >= 1){
isParameterized = true;
}
Expand Down