代码之家  ›  专栏  ›  技术社区  ›  XorX

带有sh定义变量的Jenkins管道“when”条件

  •  0
  • XorX  · 技术社区  · 8 年前

    pipeline {
        agent { label 'php71' }
        stages {
            stage('Prepare CI...') {
                steps{
                    sh '''
                        # Get the comment that was made on the PR
                        COMMENT=`echo $payload | jq .comment.body | tr -d '"'`
                        if [ "$COMMENT" = ":repeat: Jenkins" ]; then
                            BUILD="build"
                        fi
                    '''
                }
            }
            stage('Build Pre Envrionment') {
                agent { label 'php71' }
                when {
                    expression { return $BUILD == "build" }
                }
                steps('Build') {
                    sh '''
                        echo $BUILD
                        echo $COMMENT
                    '''
                }
            }
        }
    }
    

    这给了我一个错误: groovy.lang.MissingPropertyException:没有此类属性:$BUILD for class:groovy.lang.Binding

    谢谢您!

    0 回复  |  直到 8 年前
        1
  •  1
  •   Ed Randall    6 年前

    可能使用Jenkins脚本管道,它比声明性管道更灵活。 打印sh脚本中的值,并使用returnStdout使其可用于管道脚本。看到了吗 How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)? 更多细节。