Prioritize the build queue in Declarative Jenkins Pipeline

What I want:

I want to change/sort the build queue in my Jenkins file (declarative Pipeline). Actually I want to make priority in my builds.

I am familiar with the following plugins:

  • PrioritySorter

    • It doesn’t have Pipeline interface.
    • It has to be set as parameter before build the job (I don’t have up-stream/trigger job).
  • accelerated-build-now-plugin

    • It doesn’t have Pipeline interface.

Description of my problem:

My Pipeline runs on more executors (more labels). The TRIGGERS label is not limited so many builds can run on it (Eg.: 100). The MAIN label is limited, only some builds can run on it (Eg.: 10). So If I have 30 builds on the TRIGGERS label then 20 build will be in the queue (waiting for free executor). I have a condition and if this condition is True, the build should be handled as Prio1. It means an “Important” build should be the first in the queue. So I want to prioritize the builds for MAIN label (Change the default FIFO way).

Example code:

pipeline {
    agent { label 'TRIGGERS' }  // Many builds are possible (Eg.: 100).
    stages{
        stage('Pre-preparation'){
            script {
                echo "Running Pre-preparation on '${env.NODE_NAME}' node." // Run on TRIGGER
                if(env.IMPORTANT){
                    echo "This build should be the PRIO 1 (Not waiting previous build in queue)!"
                }
            }
        }
        stage('Build activities on MAIN'){
            // If there isn't free executor the builds are waiting here for free executor.
            // One-by-one, FIFO way. I want to change that mechanism!
            agent { label "MAIN" }  // Some builds are possible (Eg.: 10).
            stages{
                stage('Preparation 1'){
                    steps {
                        script {
                            echo "Running preparation 1 on '${env.NODE_NAME}' node." // Run on MAIN
                        }
                    }
                }
                stage('Preparation 2'){
                    steps {
                        script {
                            echo "Running preparation 2 on '${env.NODE_NAME}' node." // Run on MAIN
                        }
                    }
                }
            }
        }
    }
}

My questions:

  • How can I change the order of the build queue (change the default FIFO way)?
  • How can I prioritize my builds for the MAIN label?

NOTE:

  • Only declarative Pipeline compatible solution is possible!