如何从jenkinsfile中的curl获取响应代码 [英] How to get response code from curl in a jenkinsfile

查看:201
本文介绍了如何从jenkinsfile中的curl获取响应代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jenkinsfile中,我称卷曲为:

In a jenkinsfile I call curl with:

sh "curl -X POST -i -u admin:admin https://[myhost]"

我得到这样的输出:

...
HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
...

我想根据上述调用的响应代码采取不同的操作,但是如何仅将响应代码/答复存储在变量中?

I would like to take different actions based on the response code from the above call but how do I store only the response code/reply in a variable?

推荐答案

使用参数-w %{http_code}(来自您可以轻松获取HTTP响应代码:

you can easily get the HTTP response code:

int status = sh(script: "curl -sLI -w '%{http_code}' $url -o /dev/null", returnStdout: true)

if (status != 200 && status != 201) {
    error("Returned status code = $status when calling $url")
}

这篇关于如何从jenkinsfile中的curl获取响应代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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