mbigras mbigras - 3 months ago 10
Git Question

How to avoid having to change site.baseurl -> site.github.url between master/gh-pages?

I'm using github to host my blog built with jekyll.

I read in another post and the documentation that I need to change

site.baseurl
to
site.github.url
to get my static resources to be served. So that's what I did and now everything is working. See diff below between
master
and
gh-pages
:

enter image description here

It wasn't too much of a pain to do, I just project-wide replaced using atom; however, I'm wondering, is there a better workaround? Ideally I'd like my workflow to be so I can just work on my blog using a normal branching model and then merge with
gh-pages
as if it's a release branch and not have to worry about making the search and replace every time.

Thanks for your help :)

Edit: So strange, I just merged
master
with
gh-pages
and it seems like git just automagically handled the whole thing for me. So different question. Does git really know not to change
site.github.url
to
site.baseurl
when I make the merge? How does that work?

Answer

You can do this by using jekyll.environment:

{% if jekyll.environment == "production" %}
  <a class="post-link" href="{{ post.url | prepend: site.github.url }}">{{ post.title }}</a>
{% else %}
  <a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
{% endif %}

In your build command you then have to set JEKYLL_ENV=production . This is done automatically by GitHub. For other platforms, you may have to do this manually (in a Rakefile for example):

JEKYLL_ENV=production jekyll build

By default, JEKYLL_ENV equals development.