Reputation: 31
I have list of apps as checkboxes (using extended choice parameter with ',' as multiSelectDelimiter). I want to execute a stage if a matching checkbox is checked, else skip it.
For eg: apps: app1,app3 [checkboxes selected] checkboxes selected image
stages{
paralle{
stage('First'){
when {
// execute this when app1 is selected
}
steps {
}
}
stage('Second'){
when {
// execute this when app2 is selected, should skip this as 'app2' is not checked
}
steps {
}
}
stage('Third'){
when {
// execute this when app3 is selected
}
steps {
}
}
stage('Fourth'){
when {
// execute this when app4 is selected, else skip it
}
steps {
}
}
}
}
Upvotes: 3
Views: 4647
Reputation: 1
I used below piece in the pipeline to select the stage based on parameters provided in the Jenkins UI. "Boolean parameter" can be useful instead of "Extended Choice Parameter" to avoid clutter in the pipeline. Reference - https://www.jenkins.io/doc/book/pipeline/syntax/#when
stage("Install Application1"){
when { environment name: 'APP_1', value: 'true' }
steps{
sh '''
Steps to install the application1
'''
}
}
stage("Install Application2"){
when { environment name: 'APP_2', value: 'true' }
steps{
sh '''
Steps to install the application2
'''
}
}
Upvotes: 0
Reputation: 2098
It can be done this way
def choice=[]
node {
choice = params["my-checkbox"].split(",")
}
pipeline {
agent any;
parameters {
checkboxParameter name:'my-checkbox', format:'JSON', uri:'https://raw.githubusercontent.com/samitkumarpatel/test0/main/checkbox.json'
/*
consider this is the structure of CheckBox in https://raw.githubusercontent.com/samitkumarpatel/test0/main/checkbox.json URI
{
"key": "stage1",
"value": "stage1"
},
{
"key": "stage2",
"value": "stage2"
},
{
"key": "stage3",
"value": "stage3"
}
*/
}
stages {
stage('stage1') {
when {
expression {
'stage1' in choice
}
}
steps {
echo "${env.STAGE_NAME} execuated"
}
}
stage('stage2') {
when {
expression {
'stage2' in choice
}
}
steps {
echo "${env.STAGE_NAME} execuated"
}
}
stage('stage3') {
when {
expression {
'stage3' in choice
}
}
steps {
echo "${env.STAGE_NAME} execuated"
}
}
}
}
This pipeline will run the stages which are selected during the build
with this parameter, it will only trigger stage1
and stage2
Upvotes: 1