Skip to content

Instantly share code, notes, and snippets.

@BenNeise
Created January 14, 2016 19:08
Show Gist options
  • Save BenNeise/22a4650a5127fcbd15f4 to your computer and use it in GitHub Desktop.
Save BenNeise/22a4650a5127fcbd15f4 to your computer and use it in GitHub Desktop.
Invoke a Jenkins job from VMware Orchestrator
var jenkinsPollingIntervalMS = 1000;
var buildURL = "";
var buildResult = null;
/*
Create the request object. The two paramaters are:-
- An array of values for the URL template paramaters. We have none, so this is null
- Any content for POST or PUT operations. Ours is a GET, so this is also nulll
*/
var objRESTRequest = generateCatFact.createRequest(null,null);
// Execute the REST operation
var objRESTResponse = objRESTRequest.execute();
System.debug("Status code: " + objRESTResponse.statusCode);
// The location property gives us the URL of the queue item
System.debug("Location: " + objRESTResponse.getAllHeaders().get("Location"));
// Wait for job to be queued by looking for an "executable" property on the response
while (buildURL === ""){
var url = objRESTResponse.getAllHeaders().get("Location") + "api/json";
var urlObject = new URL(url);
result = urlObject.getContent() ;
//System.debug(result);
var objResult = JSON.parse(result);
if (objResult.hasOwnProperty("executable")){
System.debug(objResult.executable.url);
buildURL = objResult.executable.url;
}
System.sleep(jenkinsPollingIntervalMS);
}
// Now that the job's queued, we need to wait for it to be completed
while (buildResult === null){
url = buildURL + "api/json";
var urlObject = new URL(url);
var result = urlObject.getContent() ;
var objResult = JSON.parse(result);
System.debug(result);
System.debug("Build result:" + buildResult);
buildResult = objResult.result;
System.sleep(jenkinsPollingIntervalMS);
}
url = objResult.url + "consoleText";
var urlObject = new URL(url);
result = urlObject.getContent();
// The result is the actual console output.
System.log(result);
System.debug("Build Result: " + buildResult);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment