用于执行 Jenkins shell 脚本的 Git 环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24746051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:18:10  来源:igfitidea点击:

Git environment variables for use in executing Jenkins shell scripts

gitjenkinsjenkins-pluginscloudbees

提问by user119680

Hope someone can help - I have the following plugin installed in my Jenkins instance - but I don't seem to be able to get hold of the Git variables below. I need these to complete our CI setup. Is there something that I'm missing?

希望有人可以提供帮助 - 我在我的 Jenkins 实例中安装了以下插件 - 但我似乎无法掌握下面的 Git 变量。我需要这些来完成我们的 CI 设置。有什么我想念的吗?

I'm passing them in like:

我正在传递它们,例如:

ssh [email protected] "./build-pass.sh $GIT_BRANCH"

ssh [email protected] "./build-pass.sh $GIT_BRANCH"

https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin

Environment variables

环境变量

The git plugin sets several environment variables you can use in your scripts:

git 插件设置了几个可以在脚本中使用的环境变量:

  • GIT_COMMIT - SHA of the current

  • GIT_BRANCH - Name of the branch currently being used, e.g. "master" or "origin/foo"

  • GIT_AUTHOR_EMAIL - Committer/Author Email

  • GIT_COMMITTER_EMAIL - Committer/Author Email

  • GIT_COMMIT - 当前的 SHA

  • GIT_BRANCH - 当前使用的分支名称,例如“master”或“origin/foo”

  • GIT_AUTHOR_EMAIL - 提交者/作者电子邮件

  • GIT_COMMITTER_EMAIL - 提交者/作者电子邮件

回答by user119680

Ok I cracked this one myself.

好吧,我自己破解了这个。

To see all environment variables available add to the Execute shell field:

要查看所有可用的环境变量,请添加到 Execute shell 字段:

printenv

Console output will then show all the available vars.

然后控制台输出将显示所有可用的变量。

回答by Marcus Kosek

Please also have a look at: How can I access the last built revision according to jenkins?

另请查看: 如何根据詹金斯访问最后构建的修订版?

The part with the env-vars url was really helpful:

带有 env-vars url 的部分真的很有帮助:

Lastly, to see a less comprehensive list of available environment variables, you can also just go to this url: http://[your-jenkins-domain-and-port]/env-vars.html

最后,要查看不太全面的可用环境变量列表,您也可以访问以下网址:http://[your-jenkins-domain-and-port]/env-vars.html

回答by Cameron McKenzie

Similarly you could just echo them out:

同样,您可以将它们回显出来:

echo GIT_COMMIT %GIT_COMMIT% 
echo GIT_BRANCH %GIT_BRANCH%

enter image description here

在此处输入图片说明

Here's a quick Jenkins Git configuration tutorialon the Git environment variables topic that uses the Jenkins Git configuration described in the image.

这是关于 Git 环境变量主题的 Jenkins Git 配置快速教程,该教程使用图像中描述的 Jenkins Git 配置。