Vikram Kumar
Vikram Kumar

Reputation: 15

Unable to pass variables in Jenkins Pipeline Post Stage

Scenario: I am unable to pass parameter in post stage step . getting below error  ```` 
  post {  
     success {            script {
         sh """
         count=`ca changes.txt | wc -l`
         echo ${count}
         if [[ ${count} == 0 ]]; then
              echo 'condition success'  
          else
              echo 'condition not success'
          fi
          """
     }  

    } ```

error:

16:24:38 [email-2] Running shell script 16:24:39 ++ cat changes.txt 16:24:39 ++ wc -l 16:24:39 + count=7 [Pipeline] } [Pipeline] // script Error when executing success post condition: groovy.lang.MissingPropertyException: No such property: $count for class: WorkflowScript at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)

Upvotes: 0

Views: 1307

Answers (1)

MaratC
MaratC

Reputation: 6859

This should work:

pipeline {
    agent any
    stages {
        stage ('test') {
            steps {
                script {
                    sh """
                       echo "1" > changes.txt
                       count=\$(cat changes.txt | wc -l)
                       echo \$count
                       if [ \$count = 0 ]; then
                            echo 'condition success'  
                       else
                            echo 'condition not success'
                       fi
                       """
                }
            }
        }
    }
}

Output:

Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on jenkins in /home/user/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo 1
+ cat changes.txt
+ wc -l
+ count=1
+ echo 1
1
+ [ 1 = 0 ]
+ echo condition not success
condition not success
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS 

Upvotes: 1

Related Questions