Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Vietwear/b6dd7a46e30ef3898354881e57436c44 to your computer and use it in GitHub Desktop.
Save Vietwear/b6dd7a46e30ef3898354881e57436c44 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Original: http://steve-jansen.github.io/blog/2014/11/20/how-to-use-jenkins-to-monitor-cron-jobs/
# example cron script to post logs to Jenkins
# exit on error
set -e
log=`mktemp tmp.XXX`
timer=`date +"%s"`
jenkins_job=my_monitoring_job
jenkins_server=http://1.1.1.1:8081/job/$jenkins_job/postBuildResult
# see http://jenkins.example.com:8080/me/configure to get your username and API token
jenkins_username=username
jenkins_token=somehashapitoken
function banner() {
echo $(printf '#%.0s' {1..80}) >> "$log"
}
function report() {
result=$?
timer=$((`date +"%s"` - $timer))
banner
echo "`whoami`@`hostname -f` `date`: elapsed $timer second(s)" >> "$log"
echo "exit code $result" >> "$log"
# binary encode the log file for Jenkins
msg=`cat "$log" | hexdump -v -e '1/1 "%02x"'`
# post the log to jenkins
curl -k -X POST \
-u "$jenkins_username:$jenkins_token" \
-d "<run><log encoding=\"hexBinary\">$msg</log><result>$result</result><duration>$timer</duration></run>" \
"$jenkins_server"
}
trap report EXIT;
banner
echo "hello, world @ `date`!" | tee "$log"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment