user3296316
user3296316

Reputation: 157

Comparing strings in pipeline plugin jenkins

I am building a process-pipeline with the pipeline-plugin for jenkins. I have some jobs which I have to trigger. To decide which next step I have to use, I write a return code in a file. After this, I read this file and make a decision. But the comparing does not work.

def returnCode = readFile 'return'

//IP in Datenbank
if ( returnCode == "1" ){
}
else{
}

When I try echo returnCode the script prints "1" on the console, but it always goes into the else-part. What is wrong with the comparing?

Upvotes: 6

Views: 23179

Answers (3)

rakeshz
rakeshz

Reputation: 377

I to have faced same issue. The problem is with new line or space character. For example:

status = getstat()
if (status == "started"){
  println("in if")
} else {
  println("in else")
}

def getstat() {
  def out = new StringBuilder(), err = new StringBuilder()
    status = 'echo started'.execute()
  status.consumeProcessOutput(out, err)
  println "out> $out err> $err"
  println(out)
  return out
}

o/p is :

out> started
 err> 
started

in else

Now just change the return value to

return out.replaceAll("[\n\r]", "");

Now o/p is:

out> started
 err> 
started

in if

Upvotes: 1

David M. Karr
David M. Karr

Reputation: 15215

One of the comments mentioned using "trim()". This is very likely the problem.

Whenever I print strings while debugging, I always print them like this:

println "label[${variable}]"

If you see in the output the value on one line and the "]" on the next line (or just with additional spaces), that shows that you have to "trim()" the value.

Upvotes: 8

Mor Lajb
Mor Lajb

Reputation: 2636

PLS check this one

node ('master'){
    def returnCode = 1
    println (returnCode)

    if( returnCode == 1 ) {
        sh 'echo 1 !!'
    }
    else{
        sh 'echo not 1 !!!'
    }
}

Upvotes: -2

Related Questions