Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gokart23/a2aedaf8617e765f7b22e6cee976f9ae to your computer and use it in GitHub Desktop.
Save gokart23/a2aedaf8617e765f7b22e6cee976f9ae to your computer and use it in GitHub Desktop.
Programmatically trigger a pipeline run (with custom data) in Jenkins
import jenkins.model.*;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.queue.QueueTaskFuture;
import org.jenkinsci.plugins.workflow.cps.replay.ReplayAction;
String text = "node('some-node') { echo 'hi'; }"
Job test_job = Jenkins.getInstance().getItemByFullName("pipeline-job-name", hudson.model.Job);
ReplayAction action = test_job.getLastBuild().getAction(ReplayAction.class)
QueueTaskFuture<? extends Run> f = action.run(text, action.getOriginalLoadedScripts())
Run b = f.waitForStart()
println "Started job"
while (b.isBuilding()) {
Thread.sleep(1000);
}
println "Job completed"
b.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment