简体   繁体   English

如何使用groovy在声明性jenkins管道中填充env变量

[英]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.

相关问题 如何在Jenkins声明性管道中导入的groovy脚本中使用@Library? - How to use @Library in an imported groovy script in Jenkins declarative pipeline? 如何使用Groovy Env。 Jenkins中的变量通过Jenkins管道中的bat命令传递 - How to use groovy env. variable in Jenkins to pass through bat command in Jenkins pipeline 如何在 Jenkins 声明性管道中执行 Groovy 语句? - How to execute Groovy statements in a Jenkins declarative pipeline? 声明性Jenkins管道; 如何声明变量并在脚本或邮件通知中使用它? - Declarative Jenkins Pipeline; How to declare a variable and use it in script or mail notification? 如何在Jenkins声明性管道的代理部分中使用环境变量? - How to use an environment variable in the agent section of a Jenkins Declarative Pipeline? Jenkins声明式管道中的Groovy错误 - Groovy error in Jenkins declarative pipeline 如何更改Jenkins Declarative Pipeline环境变量? - How to change a Jenkins Declarative Pipeline environment variable? 如何使用Jenkins声明式管道修改环境变量 - How to modify environment variable with Jenkins declarative pipeline 如何在Jenkins声明式管道脚本块内的循环中分配给env - How to assign to env in loop inside Jenkins declarative pipeline script block 如何在 Jenkins 管道脚本中使用 shell 脚本变量作为 groovy 变量 - How to use shell script variable as groovy variable in Jenkins pipeline script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM