Jenkins Pipeline-在shell中插入变量会创建新行 [英] Jenkins Pipeline - inserting variable in shell creates a new line
问题描述
我在jenkins文件中使用Choice参数来选择环境,如下所示:
I am using Choice param in my jenkins file to select environment as follows:
pipeline {
agent any
parameters {
choice(
name: 'ENVIRONMENT_URL',
choices: "https://beta1.xyz.com\nhttps://beta2.xyz.com\nhttps://beta3.xyz.com",
description: 'interesting stuff' )
}
在Stage
部分,我有以下内容
stage('execute tests') {
steps {
script {
sh """URL=${ENVIRONMENT_URL} npm run e2e:tests"""
sh 'echo -e "\nTest Run Completed.\n"'
}
}
}
但是,当我通过选择添加的选择参数来运行管道作业时,将执行以下操作(插入的选择参数会创建换行符):
However, when i run the pipeline job by selecting the choice parameter i added, the following gets executed (the inserted choice param creates a line break):
+ URL=https://beta1.xyz.com
+ npm run e2e:tests
使用变量会导致换行,这就是问题所在. 我尝试了不同的方法来避免换行.尝试使用变量,但这没有帮助.尝试使用不同的报价,但没有.
Using the variable is causing a line break and that's what is causing issue. I have tried different ways to avoid line break. Tried using a variable but that didn't help. tried with different quotations, that didn't either.
如何避免换行?
推荐答案
您可以在String类类型上使用trim
方法来删除结尾的空格和换行符:
You can use the trim
method on a String class type to remove trailing whitespace and newline:
sh "URL=${params.ENVIRONMENT_URL.trim()} npm run e2e:tests"
请注意,我还指定了您的参数在params
映射中,并删除了用于多行字符串格式的三引号.
Note I also specified your parameter is in the params
map and removed the triple quotes as those are for multiline string formatting.
或者,您可以将选项指定为数组而不是多行字符串. choices
参数将显示为:
Alternatively, you can specify the choices as an array instead of as a multiline string. The choices
argument would then appear like:
choice(
name: 'ENVIRONMENT_URL',
choices: ['https://beta1.xyz.com', 'https://beta2.xyz.com', 'https://beta3.xyz.com'],
description: 'interesting stuff'
)
任何一种解决方案都可以解决您的问题.
Either solution would fix your issue.
这篇关于Jenkins Pipeline-在shell中插入变量会创建新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!