Add Input with timeout in jenkins pipeline

13,868

First thing to note, it is good practice to not allocate a node for input, or it will hold that node until the timeout or until the input is processed.

Wrap your input in a timeout:

node {
    stage('dev') {
        build job: '11', propagate: false
    }
    stage('test') {
        build job: '12', propagate: false
    }
}
timeout(time: 60, unit: 'SECONDS') {
     input 'Do you want to proceed to the Deployment?'
}
node {
    stage('prod') {
        build job: '13', propagate: false
    }
}

You should see something like this in your output:

[Pipeline] // node
[Pipeline] timeout
Timeout set to expire in 1 min 0 sec
[Pipeline] {
Do you want to proceed to the Deployment?
Proceed or Abort
[Pipeline] input

An alternate solution would be to use the milestone plugin:

node {
    stage('dev') {
        build job: '11', propagate: false
    }
    stage('test') {
        build job: '12', propagate: false
    }
}
input 'Do you want to proceed to the Deployment?'
milestone 1
node {
    stage('prod') {
        build job: '13', propagate: false
    }
}

Rather than timeout, this will wait for input indefinitely, but if another build runs and passes the milestone 1, then any previous builds that have not yet passed milestone 1 will be aborted.

Or you could wrap it all in a timeout to give useful flexibility to the input timeout.

timeout ( time: 24, unit: "HOURS" )  {
    input 'Do you want to proceed to the Deployment?'
    milestone 1
}
Share:
13,868
chandra
Author by

chandra

Updated on June 12, 2022

Comments

  • chandra
    chandra almost 2 years

    Currently, with this script, the pipeline is waiting for user input indefinitely, i want to be able to change it like

    if userinput is proceed, then proceed, if userinput is abort then abort, if no user input in 60 seconds then abort. how do i do that ? what changes do i make to this script ?

    node {
    stage('dev') {
    build job: '11', propagate: false
    }
    stage('test') {
    build job: '12', propagate: false
    }
    input 'Do you want to proceed to the Deployment?'
    stage('prod') {
    build job: '13', propagate: false
    }
    }