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. 😉