Groovy 执行 shell \ 和 Jenkinsfile 中的正则表达式

问题描述 投票:0回答:1

我正在运行 Jenkinsfile 从远程服务器 grep pm2 服务,因此我使用 grep 实用程序来查找 pm2 服务名称。尽管如此,Jenkins Groovy 仍不理解 bash 命令,并在构建管道之前给出错误。

这是我的管道

pipeline {
    agent any

    parameters {
        string(defaultValue: '', description: 'List of PM2 applications', name: 'PM2_Applications')
    }

    stages {
    stage('SSH transfer') {
        steps([$class: 'BapSshPromotionPublisherPlugin']) {
            sshPublisher(
                continueOnError: false, failOnError: true,
                publishers: [
                    sshPublisherDesc(
                        configName: "server-config",
                        verbose: true,
                        transfers: [
                            sshTransfer(execCommand: "pm2 ls | grep -vE '^\s*id\s+' | awk '{print $4}'"),
                           
                           
                        ]
                    )
                ]
            )
        }
    }

}
}

我遇到的错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 18: unexpected char: '\' @ line 18, column 75.
   Command: "pm2 ls | grep -vE '^\s*id\s+' 

预期输出: 它应该使用 grep 运行 pm2 命令,它将给出命令的实际输出。

jenkins-pipeline jenkins-groovy groovyshell
1个回答
0
投票

反斜杠

\
是Groovy中的转义字符,要按字面意思使用它,您需要将其加倍
\\
。与
$
一样,你需要转义它 -
\$

© www.soinside.com 2019 - 2024. All rights reserved.