Jenkins Git环境变量未在管道中设置 [英] Jenkins Git Environment variables not set in Pipeline

查看:450
本文介绍了Jenkins Git环境变量未在管道中设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Jenkins管道脚本中读取一些应该由Git插件设置的环境变量,但是似乎未设置它们,因为当我尝试在脚本中使用时,它的值是空字符串,并且如果我使用sh 'printenv',我可以看到它们没有被设置.

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.

有什么主意吗?

推荐答案

每页:

http://JenkinsURL/pipeline-syntax/globals :

特定于SCM的变量(例如GIT_COMMIT)不会自动 定义为环境变量;而是可以使用返回值 结帐步骤.

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.

我相信这应该在Jenkins 2.60中解决:

This is supposed to be resolved in Jenkins 2.60, I believe:

https://plugins.jenkins.io/pipeline-model-definition

请参阅JENKINS-45198的项目

See the item for JENKINS-45198

您可以通过在外壳中运行适当的git命令并将其分配给变量来解决:

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 )

在声明性管道中,您必须将其包装在script {}块中,并且可能在管道之外声明变量以获取适当的作用域.

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.

这篇关于Jenkins Git环境变量未在管道中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆