Shahar Hamuzim Rajuan
Shahar Hamuzim Rajuan

Reputation: 6129

check if job in Jenkins finished using shell script

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 -

Upvotes: 1

Views: 4746

Answers (2)

Lakshmikandan
Lakshmikandan

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

Shahar Hamuzim Rajuan
Shahar Hamuzim Rajuan

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

Related Questions