I’m looking to start publishing automatically for this blog so when I set an article with a date in the future, it will auto generate and deploy without any interference from me. Having worked with things like jenkins, I think this type of software is too heavy for my little old pi to deal with. As a result, I’m going to try to cobble something together using git, bash, and rsync.

I currently have my code projects stored in a local git repo that I run on another pi (maybe we can get into that later) so I’ll be using that for cloning, you may have your code on github or something similar, the principle should be the same.

Installing git on your pi

We need to install git on our pi so I can clone my repo. SSH onto your pi and then:

sudo apt-get update
sudo apt-get install wget git-core

01 git-core install

Clone the git repo

Now we need to clone the project, I’m cloning mine into the home location. I will be cloning via ssh so that I can pull from the repo credential free (via a script)

git clone ssh://<user>@<host>/<your-repo-location>

My example would look like:

git clone ssh://git@192.168.1.73/home/git/repositories/web/blog.git

We will need to add our public key to the authorized_keys on the git pi. Take a look in /home/pi/.ssh to see if you already have your public key file id_rsa.pub. If not run:

ssh-keygen

Choose the default location and pass (press enter without making changes) and you should now see the id_rsa.pub file. Now add the contents of the public key file into /home//.ssh/authorized_keys on the remote site, you should create this file if it doesn't exist and then chmod it to 600.

02 ssh-keygen

Now test that you can ssh to it without entering a password:

ssh <user>@<host>

My example would be:

ssh git@192.168.1.73

If you’re still having to enter the password, try the following on your git instance:

chmod 700 $HOME/.ssh
chmod go-w $HOME $HOME/.ssh
chmod 600 $HOME/.ssh/authorized_keys
chown `whoami` $HOME/.ssh/authorized_keys

This fixed it for me and I can now ssh without a password. Now we need to test if we can check for updates.

I deleted an re-cloned the repo using my new ssh authorization:

git clone ssh://<user>@<host>/<your-repo>

My example:

git clone ssh://git@192.168.1.73/home/git/repositories/web/blog.git

I have a release branch that I’ll merge into for the automated release process so this is the branch that I’ll checkout:

cd <your-project-directory>
git checkout <release-branch-name>

My example:

cd blog
git checkout release

This should allow you to make all future git requests without credentials, this is especially useful to us as we want to automate the process.

That’s the git repo sorted, now I can setup jekyll.

Install gem for building

Now we have git installed, cloned, and our branch in place, we need to install jekyll. We’ve already run sudo apt-get update so we won’t do that again.

sudo apt-get install ruby-full
sudo gem install jekyll
sudo gem install bundler
bundle install

After these installations have completed, we can try to build the jekyll blog:

jekyll build

When trying to run this I saw an error:

You have already activated addressable 2.5.1, but your Gemfile requires addressable 2.5.0. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)

So I’ve tried running:

bundle exec jekyll build

This seems to have solved the problem.

Upon completion we can then transfer the _site directory into the nginx web server directory using rsync:

rsync /home/pi/blog/_site /usr/share/nginx/html/_site

Now it’s time to put all of this into a bash script that we can run automatically

Create a bash script to run all of the above

sudo touch git_update_script
sudo chmod a+x git_update_script
sudo vi git_update_script

Add the following:

#!/bin/bash

# navigate to git repo
cd /home/pi/blog

# bring remote refs up to date
git remote update

# check for changes between our repo and remote
if [[ `git diff --name-only origin/release` ]]; then

  # changes
  echo 'changes'

  # Updated 2017-08-17 as I had some deployment issues, Added the following 2 lines and commented the 2 lines after.
  # I think it may have been caused by a manual intervention with sudo (which was silly)
  # get all changes ignore any changes in this workspace
  git reset --hard origin/release

  # pull the changes (commented out 2017-08-17)
  #git pull

  # build jekyll
  sudo bundle exec jekyll build

  # rsync nginx html directory
  rsync -ravz /home/pi/blog/_site/ /usr/share/nginx/html/_site/

  # remove changes made by building the project
  git checkout .

else

  echo 'no changes'

  # no changes so regenerate jekyll to create any new posts without updating git first
  sudo bundle exec jekyll build

  # rsync nginx html directory
  rsync -ravz /home/pi/blog/_site/ /usr/share/nginx/html/_site/

  # Updated 2017-08-17 as I had some deployment issues, Added the following 2 lines.
  # I think the jekyll blog may have been built but not reset after coming through this code block
  # remove changes made by building the project
  git checkout .

fi

Save and quit:

:wq

Move the script to /etc/cron.hourly

sudo mv git_update_script /etc/cron.daily

This should now run every hour and auto publish any posts as they become available. Happy days, a proper jekyll workflow!