Since the git plugin works fine for declarative scripts you can use a declarative script to get the git environment variables and set them for the scripted section to use.
So in a nutshell:
pipeline {
environment {
gitCommit = "${env.GIT_COMMIT}"
}
agent {
label <agent>
}
stages {
stage('SetEnvironmentProperties') {
agent {
label <agent>
}
steps {
env.setProperty("GIT_COMMIT", "$gitCommit")
}
}
}
}
node(<agent>) {
echo "Using Git Commit Hash ${env.GIT_COMMIT}"
}
This is absolutely a hack, but it works for us. We're hoping that the issue get's resolved soon and we can simply remove the declarative section altogether.
I believe this works because this creates a Declarative: Checkout SCM stage
where the environment variables become accessible in subsequent pipeline stages. This is equivalent, I think, to what is described in this answer. What I'm not so clear on is why resetting them would persist them into the scripted stages. Which is why this is a hack...