相关疑难解决方法(0)

jenkins管道:带管道的多行shell命令

我正在尝试创建一个Jenkins管道,我需要执行多个shell命令并在下一个命令中使用一个命令的结果.我发现将命令包装在一对三个单引号中'''可以完成相同的操作.但是,在使用管道将一个命令的输出提供给另一个命令时,我遇到了问题.例如

   stage('Test') {
      sh '''
         echo "Executing Tests"
         URL=`curl -s "http://localhost:4040/api/tunnels/command_line" | jq -r '.public_url'`
         echo $URL
         RESULT=`curl -sPOST "https://api.ghostinspector.com/v1/suites/[redacted]/execute/?apiKey=[redacted]&startUrl=$URL" | jq -r '.code'`
         echo $RESULT
      '''
   }
Run Code Online (Sandbox Code Playgroud)

带管道的命令无法正常工作.这是jenkins控制台输出:

+ echo Executing Tests
Executing Tests
+ curl -s http://localhost:4040/api/tunnels/command_line
+ jq -r .public_url
+ URL=null
+ echo null
null
+ curl -sPOST https://api.ghostinspector.com/v1/suites/[redacted]/execute/?apiKey=[redacted]&startUrl=null
Run Code Online (Sandbox Code Playgroud)

groovy jenkins jenkins-pipeline

18
推荐指数
2
解决办法
5万
查看次数

如何在 Jenkins Groovy 的多行 shell 脚本中同时使用 Groovy 定义的和 OS 系统变量

我需要在 Jenkins 的多行 shell 脚本中访问 Groovy 定义的变量(例如 var1)。我需要在sh中使用双引号"""。(我指的是here

但我也需要读取和更改 os 系统变量(例如 aws_api_key)。它需要在 sh 中使用单引号 ''' 并使用 \ 来转义美元 $ 符号。(我指的是这里

我怎样才能同时使用它们?任何帮助都感激不尽。

例如

node ("Jenkins-Test-Slave") {
stage ("hello world") {
    echo 'Hello World'
}

def var1="bin"
stage ("test") {
    withEnv(["aws_api_key='define in withEnv'","service_url=''"]) {
        echo var1
        sh '''
            echo the groovy data var1 is "${var1}",'\${var1}',\$var1,${var1},var1!!!
            echo default value of aws_api_key is \$aws_api_key
            aws_api_key='changed in shell'
            echo new value of aws_api_key is \$aws_api_key

            export newvar='newxxx'
            echo the new var …
Run Code Online (Sandbox Code Playgroud)

shell groovy jenkins

4
推荐指数
2
解决办法
1314
查看次数

标签 统计

groovy ×2

jenkins ×2

jenkins-pipeline ×1

shell ×1