Skip to content

Instantly share code, notes, and snippets.

@sitaramshelke
Created February 4, 2021 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sitaramshelke/db510f92eabe31b0a16a7db99d161062 to your computer and use it in GitHub Desktop.
Save sitaramshelke/db510f92eabe31b0a16a7db99d161062 to your computer and use it in GitHub Desktop.
Jenkins shared library input example
// src/com/example/Helper.groovy
#!/usr/bin/env groovy
package com.example
class Helper implements Serializable {
def context
Helper(context) {
this.context = context
}
def getUserInput(script) {
// Following didnt' work.
def ANSWER = context.input(message: 'Please provide parameters', ok: 'Next',
parameters: context.choice(name: 'ANSWER', choices: ['yes', 'no'].join('\n'), description: 'Select Answer?'))
// Tried following, this also didn't work
//def ANSWER = input(message: 'Please provide parameters', ok: 'Next',
// parameters: choice(name: 'ANSWER', choices: ['yes', 'no'].join('\n'), description: 'Select Answer?'))
// Tried following, this also didn't work
//def ANSWER = script.input(message: 'Please provide parameters', ok: 'Next',
// parameters: script.choice(name: 'ANSWER', choices: ['yes', 'no'].join('\n'), description: 'Select Answer?'))
return ANSWER
}
}
// Used the above class as in the Jenkinsfile
import com.example
def helper = new Helper(this);
pipeline {
stages {
stage("UserInputs") {
steps {
script {
env.ANSWER=helper.getAnswer(env)
}
}
}
}
}
// --------------- Helper.groovy ends -----------
// var/getAnswer.groovy
def call() {
def ANSWER = input(message: 'Please provide parameters', ok: 'Next',
parameters: choice(name: 'ANSWER', choices: ['yes', 'no'].join('\n'), description: 'Select Answer?'))
return ANSWER
}
// Used it as below in Jenkins file, this also didn't work
pipeline {
stages {
stage("UserInputs") {
steps {
script {
env.ANSWER = getAnswer()
}
}
}
}
}
// ------ getAnswer.groovyy ends here -----------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment