Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bash script to poll Github and rebuild and restart a jekyll site
#This is a simple bash script that will poll github for changes to your repo,
#if found pull them down, and then rebuild and restart a Jekyll site running
#in Nginx. Note, we cannot use cron to schedule a job every 5 seconds, so we create
#a script that executes an infinite loop that sleeps every 5 seconds
#We run the script with nohup so it executes as a background process: $nohup ./update-jekyll
while true
#move into your git repo where your jekyll site is
cd ~/a_folder_created_off_home/the_git_repo_folder_you_cloned_in;
git fetch;
LOCAL=$(git rev-parse HEAD);
REMOTE=$(git rev-parse @{u});
#if our local revision id doesn't match the remote, we will need to pull the changes
if [ $LOCAL != $REMOTE ]; then
#pull and merge changes
git pull origin master;
#build the new site, you must install jekyll on the server, alternatively you could put the built _site
#repo under version control and just update based off the changes in that folder. Jekyll outputs build into /_site
jekyll build;
#change back to home directory
sudo service nginx stop
#remove current site directory
sudo rm -rf /var/www/;
#copy the newly built site into the directory nginx will serve it from
sudo cp -r a_folder_created_off_home/the_git_repo_folder_you_cloned_in/_site /var/www/
sudo service nginx start;
sleep 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment