Jenkins configuration matrix triggering multiple jobs - jenkins

I have a configuration matrix with multiple values and anytime I select multiple values they create multiple build jobs.
Want I want to have is to be able to select multiple values but Jenkins only creating one build job.
Does anyone know where and what I have to modify in the configuration tab for this to work?

☑️ This project is parameterized lets you Add Parameter   ▼ more than once for one project/job. The same applies to pipeline input, e.g.:
stages {
stage('Parameters') {
steps {
input( message: 'Enter or select parameter values:',
parameters: [
string( name: 'StringParam1', defaultValue: 'StringValue1', trim: true ),
booleanParam( name: 'BooleanParam' )
]
)
}
}
}
See https://YOUR_JENKINS_URL/pipeline-syntax/ → Steps → Sample step: input: Wait for interactive input → Advanced... → Parameters → Add   ▼ for samples of the various input types.

Related

Jenkins Parameterized Project For Multiple Parameters

I have Jenkins Pipeline which is triggering for different projects. However the only difference in all the pipelines is just the name.
So I have added a parameter ${project} in parameter of jenkins and assigned it a value of the name of the project.
We have a number of projects and I am trying to find a better way through which I can achieve this.
I am thinking how can we make the parameter run with different parameters for all the projects without actually creating different projects under jenkins.
I am pasting some screenshot for you to understand what exactly I want to achieve.
As mentioned here, this is a radioserver project, having a pipeline which has ${project} in it.
How can I give multiple values to that {project} from single jenkins job?
IF you have any doubts please message me or add a comment.
You can see those 2 projects I have created, it has all the contents same but just the parameterized value is different, I am thinking how can I give the different value to that parameter.
As you can see the 2 images is having their default value as radioserver, nrcuup. How can I combine them and make them run seemlessly ?
I hope this will help. Let me know if any changes required in answer.
You can use conditions in Jenkins. Based on the value of ${PROJECT}, you can then execute the particular stage.
Here is a simple example of a pipeline, where I have given choices to select the value of parameter PROJECT i.e. test1, test2 and test3.
So, whenever you select test1, jenkins job will execute the stages that are based on test1
Sample pipeline code
pipeline {
agent any
parameters {
choice(
choices: ['test1' , 'test2', 'test3'],
description: 'PROJECT NAME',
name: 'PROJECT')
}
stages {
stage ('PROJECT 1 RUN') {
when {
expression { params.PROJECT == 'test1' }
}
steps {
echo "Hello, test1"
}
}
stage ('PROJECT 2 RUN') {
when {
expression { params.PROJECT == 'test2' }
}
steps {
echo "Hello, test2"
}
}
}
}
Output:
when test1 is selected
when test2 is selected
Updated Answer
Yes, it is possible to trigger the job periodically with a specific parameter value using the Jenkins plugin Parameterized Scheduler
After you save the project with some parameters (like above mentioned pipeline code), go back again to the Configure and under Build Trigger, you can see the option of Build periodically with parameters
Example:
I will here run the job for PROJECT=test1 every even minutes and PROJECT=test2 every uneven minutes. So, below is the configuration
*/2 * * * * %PROJECT=test1
1-59/2 * * * * %PROJECT=test2
Please change the crontab values according to your need
Output:

How to use multiple choices to run a job?

I have created a jenkins pipleine to run a job (e.g. Pipeline A runs job B). Within job B there is multiple parameters. One of the parameters is a choice parameter that has multiple different choices. I need pipeline A to run job B with all of the different choices at once (Pipeline A runs Job B with all of the different choices in one build). I am not too familiar with using the Jenkins declarative syntax but I am guessing I would use some sort of for loop to iterate over all of the available choices?
I have searched and searched through Stack overflow/google for an answer but have not had much luck.
You can define the options in a separate file outside your jobs, in shared library:
// vars/choices.groovy
def my_choices = [
"Option A",
"Option B", // etc.
]
You can then use these choices when defining the job:
// Job_1 Jenkinsfile
#Library('my-shared#master') _
properties([
parameters([
[$class: 'ChoiceParameterDefinition',
name: 'MY_IMPORTANT_OPTION',
choices: choices.my_choices as List,
description: '',
],
...
pipeline {
agent { any }
stages {
...
In Job 2, you can iterate over the values:
// Job_2 Jenkinsfile
#Library('my-shared#master') _
pipeline {
agent { any }
stages {
stage {
for (String option : choices.my_choices) {
build job: "Job_1",
wait: false,
parameters: [ string(name: 'MY_IMPORTANT_OPTION', value: option) , // etc.
]
Job_2 when it is run will asynchronously trigger a number of runs of Job_1 each time with a different parameter.

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:
Install Node and Label parameter plugin
In test_job's configuration, select 'This build is parameterized' and add a Label parameter and set the parameter name to 'node'
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')])])
The easiest way to generate the code you need for your parameterized scripted pipeline is to:
Go to Pipeline Snippet Generator
Select "properties: Set job properties"
Check "This project is parameterized"
Click "Add parameter" and select "Label"
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){}
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.

How to conditionally hide parameter?

I am trying to create a jenkins pipeline job with parameters. I want the parameters to show up conditionally. The condition depends on a selection of a previous parameter.
I have tried the Active choices plug-in. It allows me to choose a value of a parameter conditionally. I want the whole parameter to appear in the UI conditionally.
Is it possible with jenkins pipeline files?
I do not believe this is possible. In the case of declarative/scripted pipelines, parameters are 'post-processed' meaning essentially the ones you see are what was evaluated in the previous 'run/build'. Which is why it takes a build before 'Build with Parameters' becomes available.
As an alternative, (if you're using scripted/declarative pipelines) you could use an Input step and make it trigger conditionally.
if ( x == true ) {
def userInput = input(
id: 'userInput', message: 'Let\'s promote?', parameters: [
[$class: 'TextParameterDefinition', defaultValue: 'uat', description: 'Environment', name: 'env'],
[$class: 'TextParameterDefinition', defaultValue: 'uat1', description: 'Target', name: 'target']
])
}
Example pulled from:
https://support.cloudbees.com/hc/en-us/articles/204986450-Pipeline-How-to-manage-user-inputs

How to access all nodes from NodeParameterDefinition in jenkins pipeline?

I'm writing a Jenkinsfile that use the NodeLabel Parameter Plugin for jenkins. Here I use the NodeParameterDefinition to give the user the ability to select nodes where the build should happen. I have enabled allowMultiSelectionForConcurrentBuilds, but I only still get a string with only one node name when accessing the parameter value in the Jenkinsfile. The parameter value type is also a string, how can I get all the nodes the user selected for the parameter?
paramter definition:
[
$class: 'NodeParameterDefinition',
allowedSlaves: ['ALL (no restriction)'],
defaultSlaves: ['master'],
description: 'What nodes to run the build on.',
name: 'BUILD_NODE',
nodeEligibility: [$class: 'AllNodeEligibility'],
triggerIfResult: 'allowMultiSelectionForConcurrentBuilds'
]
So if I select multiple nodes when executing, I still only get one node name when accessing this parameter value.
echo "Will build on $BUILD_NODE";
Is multi node selection was enabled not possible with pipeline scripts?
How I access the parameter value:
echo "Will build on $BUILD_NODE";
node("$BUILD_NODE")
{
...
}
NodeLabel Parameter Plugin doesn't work smoothly with Pipeline and Blue Ocean, just as it is not updated frequently (see the revision history). Jenkins Plugins must follow requirements in order to be compatible with Pipeline.
Unfortunately the issue is still unresolved (unknown when it will be resolved):
https://issues.jenkins-ci.org/browse/JENKINS-43720
The problem is that I can not use env.NODE_PARAM or NODE_PARAM to get
multiple selection of nodes, as those are only a string representation
of a single node.
You can vote for this jira-task JENKINS-43720 (click "Vote for this issue"), or participate in the plugin development.
So far I found my clumsy way to imitate the plugin behavior by using another parameter option choice (but this works in Blue Ocean!):
properties([
parameters([
choice(choices: ["none", "node_1", "node_2"], description: "", name: "NODE_1"),
choice(choices: ["none", "node_1", "node_2"], description: "", name: "NODE_2")
])
])
// here you can write your behaviour
// e.g. validation of params, e.g. if 'none' is selected, then use the default node_X
node(env.NODE_1) { }
node(env.NODE_2) { }
or you can use the option string:
properties([
parameters([
string(defaultValue: "node_1, node_2", description: "", name: "NODE", trim: false)
])
])
// parse here the param env.NODE

Resources