user517696
user517696

Reputation: 2672

How to use global variable in If condition in a Jenkinsfile?

I am new to Jenkins and am confused between variables. I have tried to write a Jenkinsfile where I am simply using an If condition to deploy a file. Following is the file:

def checker = "Trial"

pipeline{
   agent any

   stages{
     stage('Stage 1'){
     steps{
       script{
         checker = sh(returnStdout: true, script: 'echo "Trial" ')
         if (checker == 'Trial'){ echo "Hello"}
         else {echo "Hi"}
       }
     }
    }
}
}

The output should ideally be "Hello", however I get "Hi" always. How do I get the If condition working?? Thanks

Upvotes: 1

Views: 1200

Answers (1)

yong
yong

Reputation: 13712

You missed the trim() to remove the newline at the end of output of the bash.

checker = sh(returnStdout: true, script: 'echo "Trial" ').trim()

Upvotes: 1

Related Questions