0

I am getting mad about the following pipeline code, as it does not do what I expect. Does anyone have an idea what I am doing wrong?

Here is my pipeline code to define the parameter


pipeline {
    agent any
    parameters { booleanParam(name: 'bForceCheckout', defaultValue: false, description: '') }

    ...

And here the stage itself


stage('SVN Checkout') {
   // Get code from SVN repository
   steps {
      script {

         // If project is not yet checked out, setup checkout structure, i.e. which
         // folders will be checked out and which will not be checked out
         retry (5) {
            try {
               def svnInfoError = bat (returnStatus: true, script: "svn info ${projectName}")
               // bForceCheckout has to be set as parameter in the job

               println "---> " + ((svnInfoError != 0) || bForceCheckout)

               if ((svnInfoError != 0) || bForceCheckout) {


                  println "svnInfoError:  " + svnInfoError
                  println "bForceCheckout: " + bForceCheckout

                  timeout(activity: true, time: 90, unit: 'MINUTES') {

                     // Clean up
                     deleteDir ()

                     ... some SVN related stuff ...
                  }
               }
            } catch (Exception ex) {
               // Clean up
               deleteDir ()

               println(ex.toString());

               error ("SVN checkout: directory structure could not be set up")
            }
         }
      }
   }
}

And here is the console output. As you can see, svnInfoError and bForceCheckout are 0 / false, but the part in the if condition still get executed...

12:51:22  
[Pipeline] echo
12:51:22  --->false
[Pipeline] echo
12:51:23  svnInfoError:  0
[Pipeline] echo
12:51:23  bForceCheckout: false
[Pipeline] timeout
12:51:23  Timeout set to expire after 1 hr 30 min without activity
[Pipeline] {
[Pipeline] deleteDir

[Pipeline] bat
12:55:21  

nogenius
  • 574
  • 1
  • 6
  • 18
  • 1
    it's possible that you have a string `"false"` in the variable `bForceCheckout`. in this case the code will work according to your log – daggett May 10 '19 at 12:15
  • Yeah I would agree that this is most likely a type casting issue. They are both possibly cast as String instead of Integer and Boolean respectively. – Matthew Schuchard May 10 '19 at 13:53
  • I will check, but please have a look at the _println "--> " ...._ part and the corresponding output. It seems that it IS evaluated correctly. – nogenius May 11 '19 at 13:29

1 Answers1

0

Thank you guys, it did turn out to be an issue of the data type. I also now found the following question leading to the same answer: booleanParam in jenkins dsl

Defining a parameter as follows will still give you a variable of type string.

parameters { booleanParam(name: 'bForceCheckout', defaultValue: false, description: '') }

Checking the data type:

println bForceCheckout.getClass()

...will give you

09:06:03  class java.lang.String

In my opinion this is a bug, so I created a Jenkins issue: https://issues.jenkins-ci.org/browse/JENKINS-57499

For now I ended up changing the code above to the following (yes, I will do some renaming as the variable does not hold a boolean value), and now it works:

if ((svnInfoError != 0) || bForceCheckout.toBoolean()) {

EDIT: I found that actually you should use the following syntax to access parameters and this is working as expected:

if ((svnInfoError != 0) || params.bForceCheckout) {
nogenius
  • 574
  • 1
  • 6
  • 18