Jenkins Pipeline: "input" step blocks executor

Theodor picture Theodor · Jun 15, 2016 · Viewed 34.1k times · Source

After going through the pipeline and Jenkinsfile documentation, I am a bit confused on how to create a Stage -> Production pipeline.

One way is to use the input step like

node() {
  stage 'Build to Stage'
  sh '# ...'

  input 'Deploy to Production'
  stage 'Build to Production'
  sh '# ...'
}

This seems a bit clunky, as this will block an executor all the time until you want to deploy to production. Is there any alternative way of being able to deploy to production, from Jenkins.

Answer

StephenKing picture StephenKing · Jun 21, 2016

EDIT (Oct 2016): Please see my other answer "Use milestone and lock" below, which includes recently introduced features.

Use timeout Step

As first option, you can wrap your sh step into a timeout step.

node() {
  stage 'Build to Stage' {
    sh '# ...'
  }

  stage 'Promotion' {
    timeout(time: 1, unit: 'HOURS') {
      input 'Deploy to Production?'
    }
  }

  stage 'Deploy to Production' {
    sh '# ...'
  }
}

This stops the build after the timeout.

Move input Step to Flyweight Executor

Another option is to not allocate a heavyweight executor for the input step. You can do this by using the input step outside of the node block, like this:

stage 'Build to Stage' {
  node {
      sh "echo building"
      stash 'complete-workspace'
  }
}

stage 'Promotion' {
  input 'Deploy to Production?'
}

stage 'Deploy to Production' {
  node {
    unstash 'complete-workspace'
    sh "echo deploying"
  }
}

This is was probably the more elegant way, but can still be combined with the timeout step.

EDIT: As pointed out by @amuniz, you have to stash/unstash the contents of the workspace, as different nodes respectively workspace directories might be allocated for the two node steps.