Enabling Continuous Deployment for Jekyll
Making DigitalOcean, Jekyll, GitHub and Travis CI all work
This post is from 2016. Travis CI's free tier for open source is long gone, and this exact setup would live in GitHub Actions today, but the pattern (build, validate, push the built site to the server over SSH) holds up unchanged.
Static sites are great and have various advantages. However, I am aware of how much easier WordPress is to use for lots of users. If we want to compare WordPress workflow to that of a static site: even though making changes to my Jekyll site may seem rather easy to me, it really isn’t that straightforward:
- Write a post in Markdown
- Commit changes to GitHub
- Build the site on my local machine
- Manually transfer the changed files to the server
Although I have moved huge hidden parts like compressing images and compressing files and HTML to Grunt tasks, there are still steps needed to easily deploy changes locally to the live server. Another problem with this workflow is that the GitHub repo sometimes wouldn’t be up to date with the content I was serving, or vice versa, because I had to make shortcuts here and there (edit content directly on the live server, push a change locally but forget to deploy that live and so on). Basically, you want the code on GitHub to actually represent what you are serving. All of the above has led me to set up automatic deployments to my server. This means that my workflow now just consists of creating content and committing it: the rest is done automatically. Travis CI builds the site, compresses assets, runs a few tests, and deploys it all to DigitalOcean, where a web server (Apache, Nginx) serves it.
Failed experiments
I have my Jekyll instance running on a DigitalOcean Droplet. To configure one, you can easily follow their great tutorial.
The Jekyll website has nice articles on continuous integration and deployment. In a nutshell, serving Jekyll can be done in two ways:
- It is a static website, so just have a web server like Apache running and point that to the
_site - Run the
jekyll serveprocess on your server and have your web server (e.g., Apache) proxy requests to it athttp://127.0.0.1:4000
By default
jekyll servebinds to 127.0.0.1, which is all the proxy needs since Apache runs on the same machine. Setting 0.0.0.0 as the address instead (jekyll serve -H 0.0.0.0) is a way of saying to match all network interfaces on the machine: if your local IP was 192.168.0.1 it would serve on both 127.0.0.1 and 192.168.0.1, and computers on your local network would reach your Jekyll site directly, without going through the web server.
To enable those two scenarios, I used Codeship's deployment pipeline that will make sure to pull the latest changes on the website and its submodules whenever a push is triggered on GitHub. I did that by having this line:
Here you substitute the location of your GitHub repo on the server, e.g., /var/www/blog.
Now, after that you can have a Git post-receive or post-merge hook that will run the appropriate Grunt task, e.g., grunt serve, or, if you are running jekyll serve inside tmux for example and the process is already running, it will pick up the change and your changes will be reflected automatically.
Both hooks must have the correct permissions and be executable:
The only thing I'd like to note if you are using the jekyll serve approach is that you will need to tell your web server to proxy requests to Jekyll's instance. To do that, you will need to edit /etc/apache2/sites-available/000-default.conf file as follows:
For reasons I am still not sure about, this process failed for me as the website was not updating as I was pushing changes. For example, when I push to GitHub, the build triggers and the build is successful and is able to pull the latest for the main repo and the submodule but the git hooks seem to never be fired. However, I have tried to pull manually from the repo and indeed the hooks were triggered then.
I also tried creating a new script called deploy.sh, which is called as part of the Codeship custom script, so that the script is:
And the deploy.sh has the correct permissions and is:
Where the file deployment.txt will contain a log of the latest commit messages that were deployed. Again, this method failed as well.
I have tried to add a remote in my web server called production so that I can push directly to my server with git push production master: the data is pushed indeed, but I still don't get the hooks to be run at all.
As a result of my failed attempts, I had to try another approach and I decided to go and try Travis CI.
Going the Travis way
The first step to achieving this new workflow was to have rigorous rules set up on the server. No more deployments as root; from now on we’re doing things correctly.
Setting up the server
Create a new user with restricted access
We need to create a new user with limited permissions: the user will only be allowed to operate in the directory where the blog lives on the server, e.g., /var/www/blog. This new user will also be used by Travis CI to log in, and we’ll give it ownership of our live directory.
Set up public key authentication
For Travis CI to log in to the server, we’ll set up public key authentication. To do this, on your local machine, you should run:
Which should output something like this:
It will ask for a passphrase; leave that blank. This process should leave you with two files in your .ssh folder: deploy-key, which is your private key (this is like a password, so protect it at all times!) and deploy-key.pub, your public key.
Now your server needs to know your public key for the correct user so that you can log in using your private key. SSH to your live server and:
Then, we’ll create a .ssh directory in the home directory, and an authorized_keys file inside of it:
At this point, you can paste in the contents of your deploy-key.pub file. Now, we’ll just restrict the permissions to the authorized_keys file:
Create a remote
We now want to be able to push to our server the same way we push to GitHub. Now, to understand how we will structure our folders, we need to remember how Jekyll compiles and generates our site. Basically, Jekyll will build, based on your template and configuration, the final "static" version of the website and put it in the _site folder. This means that what you should be serving is only that folder and we really don't care about all the other stuff.
I have created a separate hidden folder called .build which will contain all the sites to build. I then create a subfolder for each remote repository. For example, for my blog I have created an empty folder in it with mkdir /var/www/.build/blog and then initialized the Git repo there with cd /var/www/.build/blog && git init --bare.
We need to make sure that we have correct access permissions and ownership for our repositories.
Now we need to add a post-receive hook, which is a list of commands to run once the remote has received a push:
Now, I need to tell Git where to find my main _site or the content that will be served live. For me, I have set up my server to serve my content from /var/www/blog. So, in my git hook I have:
The post-receive hook will just need to be executable, so that it actually can do its job:
Setting up Travis CI
Everything now is ready for Travis to start doing its magic. However, we have a slight problem of granting Travis access to the private key. If our GitHub repo is private, committing the key could be a solution, although this is definitely not a recommended approach.
It turns out that Travis has a command-line utility to encrypt files. Using this, it’s safe to upload the key publicly to GitHub, and Travis CI can still use it. You’ll need to log in first though, because it will have to add two private environment variables to your Travis account, so that it can decrypt the encrypted private key later on (yeah, we’re getting into some pretty heavy crypto setups now, but it’s the best we can do).
Travis executes commands based on instructions defined in a .travis.yml file. We need to create such a file in your repository:
You’ll need to copy your private key file (deploy-key) to your local repository. Then, we’ll install the Travis command line utility, log in, and encrypt the file:
The last command will create an encrypted copy of your deployment key, deploy-key.enc. It will also add a few lines to your .travis.yml:
You should immediately remove the unencrypted private key from your repo, so that you don’t risk uploading it to GitHub. There are bots out there that do nothing but analyze GitHub commits to find login information, and they will compromise your server in a matter of seconds if you accidentally disclose the key.
Instructing Travis with .travis.yml
All we need to do now is to tell Travis how to build, test and deploy the site. For that purpose, we have the .travis.yml, which acts as a list of instructions for Travis.
Right now, my .travis.yml file looks like this:
Note that I have masked various things like server address and Slack token for security reasons. The variables
<SERVER_IP_ADDRESS>,<SERVER_PORT_NUMBER>,<SLACK_CHANNEL>and<TOKEN>have to be filled with your specific details.
There are various configurations that we can set up to customize the build. Our configuration file defines Ruby as the main language, and version 2.2 as the version that should be installed on Travis with rvm.
We define as well what files and folders to cache. This is of great importance as it speeds up our build significantly. Caching lets Travis CI store directories between builds, which is useful for storing dependencies that take longer to compile or download. For us, we cache the dependencies for Jekyll with bundler and all of our node_modules as well.
We also add our server IP address, once on its own and once with the SSH port (I did this as I have a different SSH port number than the default one), as known hosts so that I don’t get a warning when I try to connect to it.
I reference a few bash scripts to run at various stages of the build process. These scripts have been placed in the _scripts directory, which won’t be built by Jekyll since its name starts with an underscore.
before_install
Before starting any of the process we need to make sure that we have set up our environment by installing our gem files, the Node.js version we need through nvm (which the Travis image already ships with) and all of our global npm modules. Necessary files and folders will be brought up from the cache when needed. Afterwards, we execute the install.sh file which contains:
The openssl command decrypts the encrypted private key. I’ve actually just copy-pasted what travis encrypt-file added to .travis.yml earlier on.
Also, note that I’m moving the key to the .ssh directory under the name id_rsa. This is the default name for the key SSH will look for when Git pushes to the server. It makes our lives a bit easier to place it there, under that name, since we won’t need to specify what key should be used later on.
before_script
A common problem I hit when trying to connect with SSH to my server was having SSH read properly from the known_hosts whenever I try to connect. However, there seemed to be a problem that I could not wrap my head around until I came across this article. As a result, I add a Host entry for the server to ~/.ssh/config that sets its address and SSH port and disables StrictHostKeyChecking, so that Travis can log in properly with this one command.
build
The build is straightforward for me as I have everything configured in Grunt. My build.sh file looks like:
test
Testing is done with HTML-Proofer. Since it is in my Gemfile, Travis automatically installs it for us. My test file is configured as:
In this one command, I’m validating HTML, checking that no external link returns a 4xx error (ignoring any redirects as they are fine), and that the favicon is present and referenced on every page. I ignore my blog address URL and GitHub's with --url-ignore "/assaf.website|github.com/" as whenever you’re adding a new post, it’ll return an error because the post isn’t online yet (I do that also for GitHub as I link as well the posts to my GitHub repository).
deploy
At this point, we want to deploy what we’ve just built. That’s why we’re going to push from a new repo in our _site folder.
We’re adding our remote with the deployment username that we have configured earlier, through the blog-server host entry from ~/.ssh/config, on the path where our .git directory is. We commit everything and push it using the --force. This is necessary, since this new repo isn’t strictly linked to our remote; it doesn’t have the same history or anything. This argument just tells Git to ignore that fact.
Feel free to ask or check out my GitHub repo to check how everything is set up in action!
The opinions and views expressed on this blog are solely my own and do not reflect the opinions, views, or positions of my employer or any affiliated organizations. All content provided on this blog is for informational purposes only.
I also offer consulting and mentoring in AI strategy, engineering leadership, and scaling teams. Get in touch if I can help.