Reputation: 6129
I have this shell script that checks is a specific Jenkins job is running or not:
JOB_URL=http://jenkins.local/job/stevehhhbuild
JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json
GREP_RETURN_CODE=0
# Start the build
curl $JOB_URL/build?delay=0sec
# Poll every 7seconds until the build is finished
while [ $GREP_RETURN_CODE -eq 0 ]
do
sleep 7
# Grep will return 0 while the build is running:
curl --silent $JOB_STATUS_URL | grep result\":null > /dev/null
GREP_RETURN_CODE=$?
done
echo Build finished
I put this script inside a Jenkins shell step, and it does what it suppose to do which is:
poll every 7seconds until the build is finished- once build is finished I get an exit code 1 which fails my build.
Is there a way I can "avoid" that exit code?
meaning -
If a build is in progress, a grep for result\":null will return 0.
If a build is finished, a grep for result\":null will return 1 but I don't want it to fail my build I just want it to echo something on the log and the build will continue.
Upvotes: 1
Views: 4746
Reputation: 4617
I have answered at https://serverfault.com/questions/309848/how-do-i-check-the-build-status-of-a-jenkins-build-from-the-command-line/980448#980448
JOB_URL=http://localhost:8080/view/TestTab/job/JobWait
JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json
GREP_RETURN_CODE=0
# Start the build
curl --user "username:password" $JOB_URL/build?delay=0sec
# Poll every 10 second until the build is finished
while [ $GREP_RETURN_CODE -eq 0 ]
do
sleep 10
# Grep will return 0 while the build is running:
curl --user "username:password" --silent $JOB_STATUS_URL | grep result\":null > /dev/null || if [ "$?" == "1" ]; then
exit 0
fi
GREP_RETURN_CODE=$?
done
echo Build finished
Upvotes: 0
Reputation: 6129
OK,
So the solution was to add ||
and after that the condition for exit code 1,
for small things that you want to happen when a shell command fails, you can use ||
:
|| if [ "$?" == "1" ];then
JOB_URL=http://jenkins.local/job/stevehhhbuild
JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json
GREP_RETURN_CODE=0
# Start the build
curl $JOB_URL/build?delay=0sec
# Poll every 7seconds until the build is finished
while [ $GREP_RETURN_CODE -eq 0 ]
do
sleep 7
# Grep will return 0 while the build is running:
curl --silent $JOB_STATUS_URL | grep result\":null > /dev/null || if [ "$?" == "1" ];then
GREP_RETURN_CODE=$?
done
echo Build finished
Upvotes: 1