首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Jenkins管道后阶段无法传递变量

在Jenkins管道后阶段无法传递变量
EN

Stack Overflow用户
提问于 2019-12-09 15:08:16
回答 1查看 927关注 0票数 0

场景:我无法在后期步骤中传递参数。下面是错误 post {Script{ sh“count=ca changes.txt | wc -l echo ${count}如果[ ${count} == 0 ];然后回显‘条件成功’否则回送‘条件而不是成功’fi”“}‘}’

错误:

16:24:38 email-2运行shell脚本16:24:39 ++ cat changes.txt 16:24:39 ++ wc -l 16:24:39 + count=7管道}管道/脚本错误执行成功后条件: groovy.lang.MissingPropertyException: No此类属性:$count表示类: WorkflowScript at org.codehaus。groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-09 20:38:42

这应该是可行的:

代码语言:javascript
复制
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
                       """
                }
            }
        }
    }
}

输出:

代码语言:javascript
复制
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 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59251635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档