无法在Jenkins Pipeline中运行 shell 脚本

xn1cxnb4  于 2023-03-01  发布在  Jenkins
关注(0)|答案(1)|浏览(438)

我尝试在我的管道中添加以下步骤,但收到此错误illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 33, column 75. td>Total<([^>]+>){4}([^<]*).*$/Missed in
我的脚本是:

stage('Test Coverage'){
            steps {
                echo "Running test"
                sh "mvn test"
                script{
                    TOTAL_COVERAGE_STR = sh (
                    script: "sed -nE 's/^.*<td>Total<([^>]+>){4}([^<]*).*$/Missed instructions: \2/p' target/site/jacoco/index.html | tr -dc '0-9.0-9'",
                    returnStdout: true
                    ).trim()
                    THRESHOLD = 12.00
                    Float TOTAL_COVERAGE = TOTAL_COVERAGE_STR as Float
                    echo "Total Coverage : ${TOTAL_COVERAGE}, Threshold: ${THRESHOLD}"
                    if (THRESHOLD > TOTAL_COVERAGE) {
                        error('Aborting the build, as The Test Coverage Is Not As Expected')
                    }
                }
            }
        }

我尝试捕获单引号内的脚本,也像这样

'sed -nE 's/^.*<td>Total<([^>]+>){4}([^<]*).*$/Missed instructions: \2/p' target/site/jacoco/index.html | tr -dc '0-9.0-9''

在上述情况下,获取错误:unexpected token: s

roejwanj

roejwanj1#

解析正则表达式时似乎出现了问题。也许您可以尝试将正则表达式语法设置为独立于变量,并在脚本中使用它:
regexVal = /^.*<td>Total<([^>]+>){4}([^<]*).*$/

  • 如果它包含要插入的变量,则需要前导和尾随斜线。*

因此,您的脚本将更改为以下内容:
'sed -nE 's/${regexVal}/Missed instructions: \2/p' target/site/jacoco/index.html | tr -dc '0-9.0-9''

相关问题