1

Don't need 2 prompt in Jenkins pipeline with input script. In snippet, there is highlighted 1 and 2 where pipeline prompt 2 times for user input.

If execute this pipeline, prompt 1 will provide user to "Input requested" only, so no option to "Abort". Prompt 2 is okay as it ask to input the value and also "Abort" option.

Prompt 1 is almost useless, can we skip promt this and directly reach to prompt 2?

pipeline {
    agent any
    parameters {
        string(defaultValue: '', description: 'Enter the Numerator', name: 'Num')
        string(defaultValue: '', description: 'Enter the Denominator', name: 'Den')

    }

    stages {
        stage("foo") {
            steps {
                script {
                    if ( "$Den".toInteger() == 0) {
                        echo "Denominator can't be '0', please re-enter it."
                        env.Den = input message: 'User input required', ok: 'Re-enter', // 1-> It will ask whether to input or not
                        parameters: [string(defaultValue: "", description: 'Enter the Denominator', name: 'Den')] // 2-> It will ask to input the value
                    }    
                }
                sh'''#!/bin/bash +x
                echo "Numerator: $Num\nDenominator: $Den"
                echo "Output: $((Num/Den))"
                '''
            }
        }
    }
}
Ashish Kumar
  • 524
  • 6
  • 18

1 Answers1

0

Yes, you can simplify the input by using $class: 'TextParameterDefinition'.

e.g.

pipeline {
    agent any

    parameters {
        string(defaultValue: '', description: 'Enter the Numerator', name: 'Num')
        string(defaultValue: '', description: 'Enter the Denominator', name: 'Den')
    }

    stages {
        stage("foo") {
            steps {
                script {
                    if ( "$Den".toInteger() == 0) {
                        env.Den = input(
                            id: 'userInput', message: 'Wrong denominator, give it another try?', parameters: [
                                [$class: 'TextParameterDefinition', defaultValue: '', description: 'Den', name: 'den']
                            ]
                        )
                    }
                }
                sh'''#!/bin/bash +x
                echo "Numerator: $Num\nDenominator: $Den"
                echo "Output: $((Num/Den))"
                '''
            }
        }
    }
}

If you want to go the extra mile and ask for both values:

def userInput = input(
    id: 'userInput', message: 'Let\'s re-enter everything?', parameters: [
        [$class: 'TextParameterDefinition', defaultValue: '', description: 'Denominator', name: 'den'],
        [$class: 'TextParameterDefinition', defaultValue: '', description: 'Numerator', name: 'num']
    ]
)
print userInput
env.Den = userInput.den
env.Num = userInput.num
Stefano
  • 4,730
  • 1
  • 20
  • 28
  • thanks @stefano, but what I am looking for to skip prompt 1 "Input requested" step, which has no meaning if user enter 0 as denominator. If user enter 0, pipeline should directly open the input text area, rather than of asking "Input requested" . – Ashish Kumar Sep 01 '19 at 18:38