Skip to content

Instantly share code, notes, and snippets.

@kitingChris
Last active May 19, 2016 12:43
Show Gist options
  • Save kitingChris/27aac416552d0c8718460f06794fa69a to your computer and use it in GitHub Desktop.
Save kitingChris/27aac416552d0c8718460f06794fa69a to your computer and use it in GitHub Desktop.
Jenkins Github observer cron script triggers new build on changes. Useful when webhook is no option.
#!/bin/bash
# crontab -e
# */5 * * * * /path/to/jenkins-github-observer.sh myRepo myJenkinsJob > /dev/null 2>&1
repo=$1
jenkinsJob=$2
remoteUrl=git@github.com:kitingChris/$repo.git
jenkinsGitDir=/srv/jenkins/jobs/$jenkinsJob/workspace/.git
jenkinsCli=/usr/local/bin/jenkins-cli.jar
jenkinsUrl=http://localhost:8080/
git=/usr/bin/git
java=/usr/bin/java
github_hash=$($git ls-remote $remoteUrl | head -1 | sed "s/HEAD//")
jenkins_hash=$($git --git-dir=$jenkinsGitDir rev-parse HEAD)
echo " "
echo $repo
echo "jenkins: $jenkins_hash"
echo "github: $github_hash"
if [ -z "$jenkins_hash" ] || [ -z "$github_hash" ]; then
exit 1;
fi
if [ $jenkins_hash != $github_hash ]; then
echo " "
echo "hashes not equal; run jenkins job $jenkinsJob"
$java -jar $jenkinsCli -s $jenkinsUrl build $jenkinsJob
fi
echo " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment