1

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.

user2510141
  • 374
  • 3
  • 12
  • The node's label is set under each node in manage Jenkins > manage nodes. – Mark W Sep 03 '18 at 08:12
  • I don't believe you need a plugin to work with nodes. – Mark W Sep 03 '18 at 08:13
  • I'm also looking for the solution. Looks like here's [an official example](https://jenkins.io/doc/pipeline/examples/#trigger-job-on-all-nodes) but it silently failed without any information or exception thrown as I tried. – qingbo Nov 22 '18 at 08:44

3 Answers3

2

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'
                    }
                }
            }
        }
    }
}
Rene B
  • 31
  • 6
  • `choices: [nodesByLabel('label')],` didn't work for me, I had to change it to `choices: nodesByLabel('label'),` otherwise my agent name looked like `[myagentname]`. And I was able to do `agent { node { label params.node } }` at the top level of the `pipeline` – Sean Saleh Jun 10 '21 at 08:17
1

Let's say you added the parameter(say named slaveName) using the NodeLabel plugin on your pipeline. You now need to extract the value of slaveName and feed it into the agent->node->label field.

You can specify the node using the node property inside the agent. Like this -

agent
{
    node
    {
        label "${slaveName}"
    }
}
KatariaA
  • 752
  • 1
  • 7
  • 22
0

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()
mauryaAjay
  • 249
  • 2
  • 9