Jenkins boolean parameter is always true in groovy

I have a Jenkins job that calls a groovy script, and a groovy script uses Jenkins parameters to work it. I can get all parameters without problems except the boolean parameter. Boolean parameter is a flag in Jenkins

unselected boolean parameter

I read the jenkins parameter in groovy as follows:

boolean libraryBranch = config.get('library_branch_feature'); 

Now when I print the libraryBranch variable

 out.println "-- Library branch feature?: " + libraryBranch.toString(); 

I get the following print string:

- Library branch function ?: true

Thus, it does not matter if the Jenkins boolean parameter is selected or not. I always have the boolean value "true" in Groovy. All other (string) parameters inside the same task are read without problems.

Can someone help me with this problem?

EDIT

Good. I decided to try and get the code in several other ways and with a tier to find a good solution:

 Boolean libraryBranch = build.buildVariableResolver.resolve("library_branch_feature"); String libraryBranchString = build.buildVariableResolver.resolve("library_branch_feature").toString(); Boolean libraryBranchStringAsBoolean = build.buildVariableResolver.resolve("library_branch_feature") as Boolean; 

Then the following variables are output:

 out.println "-- Library branch feature?: " + libraryBranch; out.println "-- Library branch feature to String: " + libraryBranch.toString(); out.println "-- Library branch feature to String: " + libraryBranch.toString(); out.println "-- Library branch feature as String: " + libraryBranchString; out.println "-- Library branch feature String as Boolean: " + libraryBranchStringAsBoolean; 

The output of the above prints is posted below:

 -- Library branch feature?: true -- Library branch feature to String: true -- Library branch feature to String: true -- Library branch feature as String: false -- Library branch feature String as Boolean: true 

Thus, the only way so far is to have the correct logical value as false, not to turn it into a logical one, but simply read it as a string and use it as a string.

I would prefer to use it as a logical one, although any suggestions on this are still appreciated.

+5
source share
3 answers

The answer is to read the Jenkins parameter as a String, and then convert it to a boolean using the .toBoolean () method; Thus, my library is now installed as follows:

 boolean libraryBranch = build.buildVariableResolver.resolve("library_branch_feature").toString().toBoolean(); 
+6
source

Just out of curiosity, I tried the following:

Set the testmode variable to boolean in the pipeline configuration. Run the task with the testmode false (unchecked) value set. In the script pipeline, run the following code immediately after input:

  testmode=testmode.toBoolean() if (testmode==false) { print "testmode tests true"}else{print "testmode tests false"} 

you will see the result of "testmode tests false".

To test my claim that the test method of the variable "boolean" ACTUALLY is a string when it comes in, I tried:

  testmode=testmode.toBoolean() testmode=testmode.toBoolean() 

and it exploded on the 2nd 'toBoolean'. I will give you an error message ...

So, I argue that testmode comes as a string of "false" and not as a logical one (whether due to promotion, demotion, forcing, etc.).

So, if this is a boolean parameter, and you really wanted to treat it as a logical one, and you do not need to say “if (booleanvar ==" true "), then convert it to a logical one, as indicated above, and you did.

+1
source

I ran into the same problem while reading java properties - returned booleans are always true. So, let's say I have a settings file with the debugging=false property. This test will fail:

 class ConfigurationTest extends GroovyTestCase { void testProcessConfiguration() { Properties properties=new Properties() FileReader fileReader=new FileReader('settings') properties.load(fileReader) boolean debug=properties.getOrDefault('debugging',false) assert !debug fileReader.close() } } 

But if you do this properties.getOrDefault('debugging',false).toString().toBoolean() , it will return the correct value. Probably because of coercion?

0
source

Source: https://habr.com/ru/post/1205279/


All Articles