Git, Gogs, Jekyll and Auto-deployment
Since yesterday, this blog deploys automatically when I push changes to its git repository. Before that, my workflow was something like:
- ssh into the server
- write something
- git add, git commit, git push (or forget about this step altogether)
- bundle exec jekyll build
Now, I do:
- Write something (from any machine where I have a copy of my blog's git repository)
- git add, git commit, git push
The best part for me is that I should now have a proper git history of my blog's modifications instead of a few giant commits spaced far apart in time due to skipping step 3 by mistake or laziness.
This isn't unique, has been done before, and a few automated deployment techniques are documented on the Jekyll site, but I wanted to jot down my configuration for a few reasons:
- As a "note to self" so I can recall how it works if I need to set this up again
- I'm due for my yearly blog post (yes, I should write more)
- It might be helpful to other people using a similar list of tools
This blog is hosted on a VPS (Linode) that runs a few other products, notably Gogs which I use to "self-host" my git projects.
One of those git projects is the source files for the blog.
Gogs lets you edit git hooks via its web interface under the git repository's "Settings > Git Hooks" section. The one we're interested in is "post-receive", which runs on the server-side after git is done receiving the changes you pushed to it via "git push" from the client-side.
Since both Gogs and the Jekyll blog are on the same server, the only thing I have to do is tell git to run the "jekyll build" command (along with some house-keeping details) after it got the new data:
#!/usr/bin/bash mkdir -p /tmp/gogs-bundle # Create directory where bundle will install required tools to build export BUNDLE_BIN_PATH=/tmp/gogs-bundle # Set env. var used by bundle so it knows where to install stuff cd /srv/http/chromic.org/public_html # Change to the directory where the blog's source files are unset GIT_DIR # Have git use $PWD instead of $GIT_DIR git pull # Get latest changes bundle install # Make sure we have all the required gems, etc. installed bundle exec jekyll build # Deploy!
And that's pretty much it. Make sure the user running your Gogs instance has access to the blog's files/directories and you should be good to go.