Skip to main content

Pipeline: How to add an input step, with timeout, that continues if timeout is reached, using a default value

Comments

8 comments

  • Ronaldo Ronaldinho

    hi, sir and madam, 

    is it possible to 

    • if there is no objection from all stakeholders within a period, let's say 1 week, then it will automatically to deploy to production env.
    • if there is object, the stakeholder click 'reject' button on email to halt, the deployment to production env will be stopped.

    thank you very much!

    0
  • Austin Witt

    Ben,

    I have this same use-case. Using your "hack" for now.

    0
  • Josh Wand

    A slightly (but only barely) better solution is to ask for the class of the cause, rather than the user:

    timeout(10) { 
    try {
    // do stuff
    } catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
    cause = e.causes.get(0)
    if (cause instanceof org.jenkinsci.plugins.workflow.steps.TimeoutStepExecution.ExceededTimeout) {
    currentBuild.result = 'ABORTED'
    println('ExceededTimeout!')
    error('ExceededTimeout!')
    } else {
    error('error inside FlowInterruptedException: " + e)
    }
    }
    0
  • Ben Kolera

    Oh hi there Denys, fancy meeting you here! :)

    I held off submitting an issue because I thought that my use case was a little weird, but if you think that it's worthwhile submitting one I will do so. 

     

    Cheers,
    Ben

     

    0
  • Ben Kolera

    The issue with this is that when a user clicks the X button to cancel the build outside of the input rejection. I see that the user in the cause is actually SYSTEM rather than the user that cancelled it.

    What we see when the build is clicked through the X button in the running builds list. The log even says "Aborted by admin" but the user on the cause is actually SYSTEM. This effectively means that there is no good way to tell a timeout apart from a user abort outside of the input. 

    Started by user admin
    [Pipeline] timeout
    Timeout set to expire in 10 sec
    [Pipeline] {
    [Pipeline] input
    Do you approve?
    Proceed or Abort
    Aborted by admin
    [Pipeline] }
    [Pipeline] // timeout
    [Pipeline] echo
    Aborted by SYSTEM

    Vs the Timeout

    Started by user admin
    [Pipeline] timeout
    Timeout set to expire in 10 sec
    [Pipeline] {
    [Pipeline] input
    Do you approve?
    Proceed or Abort
    Cancelling nested steps due to timeout
    [Pipeline] }
    [Pipeline] // timeout
    [Pipeline] echo
    [Pipeline] echo
    Aborted by SYSTEM
    0
  • Denys Digtiar

    Hi Ben,

     

    Thank you for clarification and a workaround. Have you considered raising an Improvement request for this.

    https://wiki.jenkins-ci.org/display/JENKINS/How+to+report+an+issue

    https://issues.jenkins-ci.org/

    0
  • Ben Kolera

    I guess it is a weird requirement to actually want to proceed if the input times out rather than dying. If someone else has this requirement one day, we used this hack. :)

     

    userAborted = false
    startMillis = System.currentTimeMillis()
    timeoutMillis = 10000

    try {
      timeout(time: timeoutMillis, unit: 'MILLISECONDS') {
        input 'Do you approve?'
      }
    } catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
      cause = e.causes.get(0)
      echo "Aborted by " + cause.getUser().toString()
      if (cause.getUser().toString() != 'SYSTEM') {
        startMillis = System.currentTimeMillis()
      } else {
        endMillis = System.currentTimeMillis()
        if (endMillis - startMillis >= timeoutMillis) {
          echo "Approval timed out. Continuing with deployment."
        } else {
          userAborted = true
          echo "SYSTEM aborted, but looks like timeout period didn't complete. Aborting."
        }
      }
    }

    if (userAborted) {
      currentBuild.result = 'ABORTED'
    } else {
      currentBuild.result = 'SUCCESS'
      echo "Firing the missiles!"
    }

    0
  • Asuji 407

    Hi,

    I tried this working but it is blocking agent at manual input, do we have any solution to fix (not to block agent during manual input)

    0

Please sign in to leave a comment.

About CloudBees Support

Our Support Engineers are available to help with any questions or problems you may have with any of our products.