【问题标题】:Execute bash command in jenkinsfile groovy在jenkinsfile groovy中执行bash命令
【发布时间】:2021-03-22 14:57:43
【问题描述】:

请帮忙

这里我有 Jenkinsfile 的一部分,比如

@Library('groovy_shared_libraries')_

stage("Ensure droplet don`t exist and Create DO Droplet") {
    // Ensure droplet don`t exist
    ExistDroplet = sh(
        script: "doctl compute droplet list | awk '{gsub(/\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\.//g") | awk '{ print $2 }' | wc -l"
        returnStdout: true
    ).trim()

如何在 jenkinsfile groovy 中执行这个 bash 命令?

doctl compute droplet list | awk '{gsub(/\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\.//g") | awk '{ print $2 }' | wc -l

对于当前的实现,它会返回一个错误

WorkflowScript: 26: unexpected char: '\' @ line 26, column 63.
   te droplet list | awk '{gsub(/\./, "", $

如果我在命令中添加额外的 \ 我有这个错误

WorkflowScript: 26: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 26, column 21.
           script: `"doctl compute droplet list | awk '{gsub(/\\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g") | awk '{ print $2 }' | wc -l",`

添加 escape $2 jenkins 后显示此错误

WorkflowScript: 26: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 26, column 21.
           script: `"doctl compute droplet list | awk '{gsub(/\\./, "", \$2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g") | awk '{ print \$2 }' | wc -l",`

这一切都适用于这个命令doctl compute droplet list | grep -w \"$FullDomainName\" | awk '{ print \$2 }' | wc -l 但我需要添加

 awk '{gsub(/\\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g")

【问题讨论】:

  • 您当前的版本会发生什么?您是否在任何地方看到错误?请点击edit 并添加所有相关的详细信息。

标签: linux bash jenkins jenkins-pipeline jenkins-groovy


【解决方案1】:

\ 是 shell 和 Jenkins 的转义字符。如果你想将反斜杠字符发送到 shell,你需要用另一个 Jenkins 来转义它(注意\\):

script: "doctl compute droplet list | awk '{gsub(/\\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g") | awk '{ print $2 }' | wc -l"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2018-01-31
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多