curl - Jenkins上的卷曲状态响应处理,无法中止管道执行
问题描述
我有一个脚本管道(Jenkinsfile),我想在其中获取 CURL 响应(如果 200 ok,继续。否则,中止工作)。我正在为管道阶段的 curl 语法苦苦挣扎:
stage('Getting getting external API') {
steps {
script{
/* groovylint-disable-next-line LineLength */
final String url = "curl -X 'GET' \ 'https://my-endpoint-test/operations/promote/Approved' \ -H 'accept: */*'"
final def (String code) = sh(script: "curl -s -w '\\n%{response_code}' $url", returnStdout: true)
echo "HTTP response status code: $code"
/* groovylint-disable-next-line NestedBlockDepth */
if (code != "200") {
error ('URL status different of 200. Exiting script.')
}
}
}
}
我认为这个 URL 的方向不对,它抱怨“GET 之后和“-H”之前的“”。
WorkflowScript: 54: unexpected char: '\' @ line 54, column 47.
l String url = "curl -X 'GET' \ 'https:/
^1 error
另外,您能否建议一种更简单的中止此管道的方法,具体取决于 http 状态响应?
解决方案
您的 curl cmd 不正确。
stage('Getting getting external API') {
steps {
script{
cmd = """
curl -s -X GET -H 'accept: */*' -w '{http_code}' \
'https://my-endpoint-test/operations/promote/Approved'
"""
status_code = sh(script: cmd, returnStdout: true).trim()
// must call trim() to remove the default trailing newline
echo "HTTP response status code: ${status_code}"
if (status_code != "200") {
error('URL status different of 200. Exiting script.')
}
}
}
}
推荐阅读
- python - 查找错误的常见子索引值?
- magento - Magento 2 Admin 和前端显示错误“utils”
- php - 我只想通过编写雄辩的 $query 范围函数来获取第一个表的数据
- graph - 未调用阿波罗服务器突变解析器
- python - 在派生字段上查询 Django
- javascript - 为什么我不能将此代码部署到 firebase 函数?我不明白错误
- raster - 计算栅格中的波段数
- asp.net-mvc - Intellisense 不建议在 VS 2019 中用于旧 MVC5 项目
- r - 1:n[j != 0] 中的代码错误:NA/NaN 参数
- reactjs - 在父组件中使用 fetch 传递给子组件