Jernej Gorički
Jernej Gorički

Reputation: 894

How to define a Label parameter in a parameterized build using scripted pipeline approach

I'm trying to solve the same problem as this SO question: How to trigger a jenkins build on specific node using pipeline plugin?

The only difference in my case is that the job I'm triggering is another scripted pipeline job. So the second step in the proposed solution does not apply in my case:

  1. Install Node and Label parameter plugin
  2. In test_job's configuration, select 'This build is parameterized' and add a Label parameter and set the parameter name to 'node'
  3. In pipeline script, use code (code omitted)

My question is how to define the :

org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterDefinition

parameter inside my scripted pipeline parameterized job (not through the GUI).

What I have tried:

properties([[$class         : 'RebuildSettings',
         autoRebuild    : false,
         rebuildDisabled: false],
         parameters([org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterDefinition(name: 'node')])])

Upvotes: 2

Views: 11057

Answers (2)

np2807
np2807

Reputation: 1160

If your use case is to have generic pipeline to be executed in particular Agent Node, then you can use 'Agent-Server-parameter' plugin with which you can add agent-name parameter as agent of your choice from drop down, into parameterized pipeline (or call it as 'Master' pipeline) and can use agent-name parameter under your pipeline script(e.g. calling sample.groovy inside Master parameterized-pipeline). And for another parameters, (may be string, boolean, choice) you defined within pipeline(without GUI). See the below example of sample.groovy which I am calling from Master job.

#!groovy

/* This Groovy implementation is pipeline for a Sample project */

pipeline {
    agent { label params['agent-name'] } //agent can be configured for stage as well.
    options {
        timeout(time: 1, unit: 'HOURS', activity: true)  // abort if nothing happens 
    timestamps()  // prepend timestamps on the console output
    }//option

    parameters {
        booleanParam(
        name:        'BOO_PARAM1',    defaultValue: false,
        description: 'Enable Parameter 1')
        booleanParam(
        name:        'BOO_PARAM2', defaultValue: false,
        description: 'Enable Parameter 2')
        stringParam('MY_PATH', 'C:\SampleProject')
        choiceParam('RUN_JOBON_NODE', ['YES', 'NO'])
    }//parameters

    environment {
        /* Environment Variable definition and its use */
        BOO_PARAM1 = "${params.BOO_PARAM1}"
    }//environment
    stages {
        /* agent is single for complete pipeline but can be changed for stage */
        stage('Hello') {
           when {
                expression { return params.BOO_PARAM1}
            }
            print"Hello Stage on %agent-name%"
        } // Stage
    }//stages
}//pipeline 

Note: post build stage is excluded. 'agent-server-parameter' plugin gives you leverage to have generic pipeline (common stages) to be executed on different Nodes.

Upvotes: 0

Jernej Gorički
Jernej Gorički

Reputation: 894

The easiest way to generate the code you need for your parameterized scripted pipeline is to:

  1. Go to Pipeline Snippet Generator
  2. Select "properties: Set job properties"
  3. Check "This project is parameterized"
  4. Click "Add parameter" and select "Label"
  5. Click "Generate pipeline script"

This gives you:

properties([

    [$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false], 

    parameters([
        [$class: 'LabelParameterDefinition', 
            allNodesMatchingLabel: false, 
            defaultValue: '', 
            description: '', 
            name: 'node', 
            nodeEligibility: [$class: 'AllNodeEligibility'], t
            riggerIfResult: 'allCases']
        ]
    )

])

But in my case this wasn't even necessary. All you need is a regular string parameter with a custom name, lets say "node" and then do:

node(params.node){}

Upvotes: 4

Related Questions