2

I'm trying to pass in a commit messages concatenated string to a shell script via a Jenkins declarative pipeline. I can get the concatenated string, but I cannot figure out how to pass it to my shell script. Environment variables are readable in my shell script, but I cannot set the Environment variable outside of my stages, as the stage is where I define my git connection, and if I set it in the stage it does not update the Environment variable that I call in my post section. How can I pass the value of changeString to my bash script (in success)?

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
            """ 
        }
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
edencorbin
  • 2,569
  • 5
  • 29
  • 44

1 Answers1

5

If you want to export environment variable from script step and access it outside the current stage you have to use a variable name that was not specified in global or local environment {} block. Consider following example:

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

This is just a simplification of your pipeline script. When I run it I see following console output:

[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

The shell script in post success block prints Foo Bar in the first line and IMMUTABLE_VARIABLE = my value in the second one. Also notice that even though I have explicitly try to override

env.IMMUTABLE_VARIABLE = 'a new value'

it didn't make any effect and when I did

echo "IMMUTABLE_VARIABLE = ${env.IMMUTABLE_VARIABLE}"

it simply echoed the initial value from environment {} block:

IMMUTABLE_VARIABLE = my value

Hope it helps.

Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
  • Perfect, helps and explains a few more related items. I tested and got the exact same results, and can now complete my pipeline workflow, thanks! – edencorbin Jun 09 '18 at 12:18