[フレーム]
Last Updated: September 27, 2021
·
44.17K
· julianchurchill

Check Jenkins for build status via a shell script

This can be handy for embedding into a tmux status bar, occasional checks in a terminal or embedding as part of another process. Saves you having to go to the Jenkins web page.

The following script can also be found as a gist here.

Use like this: 'checkJenkins.sh <jobname>' and it will query the server with the given job name every 30s. Use like this: 'checkJenkins.sh <jobname> tmux' and it will query the server once and output tmux coloured messages appropriate to display in a tmux status bar. E.g. add this to your .tmux.conf 'set -g status-right "#(checkJenkins.sh jobname tmux)"

#!/bin/bash

CONSOLE_RED="033円[2;31m"
CONSOLE_GREEN="033円[2;32m"
CONSOLE_CLEAR="033円[0m"

JENKINS_SERVER=http://my_jenkins_server
JOB=1ドル
JOB_QUERY=/job/${JOB}

BUILD_STATUS_QUERY=/lastBuild/api/json
JOB_STATUS_JSON=`curl --silent ${JENKINS_SERVER}${JOB_QUERY}${BUILD_STATUS_QUERY}`

CURRENT_BUILD_NUMBER_QUERY=/lastBuild/buildNumber
CURRENT_BUILD_JSON=`curl --silent ${JENKINS_SERVER}${JOB_QUERY}${CURRENT_BUILD_NUMBER_QUERY}`

LAST_STABLE_BUILD_NUMBER_QUERY=/lastStableBuild/buildNumber
LAST_STABLE_BUILD_JSON=`curl --silent ${JENKINS_SERVER}${JOB_QUERY}${LAST_STABLE_BUILD_NUMBER_QUERY}`

check_build()
{
 GOOD_BUILD="${GREEN}Last build successful. "
 BAD_BUILD="${RED}Last build failed. "
 CLEAR_COLOURS=${CLEAR}
 RESULT=`echo "${JOB_STATUS_JSON}" | sed -n 's/.*"result":\([\"A-Za-z]*\),.*/1円/p'`
 CURRENT_BUILD_NUMBER=${CURRENT_BUILD_JSON}
 LAST_STABLE_BUILD_NUMBER=${LAST_STABLE_BUILD_JSON}
 LAST_BUILD_STATUS=${GOOD_BUILD}
 echo "${LAST_STABLE_BUILD_NUMBER}" | grep "is not available" > /dev/null
 GREP_RETURN_CODE=$?
 if [ ${GREP_RETURN_CODE} -ne 0 ]
 then
 if [ `expr ${CURRENT_BUILD_NUMBER} - 1` -gt ${LAST_STABLE_BUILD_NUMBER} ]
 then
 LAST_BUILD_STATUS=${BAD_BUILD}
 fi
 fi

 if [ "${RESULT}" = "null" ]
 then
 echo "${LAST_BUILD_STATUS}Building ${JOB} ${CURRENT_BUILD_NUMBER}... last stable was ${LAST_STABLE_BUILD_NUMBER}${CLEAR_COLOURS}"
 elif [ "${RESULT}" = "\"SUCCESS\"" ]
 then
 echo "${LAST_BUILD_STATUS}${JOB} ${CURRENT_BUILD_NUMBER} completed successfully.${CLEAR_COLOURS}"
 elif [ "${RESULT}" = "\"FAILURE\"" ]
 then
 LAST_BUILD_STATUS=${BAD_BUILD}
 echo "${LAST_BUILD_STATUS}${JOB} ${CURRENT_BUILD_NUMBER} failed${CLEAR_COLOURS}"
 else
 LAST_BUILD_STATUS=${BAD_BUILD}
 echo "${LAST_BUILD_STATUS}${JOB} ${CURRENT_BUILD_NUMBER} status unknown - '${RESULT}'${CLEAR_COLOURS}"
 fi
}

if [ "2ドル" = "tmux" ]
then
 GREEN="#[bg=blue fg=white]"
 RED="#[bg=red fg=white]"
 CLEAR=
 check_build
else
# GREEN=${CONSOLE_GREEN}
# RED=${CONSOLE_RED}
# CLEAR=${CONSOLE_CLEAR}
 GREEN=
 RED=
 CLEAR=
 QUERY_TIMEOUT_SECONDS=30
 while [ true ]
 do
 check_build
 sleep ${QUERY_TIMEOUT_SECONDS}
 done
fi

2 Responses
Add your response

nice script, thank you!

over 1 year ago ·

Excellent one, Can you please write a shell script to list all jenkins jobs which have latest build is older than 6 months....?

over 1 year ago ·

AltStyle によって変換されたページ (->オリジナル) /