[英]How to use groovy to populate an env variable in a declarative jenkins pipeline
I am struggling to populate an environment variable in a Jenkinsfile using groovy 我正在努力使用groovy在Jenkinsfile中填充环境变量
The code below fails: 以下代码失败:
pipeline {
environment {
PACKAGE_NAME = JOB_NAME.tokenize('/')[1]
}
{
with the following error: 出现以下错误:
Environment variable values can only be joined together with '+'s
环境变量值只能与'+'连接在一起
What am I doing wrong? 我究竟做错了什么? Sorry if the question is basic, I am just starting with both groovy and pipelines.
对不起,如果问题是基本的,我只是从groovy和管道开始。
Declarative pipeline is pretty strict about the values you can assign to environment variables. 声明性管道对于可以分配给环境变量的值非常严格。 For instance, if you try to set
PACKAGE_NAME
to JOB_NAME
you will get following error: 例如,如果您尝试将
PACKAGE_NAME
设置为JOB_NAME
,则会出现以下错误:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: Environment variable values must either be single quoted, double quoted, or function calls. @ line 5, column 24.
PACKAGE_NAME = JOB_NAME
To avoid getting errors and set PACKAGE_NAME
env variable as expected you can put it into double quotes and evaluate expression inside the GString: 为避免出现错误并按预期设置
PACKAGE_NAME
env变量,可以将其放入双引号并计算GString中的表达式:
environment {
PACKAGE_NAME = "${JOB_NAME.tokenize('/')[1]}"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.