詹金斯:在Shell脚本/CURL请求中使用变量 [英] Jenkins: Using variable in Shell Script / CURL Request

查看:329
本文介绍了詹金斯:在Shell脚本/CURL请求中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将早期HTTP请求的响应中的数字保存到变量中.在CURL Request中,我需要将此数字插入URL,但是不知何故,它不会被识别为变量.只是整理出来了,这就是网址错误的原因,并且我收到了400错误.

I save a number out of a response from an earlier HTTP Request into a variable. In the CURL Request I need to insert this number into the URL, but somehow It doesn´t get recognized as a variable. It just gets sorted out which is why the URL is wrong and I get a 400 Error.

script{

// first httpRequest where I get the number from
def response = httpRequest httpMode: 'POST', url:"https://myServer.com", contentType: 'APPLICATION_JSON_UTF8', requestBody: 'myBody'

def props = readJSON text: response.content

def num = props.id[0]       //num stores the number now

sh(Script: curl "https://myServer.com/$num/files" -i -X POST -H Content-Type:multipart/form-data -F file=@JenkinsDemo.txt
}

控制台输出显示sh尝试访问 https://myServer.com//files,因此省略了变量.

The console output shows me that the sh tries to reach https://myServer.com//files , so it leaves out the variable.

我还尝试将整个URL放入一个变量中并使用curl进行引用,但它也无法被识别. 当然,我尝试了几种键入选项,例如$ {num},'$ num'等...

I also tried to put the whole URL into a variable and refer in curl but it doesn´t get recognized as well. Of course I tried several typing options like ${num}, '$num' etc...

有什么想法吗?

编辑

我还收到消息"message":给定的URL包含恶意字符". ,可能是因为"$"

I also get the message "message":"The given url contains malicious characters" , probably because of the '$'

另一件事很奇怪:如果我键入$ {props.id [0]}而不是$ {num}(因为它们拥有相同的数字,这没有什么区别),则消息更改为 "/var/jenkins_home/workspace/Pipeline Test@tmp/durable-153a0829/script.sh:1:/var/jenkins_home/workspace/Pipeline Test@tmp/durable-153a0829/script.sh:错误的替换"

Another weird thing: If I type ${props.id[0]} instead of ${num} (which doesn´t make any difference because they hold the same number) the message changes to "/var/jenkins_home/workspace/Pipeline Test@tmp/durable-153a0829/script.sh: 1: /var/jenkins_home/workspace/Pipeline Test@tmp/durable-153a0829/script.sh: Bad substitution"

推荐答案

我解决了这个问题.卷曲请求需要用双引号引起来.

I solved the problem. the curl request needs to be surrounded by double quotation marks.

sh("curl https....")

双引号使使用groovy脚本中定义的变量成为可能.没有它们,它将被解释为java String.

The double quotation marks enable the possibility to use the variables defined in groovy script. Without them it just gets interpreted as java String.

这篇关于詹金斯:在Shell脚本/CURL请求中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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