Jenkins run job with different parameter one by one - jenkins

I have a job with multiple parameters, but one is a choice parameter and it contains 10 choices, i need to build this job with all these choices one by one.
is that possible?

You can achieve this by using Jenkins Declarative Pipelines.
Here is an example pipeline which iterates through selected multi-choice parameter:
pipeline {
agent any
parameters {
choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Please select one/multiple options.')
}
stages {
stage('Build') {
steps {
script {
for (String selectedChoice : params.CHOICE) {
// do something
}
}
}
}
}
}

Related

How to choose node label based on an expression?

I have this example pipeline
pipeline {
parameters {
string(name: 'PLATFORM', description: 'target platform to build to')
}
agent {
node {
/* I want to choose 'windows-machine'
when PLATFORM matches "windows.*" */
label 'linux-machine'
}
}
}
I want jobs that target windows platforms to run on different nodes. How can you choose node labels based on whether pipeline parameters match an expression or not?
Instead of node, you can use agent selector label. Here is an example of how you can do this.
pipeline {
parameters {
string(name: 'PLATFORM', description: 'target platform to build to')
}
agent {label PLATFORM=='WINDOWS' ? 'windows': 'linux'}
stages {
stage('Test') {
steps {
script {
echo "test"
}
}
}
}
}

Jenkins Build Parameter dynamically from API

In Jenkins, when someone selects "Build with Parameters" options, how can I:
call an API that returns an Array
use that Array values list dynamically as checkboxes
get selected values in the job execution
You can do something like this and This is just generalised overview of you problem statement
arrayName = functionWhichReturnsArray()
pipeline {
agent any
parameters {
booleanParam(name: 'NAME_OF_CHECKBOX_YOU_WANT', defaultValue: arrayName, description: 'Tick the box to exceute the checkbox')
}
stages {
stage('Test') {
}
}
}
def functionWhichReturnsArray() {
add your api logic and return what you want to return
}

How to run jenkins pipeline on all or some servers based

I have a jenkins pipeline that copy file to a server. In job, i have defined 3 servers with the IPs.
What i need to achieve is that
A user can choose on which server to deploy the copy by typing yes or no under the depoly_on_server_x.
In my original pipeline, i'm using a list of IP - But the request is as I mentioned above
How can I define the request?
Thanks
server_1_IP - '1.1.1.1'
server_2_IP - '1.1.1.2'
server_3_IP - '1.1.1.3'
deploy_on_server_1 = 'yes'
deploy_on_server_2 = 'yes'
deploy_on_server_3 = 'no'
pipeline {
agent { label 'client-1' }
stages {
stage('Connect to git') {
steps {
git branch: 'xxxx', credentialsId: 'yyy', url: 'https://zzzz'
}
}
stage ('Copy file') {
when { deploy == yes }
steps {
dir('folder_a') {
file_copy(server_list)
}
}
}
}
}
def file_copy(list) {
list.each { item ->
sh "echo Copy file"
sh "scp 11.txt user#${item}:/data/"
}
}
How about using checkboxes instead?
You can use the Extended Choice Parameter to create a checkbox list based on the server values, when the user builds the job he selects the relevant servers, this list of selected servers is propagated to the job with the selected values, which you can then use for your logic.
Something like:
pipeline {
agent { label 'client-1' }
parameters {
extendedChoice(name: 'Servers', description: 'Select servers for deployment', multiSelectDelimiter: ',',
type: 'PT_CHECKBOX', value: '1.1.1.1,1.1.1.2 ,1.1.1.3', visibleItemCount: 5)
}
stages {
stage('Connect to git') {
steps {
git branch: 'xxxx', credentialsId: 'yyy', url: 'https://zzzz'
}
}
stage ('Copy files') {
steps {
dir('folder_a') {
script{
params.Servers.split(',').each { server ->
sh "echo Copy file to ${server}"
sh "scp 11.txt user#${server}:/data/"
}
}
}
}
}
}
}
In the UI it will look like:
You can also use a multi select select-list instead of checkboxes, or if you want to allow only a single value you can use radio buttons or a single select select-list.
If you want the user to see different values then those that will be used in the code it is also possible because you have the ability to manipulate the input value using groovy before using it.
For example if you want the user options to be <Hostname>-<IP> you can update the parameter value to be something like value: 'server1-1.1.1.1,server2-2.2.2.2', then in your code extract the relevant ip from the given values:
script {
params.Servers.split(',').each { item ->
server = item.split('-').last()
sh "echo Copy file to ${server}"
sh "scp 11.txt user#${server}:/data/"
}
}

How to configure dynamic parameters in declarative pipeline (Jenkinsfile)?

Using the declarative pipeline syntax, I want to be able to define parameters based on an array of repos, so that when starting the build, the user can check/uncheck the repos that should not be included when the job runs.
final String[] repos = [
'one',
'two',
'three',
]
pipeline {
parameters {
booleanParam(name: ...) // static param
// now include a booleanParam for each item in the `repos` array
// like this but it's not allowed
script {
repos.each {
booleanParam(name: it, defaultValue: true, description: "Include the ${it} repo in the release?")
}
}
}
// later on, I'll loop through each repo and do stuff only if its value in `params` is `true`
}
Of course, you can't have a script within the parameters block, so this won't work. How can I achieve this?
Using the Active Choices Parameter plugin is probably the best choice, but if for some reason you can't (or don't want to) use a plugin, you can still achieve dynamic parameters in a Declarative Pipeline.
Here is a sample Jenkinsfile:
def list_wrap() {
sh(script: 'echo choice1 choice2 choice3 choice4 | sed -e "s/ /\\n/g"', , returnStdout: true)
}
pipeline {
agent any
stages {
stage ('Gather Parameters') {
steps {
timeout(time: 30, unit: 'SECONDS') {
script {
properties([
parameters([
choice(
description: 'List of arguments',
name: 'service_name',
choices: 'DEFAULT\n' + list_wrap()
),
booleanParam(
defaultValue: false,
description: 'Whether we should apply changes',
name: 'apply'
)
])
])
}
}
}
}
stage ('Run command') {
when { expression { params.apply == true } }
steps {
sh """
echo choice: ${params.service_name} ;
"""
}
}
}
}
This embeds a script {} in a stage, which calls a function, which runs a shell script on the agent/node of the Declarative Pipeline, and uses the script's output to set the choices for the parameters. The parameters are then available in the next stages.
The gotcha is that you must first run the job with no build parameters in order for Jenkins to populate the parameters, so they're always going to be one run out of date. That's why the Active Choices Parameter plugin is probably a better idea.
You could also combine this with an input command to cause the pipeline to prompt the user for a parameter:
script {
def INPUT_PARAMS = input message: 'Please Provide Parameters', ok: 'Next',
parameters: [
choice(name: 'ENVIRONMENT', choices: ['dev','qa'].join('\n'), description: 'Please select the Environment'),
choice(name: 'IMAGE_TAG', choices: getDockerImages(), description: 'Available Docker Images')]
env.ENVIRONMENT = INPUT_PARAMS.ENVIRONMENT
env.IMAGE_TAG = INPUT_PARAMS.IMAGE_TAG
}
Credit goes to Alex Lashford (https://medium.com/disney-streaming/jenkins-pipeline-with-dynamic-user-input-9f340fb8d9e2) for this method.
You can use CHOICE parameter of Jenkins in which user can select a repository.
pipeline {
agent any
parameters
{
choice(name: "REPOS", choices: ['REPO1', 'REPO2', 'REPO3'])
}
stages {
stage ('stage 1') {
steps {
// the repository selected by user will be printed
println("$params.REPOS")
}
}
}
}
You can also use the plugin Active Choices Parameter if you want to do multiple select : https://plugins.jenkins.io/uno-choice/#documentation
You can visit pipeline syntax and configure in below way to generate code snippet and you can put it in the jenkins file:
Copy the snippet code and paste it in jenkinsfile at the start.

Jenkins agent as variable at the stage level in Jenkins pipeline

I have multiple Jenkins worker nodes, and a declarative jenkins pipeline.
I would like to create a choice parameter at each stage in order to allow worker node selection. Is this possible ?
At the pipeline top level I have :
pipeline {
agent { label 'node1||node2' }
...
I know that it the agent can be also specified at the stage level:
stage("Test") {
agent {label "node1"}
...
}
But I would like something like this:
stage('Test') {
agent { label
parameters {
choice choices: ['node1', 'node2'], description: name: 'jenkins_worker'
}
}
}
Is this possible from the syntax point of view ?
Declare agent as parameter:
pipeline {
agent { label parameters.AGENT }
...
Overwrite it in stage:
stage('Test') {
AGENT="node1"
}

Resources