| 10 min read
I had a very old unused domain name lying around and I did not really know what to do with it. I didn't want to spend money on hosting just for hosting a site which will not be updated / changed that frequently. For some reason, I wanted it to be a static website with a little power to it. Jekyll was the first thing in my mind. Jekyll with Github Pages is just AWESOME however a techie inside me always want to have little control over things . With Github Pages and Jekyll, I found few limitations as per my need.
Here's why I chose Firebase over Github Pages.
This is how the process roughly looks like -
So let's build a Jekyll site which gets built and auto deployed upon commit.
Following things are required (at least on Windows). I have provided links for download.
Before moving ahead, let's see how we can set up Jekyll blog. I am covering steps required for Windows platform but you can easily find steps for other platforms online.
choco install ruby -y | |
gem install bundler | |
gem install jekyll |
> jekyll new my-site (you can replace my-site with your project name)
If you can see the jekyll site on provided server address. Congrats! You have a jekyll site up and running on your local machine. Next step is to set up Firebase project
Now, we need to add a firebase configuration which will let TravisCI know the desired public path for Jekyll site. Before doing that, we need to create a Firebase project.
Now let's go back to our local jekyll site folder. We will install Firebase CLI. Firebase CLI can be used to deploy code and assets to our firebase projects, preview firebase websites locally or even interact with database from our development machine.
As we are relying on Travis CI, we are using Firebase CLI only to create a basic configuration file.
> npm install -g firebase-tools
> firebase login
> firebase init
The steps above create two files under root folder of jekyll site named firebase.json and .firebaserc. The file firebase.json contains information about public folder, route rewrites whereas .firebaserc holds information about firebase project -
{ | |
"hosting": { | |
"public": "_site", | |
"rewrites": [ | |
{ | |
"source": "**", | |
"destination": "/index.html" | |
} | |
] | |
} | |
} |
Now, we are all set with Firebase configuration. Wasn't that easy? Firebase CLI has made life very easy .
Now just for fun, try running command "firebase deploy". This will auto deploy your site to Firebase and you can see it live on the provided Hosting URL -
This is super cool. However, we don't want to run the commands manually every time we write something down. Ideally, I would want to write a markdown and commit and forget about publishing part. Travis CI comes in picture here. We want to trigger it on every git commit.
We have the Jekyll source ready. Let's move it to GitHub. As a software programmer and a part time blogger, I like to maintain history of all the changes overtime. I find Github the best place for our jekyll site content to rest. Why? Github has wide variety of third party connectors to automate the build processes. Travis CI is one of them.
Some of the reasons why I prefer it to be under source control, especially Github.
We already have git installed on our local machine. Below steps will move our Jekyll source to Github Repository
git init | |
git add . | |
git commit -m "First commit" | |
git remote add origin {remote_repository_url} | |
git remote -v | |
git push origin master |
(Replace {remoterepositoryurl} with github repository url)
We can now move ahead and start configuring Travis CI.
So far we have covered two steps from our earlier process chart.
Now comes an interesting part. We will use TravisCI to build and deploy Jekyll site to Firebase.
Travis uses YAML script which is very easy to understand. Once you flip the switch and make the repository available for build triggers. Travis will look for travis.yml file under provided repository. The travis.yml file contains all the required commands to install dependencies and build Jekyll site. I have prepared the script which is fairly easy to understand. We will just copy it in our Jekyll site source.
#use node_js | |
language: node_js | |
#define node js version to use | |
node_js: | |
- "7" | |
#choose branch on commit of which this script should excecute | |
branches: | |
only: | |
- master | |
#set notifications frequency | |
notifications: | |
email: | |
on_success: never | |
on_failure : change | |
before_install: | |
#install rvm 2.2 | |
- rvm install 2.2 | |
#use rvm 2.2 | |
- rvm use 2.2 | |
#set home varaibles | |
- . $HOME/.nvm/nvm.sh && nvm install 6.1 && nvm use 6.1 | |
#install gems through bundler | |
- gem install bundler | |
#if gems are missing this script will get the missing gems. | |
- bundle check || bundle install | |
install: | |
#install firebase tools, required to deploy on firebase hosting | |
- npm install -g firebase-tools | |
# Assume bundler is being used, therefore | |
# the `install` step will run `bundle install` by default. | |
script: | |
#continue even after error | |
- set -e | |
#build jekyll site | |
- jekyll build | |
after_success: | |
#deploy to firebase using stored token. | |
- firebase deploy --token "$FIREBASE_API_TOKEN" | |
env: | |
global: | |
- NOKOGIRI_USE_SYSTEM_LIBRARIES=true # speeds up installation of html-proofer | |
sudo: false # route your build to the container-based infrastructure for a faster build | |
#Visit article at - http://wrapcode.com | |
#Cheers, Rahul. | |
While this script is quite explanatory. I will try to explain what it does.
Now we need to add this file to Github repository. Following git commands will add the file to repository.
git add . | |
git commit -m "added travis.yml" | |
git push origin master |
This will add the provided travis file to git
We always have a logged in user on our local machine. Firebase CLI manages to store the context of logged in user. However, the build agents are containers and they get recreated every time a build triggers.There is no definite way to store the user context. Fortunately, Firebase CLI has given us an option to use tokens for deployments. Once generated, these tokens can be used as secret keys for deployment. These are specifically made for CI purpose. To generate Firebase Token, you need to go to your project root folder on your local machine and run this command -
> firebase login:ci
This will redirect user to browser and we will be asked to log in and grant the permissions. Once we are logged in we will see below message in console.
You have got the token. But wondering where to store it? Obviously you can not keep it in plain sight in build script. We will use Travis environment variables to store the CI token.
Now try triggering the build. The build should pass with a success message.
That's it. You have configured the later two stages of the build process. This is how the continuous delivery will work -
Source code and site -
Github source code - https://github.com/r4hulp/my-jekyll-site
Travis CI - https://travis-ci.org/r4hulp/my-jekyll-site