Search code examples
jenkinsjenkins-pipelinejenkins-job-dsl

Jenkins DSL workflow for performing subtraction


I have written a jenkins script where i want build no and previous build no.For that i am performing a subtraction operation. i want to perform a subtraction between two variables in jenkins DSL script.

But i am not able to subtract.Ex i want to perform a=b-1 ....

the result what i am getting is always value of b.it does not perform subtraction.

It just assigns value of b to a.

Below is want i want :

build_num = "3"

pre_build_num = build_num-"1"

and result what i want should be pre_build_num = "2".

But i am getting pre_build_num = "3".

any help?


Solution

  • The problem is that your build_num variable is a string and the minus operator for strings removes a part of a string. It doesn't subtract numbers.

    Some examples:

    "three" - "th" = "ree"
    "three" - "ree" = "th"`
    

    To subtract your build_num variable you have to convert it to an integer first:

    pre_build_num = (build_num as int) - 1