Jekyll with environment variable and multiple _config.yml files

October 17, 2015 by Sandeep Bhardwaj | Tags:


When we write a new post in jekyll we usually made changes in _config.yml for our local/development environment like :-

Development environment

#url: http://sandeepbhardwaj.github.io
url: http://localhost:4000

google_analytics: #UA-68409070-1
disqus_user: #sandeepbhardwaj

Production environment

url: http://sandeepbhardwaj.github.io
#url: http://localhost:4000

google_analytics: UA-68409070-1
disqus_user: sandeepbhardwaj

But doing this every time is pain. Sometimes we forgot to revert the changes when push our changes to github. So i Google it and find out that there is jekyll.environment variable that we can use.

<% if jekyll.environment == "production" %>
   <% include disqus.html %>
<% endif %>

so it means i have to made changes in _layout and _include file. I personally don’t like this approach because i dont want to change my existing code.

Below are the two approaches which i liked most.

Approach 1

Creating environment specific _config.yml files

Finally, i find out a best way of doing this and just copy paste the existing _config.yml and rename it to _config-dev.yml and made changes for local environment.

Jekyll provide a best way of providing the config file explicitly using the command line like:-

jekyll serve -w --config _config-dev.yml

Approach 2

Overridding the default values

There is also a alternative approach for overriding the exiting properties with default one if you do not want to add unnecessary config in new _config-dev.yml

jekyll serve -w --config _config.yml,_config-dev.yml

Note :-Make sure in that case just add those config those need to be override only. Example:-

url: http://localhost:4000

google_analytics: #UA-68409070-1
disqus_user: #sandeepbhardwaj

and when i push my site to github it automatically pick the default config file _config.yml and build the site using default one.