无法在Jenkins Pipeline Post Stage中传递变量

时间:2019-12-09 15:08:16

标签: jenkins jenkins-pipeline jenkins-groovy

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
          """
     }  

    } ```
     
    

错误:

         

16:24:38 [email-2]运行shell脚本              16:24:39 ++猫change.txt              16:24:39 ++ wc -l              16:24:39 +计数= 7              [管道]}              [管道] //脚本              执行成功发布条件时出错:              groovy.lang.MissingPropertyException:无此类属性:类的$ count:WorkflowScript              在org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)

  

1 个答案:

答案 0 :(得分:1)

这应该有效:

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
                       """
                }
            }
        }
    }
}

输出:

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