Jenkins将阶段计算的值传递给shell脚本

时间:2018-06-09 07:14:26

标签: jenkins groovy jenkins-pipeline

我试图通过Jenkins声明性管道将提交消息连接字符串传递给shell脚本。我可以得到连接的字符串,但我无法弄清楚如何将它传递给我的shell脚本。环境变量在我的shell脚本中是可读的,但是我不能在我的阶段之外设置环境变量,因为舞台是我定义我的git连接的地方,如果我在舞台中设置它,它不会更新我调用的环境变量在我的帖子部分。如何将changeString的值传递给我的bash脚本(成功)?

pipeline { 
    agent any 
    environment {
        CHANGE_STRING = 'Initial default value.'
    }
    stages {
        stage('Build') { 
            environment {
                CHANGE_STRING = 'This change is only available in this stage and not in my shell script'
            } 
            steps { 
                echo 'Build stage'
                git branch: 'develop',
                credentialsId: 'blah',
                url: 'blah.git'
                sh """ 
                npm install
                """ 
                script{                   
                    MAX_MSG_LEN = 100
                    def changeString = ""

                    def changeLogSets = currentBuild.changeSets
                    for (int i = 0; i < changeLogSets.size(); i++) {
                        def entries = changeLogSets[i].items
                        for (int j = 0; j < entries.length; j++) {
                            def entry = entries[j]
                            truncated_msg = entry.msg.take(MAX_MSG_LEN)
                            changeString += " - ${truncated_msg} [${entry.author}]\n"
                        }
                    }

                    if (!changeString) {
                        changeString = " - No new changes"
                    }
                    //I would like to set CHANGE_STRING here
                }
            }
        }
    }
    post {
        success {
            echo 'Successfull build'
            sh """ 
            bash /var/lib/jenkins/jobs/my-project/hooks/onsuccess
            """ 
        }
    }
}

1 个答案:

答案 0 :(得分:2)

如果要从script步骤导出环境变量并在当前阶段之外访问它,则必须使用未在全局或本地environment {}块中指定的变量名称。请考虑以下示例:

pipeline { 
    agent any 
    environment {
        IMMUTABLE_VARIABLE = 'my value'
    }
    stages {
        stage('Build') { 
            steps { 
                script{                   
                    def random = new Random()
                    if (random.nextInt(2) == 1) {
                        env.CHANGE_STRING = "Lorem ipsum dolor sit amet"
                    } else {
                        env.CHANGE_STRING = "Foo Bar"
                    }

                    env.IMMUTABLE_VARIABLE = 'a new value'

                    echo "IMMUTABLE_VARIABLE = ${env.IMMUTABLE_VARIABLE}"
                }
            }
        }
    }
    post {
        success {
            echo 'Successfull build'
            sh '''
            echo $CHANGE_STRING
            echo "IMMUTABLE_VARIABLE = $IMMUTABLE_VARIABLE"
            '''
        }
    }
}

这只是管道脚本的简化。当我运行它时,我看到以下控制台输出:

[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
IMMUTABLE_VARIABLE = my value
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Successfull build
[Pipeline] sh
[test-pipeline] Running shell script
+ echo Foo Bar
Foo Bar
+ echo IMMUTABLE_VARIABLE = my value
IMMUTABLE_VARIABLE = my value
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

后成功块中的shell脚本在第一行打印Foo Bar,在第二行打印IMMUTABLE_VARIABLE = my value。另请注意,即使我已明确尝试覆盖

env.IMMUTABLE_VARIABLE = 'a new value'

它没有任何效果,当我做的时候

echo "IMMUTABLE_VARIABLE = ${env.IMMUTABLE_VARIABLE}"

它只是回显了environment {}块的初始值:

IMMUTABLE_VARIABLE = my value

希望它有所帮助。