user2510141
user2510141

Reputation: 424

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.

Upvotes: 2

Views: 8512

Answers (4)

Miguel Sousa
Miguel Sousa

Reputation: 91

To use the Node and Label parameter plug-in in a Declarative Pipeline you need to do something like this:

properties([
  parameters([
    booleanParam(name: 'TEST', defaultValue: false, description: 'Trigger CI tests'),
    choice(name: 'ENV', choices: ['dev','prod']),
    [
      $class: 'NodeParameterDefinition',
      allowedSlaves: ['ALL (no restriction)'],
      defaultSlaves: ['master'],
      description: 'What node to run the build on:',
      name: 'NODELABEL',
      nodeEligibility: [$class: 'IgnoreOfflineNodeEligibility'],
      triggerIfResult: 'allowMultiSelectionForConcurrentBuilds'
    ]
  ])
])

pipeline {
  agent { label env.NODELABEL }

  options {
    ansiColor('xterm')
    timestamps()
  }

  stages {
    stage('Print the Values') {
      steps {
        echo "Trigger CI: ${params.TEST}"
        echo "Environment: ${params.ENV}"
        echo "Node name: ${env.NODELABEL}"
      }
    }
  }
}

Upvotes: 1

mauryaAjay
mauryaAjay

Reputation: 279

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()

Upvotes: 0

Rene B
Rene B

Reputation: 31

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'
                    }
                }
            }
        }
    }
}

Upvotes: 2

KatariaA
KatariaA

Reputation: 822

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}"
    }
}

Upvotes: 2

Related Questions