当 sh 输出包含左括号(| Jenkins 声明性管道

Failed to assign sh output to a variable when sh output contains opening bracket ( | Jenkins Declarative Pipeline

我正在尝试在其中一个步骤中将一些数据写入文件,然后在之后的步骤中使用读取该数据并将其分配给一个变量。这是我的声明式 Jenkins 管道:


pipeline {
    agent {label 'build-slave-aws'} 
    stages {
        stage('Notify about start') {
            steps {
                sh 'echo "Some fatct with brackets ()" > /tmp/facts.issues'           
            }
        }

         stage('Gather the facts') { 
            steps {
                    script {
                        factsIssues = sh( script: "cat /tmp/facts.issues", returnStdout: true )
                    }

                   sh "echo these are facts: ${factsIssues}"     
            }
        }
    }
}

此 运行 的输出如下:

Started by user 123
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on i-000df827977fd5175 in /workspace/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Notify about start)
[Pipeline] sh
+ echo 'Some fatct with brackets ()'
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Gather the facts)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ cat /tmp/facts.issues
[Pipeline] }
[Pipeline] // script
[Pipeline] sh
/workspace/workspace/test@tmp/durable-2a1f8cdf/script.sh: line 1: syntax error near unexpected token `('
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 2
Finished: FAILURE

任何其他不包含 ( 的文本都可以正常工作。关于如何使用 ( 在文件中写入一些数据并将其读回变量,您有什么建议吗?

您在最后一个 sh 脚本中遗漏了 echo 命令参数的引号。

修复:

sh "echo 'these are facts: ${factsIssues}'"