lkisac
lkisac

Reputation: 2137

Jenkins Declarative Pipeline - How to add input step only if when condition is met

I have an input stage in my pipeline I would like to have run only if a certain condition is met.

    stage ('input stage') {
        agent none
        when {
            expression {
                condition1 == "YES"
            }
        }
        input {
            message 'Proceed with this step?'
            submitter "${approvers}"
        }
        steps {
            echo "Proceeding with step..."
        }
    }

However the input stage appears to wait for the submitter approval even if the condition is not met.

I'm assuming this would be possible for stages with inputs. Is there an issue with the above code block?

Thanks in advance.

Upvotes: 2

Views: 1744

Answers (1)

zett42
zett42

Reputation: 27766

There is no "issue", it is just how the input directive of declarative pipeline works (emphasis mine):

The input directive on a stage allows you to prompt for input, using the input step. The stage will pause after any options have been applied, and before entering the agent block for that stage or evaluating the when condition of the stage. If the input is approved, the stage will then continue.

To check when condition before input, turn input into a step:

stage ('input stage') {
    agent none
    when {
        expression {
            condition1 == "YES"
        }
    }
    steps {
        input message: 'Proceed with this step?', submitter: "${approvers}"
        echo "Proceeding with step..."
    }
}

Upvotes: 5

Related Questions