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