Search code examples
jenkinsqueuejenkins-pluginsjenkins-pipelinepause

Jenkins - How to pause queued job's runs and let a new build take priority


Jenkins any version.

I have two versions: 1.642.3 or 2.32.3

I have a Jenkins job jobA. Let's assume this job deploys an artifact to a target deploy server.

  • It takes 2 parameters, artifact name and target deploy server.
  • Execute concurrent build is currently DISABLED i.e. not check marked.

Assuming I launched multiple builds on this job manually -or via Jenkins CLI -or via Rest API way (i.e. via some automation/integration/parent upstream job calling this job).

  • I now see, there is one Jenkins build in-progress -and- all other N no. of runs are in "queued" mode.

Let's assume I have close to 100+ such builds in the queue (ready to be launched as soon as the in-progress one completes), I'm trying to see if there's a way I can PAUSE the existing queued builds (PS: I do NOT want to cancel them) and launch a new build (which I want to deploy urgently) and once that's done, UNPAUSE the queued builds (so that I don't have to cancel all of them and re-submitting/re-building them again by remembering what were the parameters passed -- for artifact name and target deploy server).

My 3 Conditions:

1) One of my current situation is the server where this job is running is one Jenkins master/slave machine (which have some credentials that can't be taken to other machines i.e. I can't replicate the source Jenkins machine (where the job is running as a slave(s) and thus, I can't use bunch of slaves) and

2) This job also creates some runtime folders/files at a common location on the source machine which I don't want to get overwritten by running concurrent / parallel builds if I enable "Execute concurrent builds". I know, the workspace for concurrent builds is individual to each job run, but not if the job is creating a common folder/file during it's run.

3) I don't want to create a copy of this job :)

In one sentence, is it possible to PAUSE existing queued builds (or some of the queued builds) so that I can launch a new build or make other ones take priority as the next build and then UNPAUSE the paused ones to resume (as launched without requiring them to relaunch)?


Solution

  • You can use Priority Sorter Plugin for this.
    Add a new string parameter to the job, for example BUILD_PRIORITY and set default value to 2. Then in Job Priorities menu select Use Priority from Build Parameter priority strategy and put that parameter there. priority configuration

    Now you can run 100+ jobs with the default BUILD_PRIORITY parameter value (2) and if you need to launch a new build urgently just set that parameter to 1 and it will be the first build in the queue.