2

I have a jenkins groovy script like this:

    freeStyleJob(“test”) {
        properties { githubProjectUrl(‘…’) }
        description(‘’’job description’’’.stripMargin('|'))

        logRotator{ numToKeep(100) }

        parameters {
            stringParam(’STRINGP1’, "", “STRINGP1 description”)
            stringParam('STRINGP2’, "", “StringP2 description”)
            booleanParam(‘b1’, false)
            booleanParam(‘b2’, false)
            booleanParam(‘b3’, false)
            stringParam("EMAIL_LIST", "", "Emails")
        }

        scm {
            github(‘repo’, '${STRINGP1}', 'git', ‘giturl’)
        }

        steps {
            shell '''|#!/bin/bash
                |ARGS=""
                |fi
                |if [[ ‘${b1}’ ]]; then
                |   ARGS=$ARGS" —-p b1”
                |fi
                |if [[ ‘${b2}’ ]]; then
                |   OS_ARGS=$ARGS" —-p b2”
                |fi
                |if [[ ‘${b3}’ ]]; then
                |   ARGS=$ARGS" —-p b3”
                |fi                
                |echo ${ARGS}'''.stripMargin('|')

        }

        publishers {
            archiveArtifacts {
                pattern(‘pattern’)
            }

            extendedEmail {
                ....
                }
            }

        }

    ....
  }

After the creation of job no matter whether user checks or unchecks the boolean parameter in the UI, the value for ARGS would be always "--p b1 ---p b2 --p b3". It means that the three if that exist in the shell script will be always evaluated to true. Why does this happen?

Rey Sa
  • 21
  • 1
  • 1
  • 3

3 Answers3

10

Parameters are available from both env and params. When you access them as $b1 you are getting them from env, not params.

All environmental variables are strings by their nature so when you access params as environmental variables, they are always strings.

If you want to access them as they are typed, use params:

script {
  assert env.b1 instanceof String
  assert params.b1 instanceof Boolean
}
Dan Dean
  • 121
  • 1
  • 4
5

At least for Pipeline scripts, Boolean parameter are in reality strings. So I do the following:

parameterAsBoolean = (MY_PARAMETER == "true")
jherb
  • 392
  • 2
  • 11
0

Have you tried running your shell script standalone? Just to make sure it acts as you want?

I think your syntax might be wrong, you values will always be valid since they exist, please check this one as reference: How to declare and use boolean variables in shell script?

Community
  • 1
  • 1
MaTePe
  • 936
  • 1
  • 6
  • 11