Created
October 17, 2017 21:39
-
-
Save cpanato/d628e4415d87ab06f71c70a6883e4963 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jenkins.model.Jenkins | |
import hudson.model.ParametersAction | |
import hudson.model.BooleanParameterValue | |
import hudson.model.Result | |
import hudson.model.Run | |
import org.jenkinsci.plugins.workflow.job.WorkflowRun | |
import org.jenkinsci.plugins.workflow.support.steps.StageStepExecution | |
import org.jenkinsci.plugins.workflow.job.WorkflowJob | |
//Get the PR Number | |
def thr = Thread.currentThread() | |
def build = thr?.executable | |
def resolver = build.buildVariableResolver | |
def PRNum = resolver.resolve("ghprbPullId") | |
// Get the channel to later connect to the slave to get the file | |
if(manager.build.workspace.isRemote()){ | |
channel = manager.build.workspace.channel | |
} | |
// Connect to the slave to copy the file | |
manager.listener.logger.println("Copying the file from the remote slave..."); | |
String fp = manager.build.workspace.getRemote().toString() + "/env_vars"; | |
remoteFile = new hudson.FilePath(channel, fp); | |
projectWorkspaceOnMaster = new hudson.FilePath(new File(manager.build.getProject().getRootDir(), "workspace")); | |
projectWorkspaceOnMaster.mkdirs(); | |
File localFile = File.createTempFile("jenkins","parameter"); | |
remoteFile.copyTo(new hudson.FilePath(localFile)); | |
manager.listener.logger.println("Done copying"); | |
// sleep a bit to wait jenkins refresh the jobs | |
sleep(3000); | |
def params = [ ]; | |
// Get the PR Job | |
def job = Jenkins.instance.getItemByFullName("tectonic-installer/PR-" + PRNum) | |
// If job is in the queue wait for that | |
manager.listener.logger.println(job.isInQueue()); | |
while(job.isInQueue()) { | |
manager.listener.logger.println("Job in the queue, waiting...."); | |
sleep(1000); | |
} | |
for (prBuild in job.builds) { | |
if (prBuild.getNumber().toInteger() == 1 && prBuild.isBuilding()) { | |
manager.listener.logger.println("Build 1 is running, will try to kill..."); | |
WorkflowRun run = (WorkflowRun) prBuild; | |
//hard kill | |
run.doKill(); | |
while(prBuild.isBuilding()) { | |
manager.listener.logger.println("Trying to kill the job...."); | |
run.doKill(); | |
sleep(1000); | |
} | |
manager.listener.logger.println("Job Killed"); | |
//release pipeline concurrency locks | |
StageStepExecution.exit(run); | |
sleep(1000); | |
// Load the File and set the job trigger | |
Properties properties = new Properties(); | |
localFile.withInputStream { | |
properties.load(it); | |
} | |
properties.each { prop, val -> | |
temp = new BooleanParameterValue(prop,val.toBoolean()); | |
params.add(temp); | |
} | |
sleep(5000); | |
manager.listener.logger.println("Will trigger new build..."); | |
job.scheduleBuild2(5, null, new ParametersAction(params)); | |
manager.listener.logger.println("New job triggered"); | |
break; | |
} | |
} | |
manager.listener.logger.println("Done"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment