Forked from Vietwear/post-external-results-to-jenkins-using-curl.sh
Created
September 21, 2018 21:23
-
-
Save gtarnaras/aaf0b184296ebfef4245e1cbdf0279ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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