jenkins管道:带管道的多行shell命令 [英] jenkins pipeline: multiline shell commands with pipe

查看:2104
本文介绍了jenkins管道:带管道的多行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个Jenkins管道,在其中我需要执行多个shell命令,并在下一个命令中使用一个命令的结果.我发现将命令包装在三个单引号'''中可以达到相同的效果.但是,在使用管道将一个命令的输出馈送到另一个命令时,我遇到了问题.例如

I am trying to create a Jenkins pipeline where I need to execute multiple shell commands and use the result of one command in the next command or so. I found that wrapping the commands in a pair of three single quotes ''' can accomplish the same. However, I am facing issues while using pipe to feed output of one command to another command. For example

   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
      '''
   }

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

Commands with pipe are not working properly. Here is the jenkins console output:

+ 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

推荐答案

我尝试在jenkins代码段生成器中为管道输入所有这些命令,并提供了以下输出:

I tried entering all these commands in the jenkins snippet generator for pipeline and it gave the following output:

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
'''

注意命令jq -r \'.public_url\'jq -r \'.code\'中的转义单引号.通过这种方式使用代码可以解决问题

Notice the escaped single quotes in the commands jq -r \'.public_url\' and jq -r \'.code\'. Using the code this way solved the problem

更新:一段时间后,甚至开始出现问题.在执行某些命令之前,需要执行某些命令.其中一个是grunt serve,另一个是./ngrok http 9000.我在这些命令中的每条命令之后都添加了一些延迟,现在它解决了问题.

UPDATE: : After a while even that started to give problems. There were certain commands executing prior to these commands. One of them was grunt serve and the other was ./ngrok http 9000. I added some delay after each of these commands and it solved the problem for now.

这篇关于jenkins管道:带管道的多行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆