Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jenkins管道-在shell中插入变量将创建新行_Jenkins_Groovy_Jenkins Pipeline - Fatal编程技术网

Jenkins管道-在shell中插入变量将创建新行

Jenkins管道-在shell中插入变量将创建新行,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,我在jenkins文件中使用Choice param选择环境,如下所示: pipeline { agent any parameters { choice( name: 'ENVIRONMENT_URL', choices: "https://beta1.xyz.com\nhttps://beta2.xyz.com\nhttps://beta3.xyz.com", description: 'interesting st

我在jenkins文件中使用Choice param选择环境,如下所示:

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('execute tests') {
            steps {
                script {
                        sh """URL=${ENVIRONMENT_URL} npm run e2e:tests"""
                        sh 'echo -e "\nTest Run Completed.\n"'   
                }
             }   
            }
但是,当我通过选择添加的选项参数来运行管道作业时,会执行以下操作(插入的选项参数会创建换行符):

使用该变量会导致换行,这就是导致问题的原因。 我试过不同的方法来避免断线。尝试使用变量,但没有帮助。用不同的语录试过,也没用


如何避免换行?

您可以对字符串类类型使用
trim
方法删除尾随空格和换行符:

sh "URL=${params.ENVIRONMENT_URL.trim()} npm run e2e:tests"
注意:我还指定了参数在
params
映射中,并删除了三重引号,因为它们用于多行字符串格式

或者,可以将选项指定为数组而不是多行字符串。
choices
参数将显示如下:

choice(
  name:        'ENVIRONMENT_URL',
  choices:     ['https://beta1.xyz.com', 'https://beta2.xyz.com', 'https://beta3.xyz.com'],
  description: 'interesting stuff'
)

任何一种解决方案都可以解决您的问题。

您可以对字符串类类型使用
trim
方法删除尾随空格和换行符:

sh "URL=${params.ENVIRONMENT_URL.trim()} npm run e2e:tests"
注意:我还指定了参数在
params
映射中,并删除了三重引号,因为它们用于多行字符串格式

或者,可以将选项指定为数组而不是多行字符串。
choices
参数将显示如下:

choice(
  name:        'ENVIRONMENT_URL',
  choices:     ['https://beta1.xyz.com', 'https://beta2.xyz.com', 'https://beta3.xyz.com'],
  description: 'interesting stuff'
)
任何一种解决方案都可以解决您的问题