如何在詹金斯的声明性管道中修改脚本块中定义的变量

时间:2018-08-09 15:51:58

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline

我在脚本中声明了一个变量 TENTATIVE_VERSION ,并且我需要使用执行脚本(或脚本本身的值)定义/修改它其他阶段),我该怎么做?我当前的脚本是这样的:

pipeline {
agent {
    label 'machine1'
}
stages {
    stage('Non-Parallel Stage') {
        agent{label "machine2"}
        steps {
            script {
                TENTATIVE_VERSION="1.0" // working
                // TENTATIVE_VERSION="sh echo 123" //not working
            }
        }
    }
    stage('Parallel Stage') {
        parallel {
            stage('A') {
                agent {label 'machine3'}
                steps {
                    echo "On other machine"
                    echo "${TENTATIVE_VERSION}"
                    build job: 'otherJob', parameters: [[$class: 'StringParameterValue', name: 'VERSION', value: "${TENTATIVE_VERSION}"],
                    [$class: 'StringParameterValue', name: 'RELEASE', value: '1']]
                }
            }
            stage('B') {
                agent {label "machine4"}
                steps {
                    script {
                        STATUS_S = "OK"
                    }
                    echo "On a machine"
                }
            }
            stage('C') {
                agent {label "machine5"}
                steps {
                    script {
                        STATUS_R = "OK"
                    }
                    echo "On a machine"
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

尝试以下操作:

pipeline {
agent {
    label 'machine1'
}
stages {
    stage('Non-Parallel Stage') {
        agent{label "machine2"}
        steps {
            script {
                TENTATIVE_VERSION = sh(returnStdout: true, script: "echo 123").trim()
            }
        }
    }
}
}