Home > Software design >  Jenkins boolean expression not evaluating as expected
Jenkins boolean expression not evaluating as expected

Time:09-23

I'm working on a Jenkins job that will allow users to run tasks against the the load balancers in our development and production environments, and there is a requirement for an "are you sure?" check for jobs run against the production environment.

I've added a simple, annoying checkbox as below, but it always acts as if the box is not checked.

pipeline {
  parameters {
    choice(
      name: 'ANSIBLE_LB_ENV',
      choices: ['---', 'dev', 'prod'],
      description: 'Environment against which to run the LB config.'
    )
    booleanParam(
      name: 'ANSIBLE_LB_SECRET_BUTTON',
      defaultValue: false,
      description: 'Secret Button.'
    )
  }

  stages {
    stage('sanityCheck') {
      steps {
        script {
          echo "ANSIBLE_LB_ENV: ${ANSIBLE_LB_ENV}"
          echo "ANSIBLE_LB_SECRET_BUTTON: ${ANSIBLE_LB_SECRET_BUTTON}"
          if( ANSIBLE_LB_ENV == '---' ) {
            currentBuild.result = 'ABORTED'
            error("You must select a valid environment.")
          }
          if( ( ANSIBLE_LB_ENV == 'prod' ) && ( ANSIBLE_LB_SECRET_BUTTON != true ) ) {
            currentBuild.result = 'ABORTED'
            error("If you want to touch production you have to click the secret button.")
          }
        }
      }
    }
  }
}

Example output:

14:51:43  ANSIBLE_LB_ENV: prod
14:51:43  ANSIBLE_LB_SECRET_BUTTON: true
ERROR: If you want to touch production you have to click the secret button.
Finished: ABORTED

I've made if( ( ANSIBLE_LB_ENV == 'prod' ) && ( ANSIBLE_LB_SECRET_BUTTON != true ) ) {...} as explicitly-defined as I can, but it just won't let me do anything when prod is selected.

CodePudding user response:

ANSIBLE_LB_SECRET_BUTTON is returned as a String, not a Boolean :)

So simply,

if( ( ANSIBLE_LB_ENV == 'prod' ) && ( ANSIBLE_LB_SECRET_BUTTON != "true" ) ) {...}
  • Related