Kirubakaran Kannan
Kirubakaran Kannan

Reputation: 43

How to use choice parameter integer value as array in Jenkins Declarative pipeline file

I have a Jenkins choice parameter which is integer value. I need to loop through the parameter value in my Jenkins File to run a function [i] times.

Say for Ex: Choice Parameter has ['1','2','3','4'] in drop down. If I chooses 4, the loop should go through 4 times.

But my below code only displays I choose in the parameter which is '4' while echoing it. Could someone help me in loop through.

script {
              def loop_value = "${params.choiceparameter}"
              loop_value.each() {
              echo it
       }
}

Upvotes: 0

Views: 7851

Answers (2)

Stefano Martins
Stefano Martins

Reputation: 482

Use it like this:

pipeline {
    agent any

    parameters {
        choice choices: ['1', '2', '3', '4', '5'], description: '', name: 'choiceParameter'
    }

    stages {
        stage("stage1") {
            steps {
                script {
                    for (i = 0; i < params.choiceParameter.toInteger(); i ++) {
                        print(i)
                    }
                }
            }
        }
    }
}

Upvotes: 3

Chris Maggiulli
Chris Maggiulli

Reputation: 3814

Groovy Solution

Here is a groovy solution more similar to your own

script {

    def loop_value = "${params.choiceparameter}"

    1.upto(loop_value.toInteger()) {
         println it
    }
}

Upvotes: 0

Related Questions