简体   繁体   English

Jenkins管道中的环境变量

[英]Environment variable in Jenkins Pipeline

Is there any environment variable available for getting the Jenkins Pipeline Title? 是否有任何环境变量可用于获取Jenkins管道标题?

I know we can use $JOB_NAME to get title for a freestyle job, but is there anything that can be used for getting Pipeline name? 我知道我们可以使用$JOB_NAME来获得自由职业的职位,但是有什么可以用来获取管道名称的吗?

You can access the same environment variables from groovy using the same names (eg JOB_NAME or env.JOB_NAME ). 您可以使用相同的名称(例如, JOB_NAMEenv.JOB_NAME )从groovy访问相同的环境变量。

From the documentation: 从文档中:

Environment variables are accessible from Groovy code as env.VARNAME or simply as VARNAME. 可以从Groovy代码中以env.VARNAME或仅以VARNAME的形式访问环境变量。 You can write to such properties as well (only using the env. prefix): 您也可以写入这些属性(仅使用env。前缀):

 env.MYTOOL_VERSION = '1.33' node { sh '/usr/local/mytool-$MYTOOL_VERSION/bin/start' } 

These definitions will also be available via the REST API during the build or after its completion, and from upstream Pipeline builds using the build step. 这些定义也可以在构建期间或构建完成后通过REST API获得,也可以通过构建步骤从上游Pipeline构建获得。

For the rest of the documentation, click the "Pipeline Syntax" link from any Pipeline job 对于本文档的其余部分,请从任何管道作业中单击“管道语法”链接 在此处输入图片说明

To avoid problems of side effects after changing env , especially using multiple nodes, it is better to set a temporary context. 为了避免在更改env (特别是使用多个节点)后出现副作用的问题,最好设置一个临时上下文。

One safe way to alter the environment is: 更改环境的一种安全方法是:

 withEnv(['MYTOOL_HOME=/usr/local/mytool']) {
    sh '$MYTOOL_HOME/bin/start'
 }

This approach does not poison the env after the command execution. 该方法不会在命令执行后毒害环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM