使用 CURL 的输出,如果输出包含错误则失败
Use output of a CURL and fail if the output contains ERROR
我在下面使用 CURL 语句:
curl -u $SONAR_TOKEN: https://$SONAR_SERVER/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY\&pullRequest=$SONAR_PR_KEY
输出:
使用 IF
语句 pass/fail 上面的场景。
if [ "$quality_gatesstatus" != "OK" ] && [ "$quality_gatesstatus" != "WARN" ]; then
echo "check sonar server and fix the issues: $quality_gatesstatus"
exit 1
else
echo "Sonar Quality gate succeeded!!!"
fi
但是 true/false 条件中的任何一个都只打印第一个 echo 语句并且作业失败。那么我在这里做错了什么,还有一件事我不能使用“jq”。
没有 jq
这应该是诀窍:
quality_gatesstatus=$(curl -u $SONAR_TOKEN: https://$SONAR_SERVER/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY\&pullRequest=$SONAR_PR_KEY)
echo "$quality_gatesstatus" | grep '"status":"ERROR"' > /dev/null
if [ $? -eq 0 ]; then
echo "check sonar server and fix the issues: $quality_gatesstatus"
exit 1
else
echo "Sonar Quality gate succeeded!!!"
fi
我在下面使用 CURL 语句:
curl -u $SONAR_TOKEN: https://$SONAR_SERVER/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY\&pullRequest=$SONAR_PR_KEY
输出:
使用 IF
语句 pass/fail 上面的场景。
if [ "$quality_gatesstatus" != "OK" ] && [ "$quality_gatesstatus" != "WARN" ]; then
echo "check sonar server and fix the issues: $quality_gatesstatus"
exit 1
else
echo "Sonar Quality gate succeeded!!!"
fi
但是 true/false 条件中的任何一个都只打印第一个 echo 语句并且作业失败。那么我在这里做错了什么,还有一件事我不能使用“jq”。
没有 jq
这应该是诀窍:
quality_gatesstatus=$(curl -u $SONAR_TOKEN: https://$SONAR_SERVER/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY\&pullRequest=$SONAR_PR_KEY)
echo "$quality_gatesstatus" | grep '"status":"ERROR"' > /dev/null
if [ $? -eq 0 ]; then
echo "check sonar server and fix the issues: $quality_gatesstatus"
exit 1
else
echo "Sonar Quality gate succeeded!!!"
fi