Jenkins Pipeline - 在 shell 中插入变量会创建一个新行

Sau*_*abh 1 groovy jenkins jenkins-pipeline

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

pipeline {
     agent any
     parameters {
    choice(
        name: 'ENVIRONMENT_URL',
        choices: "https://beta1.xyz.com\nhttps://beta2.xyz.com\nhttps://beta3.xyz.com",
        description: 'interesting stuff' )
  }
Run Code Online (Sandbox Code Playgroud)

在本Stage节中,我有以下内容

 stage('execute tests') {
            steps {
                script {
                        sh """URL=${ENVIRONMENT_URL} npm run e2e:tests"""
                        sh 'echo -e "\nTest Run Completed.\n"'   
                }
             }   
            }
Run Code Online (Sandbox Code Playgroud)

但是,当我通过选择添加的选择参数来运行管道作业时,将执行以下操作(插入的选择参数会创建换行符):

+ URL=https://beta1.xyz.com
+ npm run e2e:tests
Run Code Online (Sandbox Code Playgroud)

使用变量会导致换行,这就是导致问题的原因。我尝试了不同的方法来避免换行。尝试使用变量,但这没有帮助。尝试使用不同的报价,但也没有。

我该怎么做才能避免断线?

Mat*_*ard 5

trim您可以在 String 类类型上使用该方法来删除尾随空格和换行符:

sh "URL=${params.ENVIRONMENT_URL.trim()} npm run e2e:tests"
Run Code Online (Sandbox Code Playgroud)

注意我还指定了您的参数位于地图中params,并删除了三引号,因为这些引号用于多行字符串格式。

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

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

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