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.
CodePudding user response:
Here is a simplified version of a Pipeline. See the explanation for your steps.
- Inorder to build with parameters, you can refer the following block. Here I'm using a String parameter, but you can use different input parameter types based on your requirement. Once the value is entered it will be assigned to a variable called
TEST_STRING
parameters {
string(name: "TEST_STRING", defaultValue: "default", trim: true, description: "Sample string parameter")
}
- In order to get the current date you can use the following script. Here from within the shell script, you can print the Database date to the standard out and then return it. (Note: Without knowing your shell logic, I can't give an exact solution, you may want to tweak this accordingly). As you can see below, the date will be returned and assigned to a variable called
date
. In the below example instead ofecho $(date)
you can add your shell logic. Also note thereturnStdout: true
flag will tell Jenkins to return the StandardOut of the shell step.
def date = sh(script: 'echo $(date)', returnStdout: true)
- Once you receive the Database date you can do the check like below. If the Data is greater you can prompt the user for input. You need to implement the
CheckDate(date)
function according to the output you are getting from the DB. SimplyCheckDate(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!'
}
- Next you can execute the second script if the user clicked on
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.