I'm googling about a hour but can't find an answer
I have a script which checked date in database and compare it with date in file. After if date in database are newer we need to ask user in Jenkins did he really want to proceed and build a job
Can we interact with user using groovy script in Jenkins?
My goal is: 1)User choose parameters and press Build with parameters 2)Shell script check date and if date in database newer run groovy script 3)Groovy script ask user : "Date in database are newer then in file. Do you really want to proceed?.Press yes or no" 4) And if user press "yes" - execute second shell script.
Here is a simplified version of a Pipeline. See the explanation for your steps.
TEST_STRING
parameters {
string(name: "TEST_STRING", defaultValue: "default", trim: true, description: "Sample string parameter")
}
date
. In the below example instead of echo $(date)
you can add your shell logic. Also note the returnStdout: true
flag will tell Jenkins to return the StandardOut of the shell step.def date = sh(script: 'echo $(date)', returnStdout: true)
CheckDate(date)
function according to the output you are getting from the DB. Simply CheckDate(date)
would return true if DB date is greater.if(CheckDate(date)) { //CheckDate here Simply
input id: 'Proceed', message: 'Date in database are newer then in file. Do you really want to proceed?', ok: 'Proceed!'
}
Proceed.
sh "echo 'Second Shell script!!! ${params.TEST_STRING}'"
Full Pipeline
pipeline {
agent any
parameters {
string(name: "TEST_STRING", defaultValue: "default", trim: true, description: "Sample string parameter")
}
stages {
stage('Test') {
steps {
script {
def date = sh(script: 'echo $(date)', returnStdout: true)
if(CheckDate(date)) { //CheckDate here
input id: 'Proceed', message: 'Date in database are newer then in file. Do you really want to proceed?', ok: 'Proceed!'
}
// If aborted you will not reach here.
sh "echo 'Second Shell script!!! ${params.TEST_STRING}'"
}
}
}
}
}
If you are using Build with Parameters then you can use ${params.VAR_NAME} to access the parameters.