问题
I am trying to read some environment variables in Jenkins pipeline script that should be set by Git plugin, but it seems they are not set, because when I tried to use in script its value is empty string and also if I use sh 'printenv' I can see that they are not set.
Probably I am missing something but I cannot find what.
Any idea?
回答1:
Per this page:
http://JenkinsURL/pipeline-syntax/globals:
SCM-specific variables such as GIT_COMMIT are not automatically defined as environment variables; rather you can use the return value of the checkout step.
This is supposed to be resolved in Jenkins 2.60, I believe:
https://plugins.jenkins.io/pipeline-model-definition
See the item for JENKINS-45198
You can workaround by running the appropriate git commands in a shell and assigning them to a variable:
GIT_REVISION = sh( script: 'git rev-parse HEAD', returnStdout: true )
In a Declarative pipeline, you will have to wrap this in a script{} block, and probably declare the variable outside of your pipeline to get the appropriate scope.
来源:https://stackoverflow.com/questions/46317774/jenkins-git-environment-variables-not-set-in-pipeline