【发布时间】:2021-05-25 18:55:50
【问题描述】:
我正在尝试在变量中捕获 http 请求的响应。以下问题为我提供了完美的解决方案(How to evaluate http response codes from bash/shell script?)。当我在本地执行此命令时
response=$(curl --write-out '%{http_code}' --silent --output /dev/null http://localhost:8082/url)
echo $response
它给了我想要的 http 代码(例如 400)。然而在詹金斯我执行相同的命令,但它给了我一个空的响应:
sh '''#!/bin/bash
DOCKER_RESPONSE=$(curl --write-out '%{http_code}' --silent --output /dev/null http://localhost:8002/route?json={})
while [[ $DOCKER_RESPONSE != 200 ]]
do
sleep 1
echo "$DOCKER_RESPONSE"
DOCKER_RESPONSE=$(curl --write-out '%{http_code}' --silent --output /dev/null http://localhost:8002/route?json={})
done
'''
【问题讨论】:
-
如果你想得到
sh命令的输出,你必须使用def shell_output = sh returnStdout: true, script: ``` ... ```。否则"200" != 200可能一个是字符串,另一个是数字。
标签: bash jenkins jenkins-pipeline