0

In my Jenkinsfile, I have 2 stages: Pre Live and Live. I ask the user for input on stage Pre Live to know whether a deploy should be done to a pre live environment, and then on stage Live, I ask the user again for input to know whether to do a deploy to a live environment or not.

I managed to implement this. This is how the code looks:

stage("Pre Live") {
  input {
    message 'Deploy to Pre Live?'
    parameters {
      booleanParam(name: 'RELEASE_PRE_LIVE', defaultValue: false)
    }
  }

  when {
    beforeInput false
    expression {
      return RELEASE_PRE_LIVE.toBoolean()
    }
  }

  steps {
    // ...
  }
}

stage("Live") {
  input {
    message 'Deploy to Live?'
    parameters {
      booleanParam(name: 'RELEASE_LIVE', defaultValue: false)
    }
  }

  when {
    beforeInput false
    expression {
      return RELEASE_LIVE.toBoolean()
    }
  }

  steps {
    // ...
  }
}

What I am not able to do, however, is too keep all of this logic, but also only ask for input on the stage Live if the the previous stage (Pre Live) was executed. Normally, this could be done through the when directive in the Live stage, but the problem is that I need my when directive in that stage to evaluate after the input, because I need the input value to know if the user wants to deploy to live or not, but I also don't want to unnecessarily wait for input on this stage if Pre Live was never ran, because it doesn't make sense.

Tiago Silva
  • 455
  • 5
  • 20

2 Answers2

0

Have you considered using build step that would trigger another pipeline and splitting your pipeline into two pipelines instead of one?

Here is an example article for declarative pipeline syntax: https://support.cloudbees.com/hc/en-us/articles/360019828412-Pipeline-How-to-write-a-declarative-pipeline-to-invoke-another-job

Toni Nurmi
  • 355
  • 4
  • 20
0

I believe you can implement an additional check by setting then passing a variable from the Pre-Live stage to the Live stage. Then acting on that variable in the Live stage.

You already have this next part down, but I thought it would be good here for context.

Evaluating when before the input directive

By default, the when condition for a stage will not be evaluated before the input, if one is defined. However, this can be changed by specifying the beforeInput option within the when block. If beforeInput is set to true, the when condition will be evaluated first, and the input will only be entered if the when condition evaluates to true.

beforeInput true takes precedence over beforeAgent true.

From: https://jenkins.io/doc/book/pipeline/syntax/#when

Mark Han
  • 2,785
  • 2
  • 16
  • 31
  • I know this. The problem is that if I evaluate the `when` directive before the `input`, then I won't be able to use the `input` as a condition for the execution of the Live stage. – Tiago Silva Jan 09 '20 at 16:05
  • @TiagoSilva Wouldn't it be nice if you could have two when{} blocks? One that evaluates beforeInput and one that evaluates after? PS - I keep thinking this problem is so much simpler to solve with a scripted pipeline. I am less familiar with declarative. – Mark Han Jan 09 '20 at 16:24
  • @TiagoSilva Also, you may be able to set a variable in Pre-Live `hasExecuted` and use `when { allOf { expression1 ; hasExecuted == true } }` in the Live stage `when` directive – Mark Han Jan 09 '20 at 16:30