Skip to content

Instantly share code, notes, and snippets.

@grant-roy
Last active March 28, 2024 10:45
Show Gist options
  • Save grant-roy/49b2c19fa88dcffc46ab to your computer and use it in GitHub Desktop.
Save grant-roy/49b2c19fa88dcffc46ab to your computer and use it in GitHub Desktop.
Bash script to poll Github and rebuild and restart a jekyll site
#!/bin/sh
#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
do
#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
cd
sudo service nginx stop
#remove current site directory
sudo rm -rf /var/www/site.com/public_html;
#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/site.com/public_html
sudo service nginx start;
fi
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment