'How to use NodeLabel parameter plugin in declarative pipeline

Im trying to convert my freestyle job to a declarative pipeline job since the pipeline provides more flexibility. I cannot figure out how to use the NodeLabel parameter plugin (https://wiki.jenkins.io/display/JENKINS/NodeLabel+Parameter+Plugin) in a pipeline however.

pipeline {
agent any

parameters {
    // Would like something like LabelParameter here
}

stages {
    stage('Dummy1') {
        steps {
            cleanWs()
            sh('ls')
            sh('pwd')
            sh('hostname')
        }
    }
    stage('Dummy2') {
        steps {
            node("comms-test02") {
                sh('ls')
                sh('pwd')
                sh('hostname')
            }
        }
    }
}

I basically just need a way to start the job using a parameter that specifies where to build the job (using slave label).

Jenkins requires an agent field to be present which i set to 'any'. But it doesnt seem like there is a labelparameter available ?

As an alternative I tried using the 'node' command (https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#-node- allocate node). But that leaves me with two running jobs which, while working, doesnt look that pretty.

Does anyone if the NodeLabel parameter plugin can be used ? or maybe someone has a cleaner approach ?

Edit: Maybe I wasn't clear. I need to be able to run jobs on different nodes. The node to run on should be decided when triggering the job through a parameter. The node label plugin does this perfectly. However, I have not been able to reproduce this behavior in pipeline.



Solution 1:[1]

Here's a full example:

pipeline {
    parameters {
        choice(name: 'node', choices: [nodesByLabel('label')], description: 'The node to run on') //example 1: just listing all the nodes with label
        choice(name: 'node2', choices: ['label'] + nodesByLabel('label'), description: 'The node to run on') //example 2: add the label itself as the first choice to make "Any of the nodes" the default choice
    }
    
    agent none
    stages {
        stage('Test') {
            agent { label params.node}
            stages {
                stage('Print environment settings') {
                    steps {
                        echo "running on ${env.NODE_NAME}"
                        sh 'printenv | sort'
                    }
                }
            }
        }
    }
}

Solution 2:[2]

The following script worked for me to run the multiple jobs parallelly on different Node.

I have taken the reference from the build step plugin documentation.

https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/

def build_one() 
{
        parallel  one: { 
            stage('XYZ') {
                        catchError(buildResult: 'SUCCESS', stageResult:'FAILURE') {
                        
 build job: 'yourDownStreamJob', parameters: [[$class: 'NodeParameterValue', name: 'NodeToRun',labels: ['nodeName'], nodeEligibility: [$class: 'AllNodeEligibility']], string(name: 'ParentBuildName', value: "XX"), string(name: 'Browser', value: 'chrome'), string(name: 'Environment', value: 'envName')]
                        }
            }
        },
        two : { 
            stage('SecondArea') {
                        catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                         build job: 'yourDownStreamJob', parameters: [[$class: 'NodeParameterValue', name: 'NodeToRun',labels: ['Your'], nodeEligibility: [$class: 'AllNodeEligibility']], string(name: 'ParentBuildName', value: "XYX"), string(name: 'Browser', value: 'firefox'), string(name: 'Environment', value: 'envName')]
                        }
            }
        }
        
} 


build_one()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rene B
Solution 2 mauryaAjay