Jenkins has many ways of managing the job queues. We can add a static slave node, use the plugin which creates temporary nodes or just add more workers to the existing servers. But sometimes we need something else. We need a possibility to set the priority of the job. It’s not possible with additional workers or nodes, hence we should use the other way. And Priority Sorter Plugin can solve our problem. Very quickly and easy, trust me. From now, Jenkins build priority will not have any secrets!
Jenkins build priority? For what?!
Let’s assume such situation:
We have a standalone Jenkins server without the additional nodes. The server has four workers – it means four jobs can run at the same time. Ok, so we have some previously configured jobs, maybe some of them are triggered automatically. We want to run the specific job as soon as possible, so we log in to the Jenkins and what do we see? All workers are busy! And someone scheduled additional jobs, so we must wait. Wait and wait and wait. We would have avoided that if we had had the possibility of the priority assignment.
How to prioritize Jenkins builds?
The simplest way to do this is using the Priority Sorter Plugin. It allows you to create and manage groups of jobs with assigned priorities, but also change the priority right before the job start. You can group jobs by their names, directories and so on, thus you have a very powerful tool. The basic entity is the JobGroup, which you can configure in the Job Priorities section. You can have one or more JobGroups, according to your needs.
After you install the plugin, you will see a new option in the left menu on the main screen. But first, check the Configure System section (Manage Jenkins -> Configure System. There you have also the new option – Priority Sorter, looks like this one:
- Absolute – Each job will be assigned a priority, and, the lower the priority, the sooner the job will be run
- Fair Queuing – Each Job will be assigned a priority, but the queue will try to share the resources equally over different priorities in a round-robin fashion
- Weighted Fair Queuing – The same as Fair Queuing, but jobs with a lower priority will be run more frequently those with higher a priority. Remember, that the lower the priority assignment, the higher the importance
The Number of priorities specifies the number of priority levels you want to use. The Default priority is used to specify the default priority level each job should have set if we don’t decide otherwise. The very important thing here is the Only Admins can edit Job Priorities checkbox. Without checking it, everybody will be able to change priorities of ALL your JobGroups. So it’s better to check this box.
So we have already set up the basic settings, so we can finally go to the specify our first JobGroup. After you finish the previous step, go to the main Jenkins screen and find Job Priorities option in the bottom of the left menu. There you have the main configuration, where you can specify Jenkins build priority.
There you can create one or more JobGroups and assign them some additional behavior. For example, you can add the name of the parameter, which will be used as the Job Parameter to control the priorities of the running jobs. So let’s create the first JobGroup with all of the Jobs and parameter named JobPriority.
As you can see it’s not the rocket science. I created the first JobGroup and add a description. After that, I chose All jobs as the value of the Jobs to include. You can choose between the following option:
- Jobs and Folder marked for Inclusion (you can specify the Job Group Name here)
- All Jobs
- Jobs included in Folder (you can choose one of your existing folders)
- Jobs included in a View (you can choose one of your existing folders, also you can use regexp)
At the Priority option, you should set the default priority for all of the jobs from JobGroup, but you can also use the globally configured level. Use additional rules when assigning a priority to a Job is optional, not needed, but that is exactly the place, where you should look if you want to manipulate the priority of the job you want to run. The second Priority Strategy is shown only for you, just to take a look at the possible options. Use the Priority from Build Parameter is the one we just want to set. At the Build Parameter Name field, you should specify the name of the parameter you add to your Jobs. In my example, that parameter is named JobPriority.
Now we can move on to the Job Configuration. Only one thing we should do is adding the new String Parameter with the same name as the parameter specified above. Yes, that is all. So, let assume that we have an existing job. Go to the configuration and add a new parameter:
or if you use DSL:
stringParam('JobPriority', '3', 'Specify the priority of this build.')
With this parameter, you can change Jenkins build priority even when other jobs are in progress. But please remember about one, probably the most important thing:
This plugin adds the ability to assign different priorities to jobs: the lower the priority a job has, the sooner the job will run.
If you do not forget this rule, your life will be much easier with the possibility to specify the Jenkins build priority. Now just start the job, assign priority you want and enjoy your new toy. 😉
5 Replies to “Jenkins build priority”
This was very helpful, many thx!
Hi, is there any other option or plugin that need to be set?
In my case it doesn’t work. I tried also “Jobs and folders marked for inclusion”.
The result is alwasy the same – FIFO.
Unfortunately, I don’t know if there’s another option – I’ve been using this plugin since the beginning and it works even for nested jobs and folder. Are you sure that you have a properly configured plugin? I have only one group (“All jobs” included) and default priority is set to 3 (exactly like on the screenshots) and it works perfectly. Do you use a lower number to set a higher priority? Because lower number == higher priority.
Did you try to add multiple jobs to the queue (veeeery long jobs, maybe some test jobs with sleep in bash script) and see what’s going with the jobs in the queue (and if they start as expected or not) when you set 1 priority and 5 priority (assuming that you created five degrees)?
I’m assuming that you only cover the configuration part, but how do I update the priority itself?
Suppose I have lots of queue running, and I want a particular job to be prioritize, do I have to manually trigger the job with low priority value on the JobPriority parameter?
It depends on how do you normally run your job – if you use a parameter in a job with the prioritization, you may just pass the value from the upstream job or in any other way. 🙂