Hi,
I'm trying to find documentation on how global variables in a Jenkinsfile work. I've found bits and pieces, but not a single authoritative location on the programming model.
Any pointers?
In particular, I've managed to get the following code(paraphrased from our project) to work by googling, trial and error, but I'd like to understand the underlying rules or I fear that my code will break at any moment and I won't have a clue why.
The snippet below is intended to collect information about the build in a map that I transform to JSON and email to our mailing list.
// What's this 'Field' thing and why do I need it? What does it mean to leave it out? Sometimes I want global variables
// to be visible to all nodes, other times I want changes to be local to the node.
@Field
// Eclipse Groovy IDE will indent 'def buildInfo' below, I think Eclipse is trying to tell me something, but I don't know what.
def buildInfo
// I have to put assignment of global variables on a separate line, otherwise they don't always work
buildInfo = [:]
// If we don't make it to the end
buildInfo['success'] = false
def setupbuilds() {
node('master') {
// We need to do a bit of work on a real node in order to find out what builds we need to run.
builds = [somebuild: stage('foo') {
buildInfo['blah'] = 123
if (buildInfo['moreblah']) {
// This could be interesting.... Race conditions?
}
},
somebuild: stage('foo') {
// amazingly this works! How does the information flow from
// the node back to the flywheel executor? How are partial updates to a
// map implemented?
buildInfo['moreblah'] = 123
if (buildInfo['blah']) {
// This could be interesting.... Race conditions?
}
}]
}
}
try {
setupbuild()
parallel builds
buildInfo['success'] = true
} finally {
// Here I can email buildInfo to our mailing list and we're going to put together some code that is going to format it nicely
}