如何在 Jenkins 管道脚本中为字符串参数添加双引号

How can I add double quotes to a string parameter in Jenkins pipeline script

我有一个 Jenkins 管道作业。 该作业将输入参数作为字符串。 我能够从脚本中检索该值。

我想将该字符串添加或括在双引号内。我怎样才能做到这一点?

输入参数:www.google.com

在脚本中:echo InputLink

正在向我打印值 www.google.com

预期结果 "www.google.com"

在您的管道代码中,只需将引号添加到输入字符串中。

假设你的输入参数叫做Input:

quoted_input = '"' + Input + '"'
//or using string interpolation 
quoted_input = "\"${Input}\""

然后您可以在任何需要引用值的地方使用 quoted_input

如果您使用的是声明性管道,请使用脚本块:

script {
    quoted_input = "\"${params.Input}\""
}

或者将其定义为环境变量,以便所有阶段都可用:

pipeline {
    environment {
        QUOTED_INPUT = "\"${params.Input}\""
    }
    ...

}