Not able to read Git Environment variables Jenkins using Groovy Jenkinsfile
So, anyone else who has stumbled onto this Stackoverflow issue should be aware that this is a bug with the current pipeline situation.
Lots of discussion here: https://issues.jenkins-ci.org/browse/JENKINS-35230
Essentially the plugin is not able to correctly add environment variables due to some incompatibilities with pipeline.
This is fixed in Git plugin 3.3.1
Version 3.3.1 (Jun 23, 2017)
- Print first line of commit message in console log (JENKINS-38241)
- Allow scm steps to return revision (JENKINS-26100)
- Don't require crumb for POST to /git/notifyCommit even when CSRF is enabled (JENKINS-34350)
- Fix credentials tracking null pointer exception in pipeline library use (JENKINS-44640)
- Fix credentials tracking null pointer exception in git parameters use (JENKINS-44087)
This won't work due to lack of double quotes, missing curly braces, and missing env.
:
sh 'echo $BRANCH_NAME'
This works as expected in a Jenkinsfile:
node {
sh "echo ${env.BRANCH_NAME}"
}
Most of the pipeline examples I've found wrap the code in a "node" closure, which for some reason doesn't allow the Jenkins Git plugin to populate the environment variables.
However, if instead you wrap it like this, the environment variables are set properly:
pipeline {
agent {
label ('<AGENT>')
}
stages {
stage('<STAGE>') {
steps {
<CODE>
}
}
}
}
I'm not sure which closures are crucial here, but this formatting allowed me to access env variables such as env.GIT_COMMIT