TheWaterProgrammer
TheWaterProgrammer

Reputation: 8229

How to intake a number parameter as manually type input into my Jenkins declarative pipeline?

I have a "Jenkins declarative pipeline". I understand from explanation here that I could pass choices like below in my Jenkinsfile.

parameters {
    choice(name: 'TYPE_OF_DEPLOYMENT',
           choices: ['Android', 'iOS', 'macOS'],
           description: 'Select a platform to deploy build to')
}

Above works well to give 3 selections in a drop down.

Question:
But what if I want to intake a number or a string which is manually typed rather than selected from a drop down?
Is it possible to take a number input into the pipeline from the Jenkins page before starting a parameterised build?

Upvotes: 1

Views: 5469

Answers (1)

Stefano Martins
Stefano Martins

Reputation: 482

Sure! Let's suppose you want to receive the type of deployment and the version number (something like 1, 2, 10, etc.). You can then do something like this:

pipeline {
    agent any

    parameters {
        choice choices: ['Android', 'iOS', 'macOS'], description: 'Select a platform to deploy build to', name: 'TYPE_OF_DEPLOYMENT'
        string defaultValue: '1', description: 'Version number', name: 'VERSION', trim: true
    }

    stages {
        stage('Getting parameter values') {
            steps {
                script {
                    print("Type of deployment: ${env.TYPE_OF_DEPLOYMENT}")
                    print("Version number: ${env.VERSION}")
                }
            }
        }
    }
}

About your second question, about if it is possible to take a number input before the parameter screen, I don't think it is easily doable. Most people solve that kind of problem with inputs instead of parameters.

Like this:

pipeline {
    agent any

    parameters {
        choice choices: ['Android', 'iOS', 'macOS'], description: 'Select a platform to deploy build to', name: 'TYPE_OF_DEPLOYMENT'
    }

    stages {
        stage('Input version number') {
            steps {
                script {
                    def userInput = input id: 'VERSION_NUMBER', message: 'Please insert a version number here', parameters: [string(defaultValue: '1', description: '', name: 'VERSION_NUMBER', trim: true)]
                    print(userInput)
                }
            }
        }

        stage('Getting parameter values') {
            steps {
                script {
                    print("Type of deployment: ${env.TYPE_OF_DEPLOYMENT}")
                }
            }
        }
    }
}

Best regards!

Upvotes: 2

Related Questions