Reputation: 894
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:
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
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
Reputation: 894
The easiest way to generate the code you need for your parameterized scripted pipeline is to:
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