How to trigger a replay of another build from another job programmatically

Viewed 27

How can I trigger a replay of a build from another job?

Context of Problem: I want to be able to have a job that can prioritize a build over others for another job (that has concurrency disabled). I was thinking I could do this by killing / cancelling jobs in the queue, triggering the new job, and then replay the ones that were cancelled.

I think I know how to cancel the jobs in the queue. I.e. by something like:

def buildNumbers = []

    def job = Jenkins.instance.getItemByFullName(TARGET_JOB)
    def builds = job.builds
    job = null
    for (build in builds) {
        if (build.isBuilding() && !(build.isInProgress())) {
            if(build instanceof WorkflowRun) {
                WorkflowRun run = (WorkflowRun) build
                if(!dryRun) {
                    //hard kill
                    run.doKill()
                    //release pipeline concurrency locks
                    StageStepExecution.exit(run)
                }
                println "Killed ${run}"
                buildNumbers.add(build.getNumber())
            } else if(build instanceof FreeStyleBuild) {
                FreeStyleBuild run = (FreeStyleBuild) build
                if(!dryRun) {
                    run.executor.interrupt(Result.ABORTED)
                }
                println "Killed ${run}"
            } else {
                println "WARNING: Don't know how to handle ${item.class}"
            }
        }
    }

But say I have saved these builds or build numbers that were killed, how can I replay them? I am open to other alternatives as well that solves this problem of prioritizing one build ahead of another.

0 Answers
Related