Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Input with timeout in jenkins pipeline

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
}
}
like image 207
chandra Avatar asked Sep 18 '17 19:09

chandra


People also ask

How do I add a timeout in Jenkinsfile?

Global configuration. Go to Manage Jenkins and then Configure System . Under the item Global Build Time Out you can activate a global timeout which will be applied to any job. Choose your timeout strategy, the duration and add actions which should be executed at timeout.

What is the variable to be used in pipeline to define execution timeout period?

Examples: timeout(time: 10) // would lead to a timeout of 10 minutes (MINUTES is default value) timeout(time: 10, unit: 'SECONDS') // a 10 seconds timeout timeout(time: 10, activity: false, unit: 'MILLISECONDS')

How do you abort a Jenkins pipeline?

Pipeline jobs can be stopped by sending an HTTP POST request to URL endpoints of a build. BUILD ID URL/stop - aborts a Pipeline. BUILD ID URL/term - forcibly terminates a build (should only be used if stop does not work).

How do I add an input step with timeout in pipeline?

How do I add an input step, with timeout, that continues if timeout is reached, using a default value, in a Pipeline job? You can use a try catch block to achieve this. The following asks for input, with a timeout of 15 seconds. If the timeout is reached the default is true.

What is a timeout in Jenkins pipeline?

The declarative Jenkins Pipeline allows us to define timeout either at the pipeline level or the specific stage. This feature prevents Jenkins’s job from getting stuck. However, in some cases, we want to accept that one stage may timeout, but we want to keep the remaining stages running.

How to prompt a user for an interactive input in Jenkins?

In Jenkins declarative pipelines it is possible to prompt a user for an interactive input by creating the input step. For example, at some stage of the Jenkins pipeline you may want to ask a user to provide the credentials. The user input can be saved as an environment variable and used in the next steps.

How are downstream builds found in Jenkins?

Downstream builds are found using fingerprints of files. That is, a build that is triggered from a build isn't always considered downstream, but you need to fingerprint files used in builds to let Jenkins track them. Note: "Downstream build of" is applicable only to AbstractProject based projects (both upstream and downstream projects).


1 Answers

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
}
like image 57
Rob Hales Avatar answered Sep 28 '22 18:09

Rob Hales