With `server` deprecated, how can I reliably tell if I'm in Dev or Prod mode? #1219

Closed
philipwalton opened this Issue Jun 16, 2013 · 17 comments

Comments

Projects
None yet
9 participants

Before the release of version 1, I could check if site.server was true and then include development only features (or exclude production only features) based on that conditional.

Here's a simple example:

{% if site.server %}
  {% include local-testing-scripts.html %}
{% endif %}

But now it seems that I have no way to reliably do this check (unless I'm missing something).

The only thing I can think of is to set some random config variable (like port), and then make the assumption that I'm in dev mode unless port is set to whatever number I choose to mean production mode.

But that seems hacky. Is there an officially supported way to do what I'm after?

Owner

mattr- commented Jun 16, 2013

No, there isn't. When jekyll generates a site, it doesn't care about things
like development vs. production environments. You could set a custom
variable in your _config.yml and get the same effect. If I remember
correctly, we do pass anything through that we find in there.

On Sun, Jun 16, 2013 at 6:48 PM, Philip Walton notifications@github.comwrote:

Before the release of version 1, I could check if site.server was trueand then include development only features (or exclude production only
features) based on that conditional.

Here's a simple example:

{% if site.server %}
{% include local-testing-scripts.html %}
{% endif %}

But now it seems that I have no way to reliably do this check (unless I'm
missing something).

The only thing I can think of is to set some random config variable (like
port), and then make the assumption that I'm in dev mode unless port is
set to whatever number I choose to mean production mode.

But that seems hacky. Is there an officially supported way to do what I'm
after?


Reply to this email directly or view it on GitHubhttps://github.com/mojombo/jekyll/issues/1219
.

Right, but if it's in _config.yml it'll show up when I do both jekyll serve and jekyll build, which defeats the purpose. I also can't pass in a config variable via the command line (e.g. jekyll serve --env dev), so that won't work.

Anyway, thanks for the quick reply. I guess I have my answer. However, my personal opinion is that an environment variable would be very useful to a lot of people.

Owner

parkr commented Jun 17, 2013

Someone asked about this before – I recommended that they use a plugin to handle this:

$ JEKYLL_ENV=production jekyll build
class Jekyll::Site
  attr_accessor :env
end

class Env < Jekyll::Generator
  priority :highest
  def generate(site)
    site.env = ENV['JEKYLL_ENV'] || 'development'
  end
end

Or something like that.

parkr closed this Jun 17, 2013

@parkr, thanks that's helpful.

mdo commented Jul 19, 2013

@parkr What about folks who cannot use plugins with GitHub Pages?

Owner

parkr commented Jul 19, 2013

@mdo What you generate locally is also a valid Jekyll site so push your _site!

mdo commented Jul 19, 2013

@parkr I fully understand that any local Jekyll server is a valid Jekyll site that can use plugins, but as I understand it GitHub Pages triggers --safe on deploy, thus nullifying plugins. My use case is for Bootstrap where I want to show our ads and analytics code only in production and not in local development. I'd rather not have to add to the deployment process if I can avoid it as that's the primary appeal of Jekyll in GitHub Pages.

@mdo that was my use-case as well. I didn't want my Google Analytics code to run when developing locally, but obviously wanted it to run when deployed. And for at least one of my sites, I couldn't use a custom plugin for the reason you just mentioned.

The (kinda hacky) solution I ended up going with was to check site.port in my conditional. I wrote a rake task to always preview my site on port 1234. That allows me to say: insert this Google Analytics snippet unless site.port is 1234.

Member

ixti commented Jul 20, 2013

Why not add environment flag (and respect JEKYLL_ENV) environment variable out of the box. This is a trivial, safe change which will make life easier for lots of people. Like we have --drafts flag which is also helpful for local-only development.

Owner

parkr commented Jul 20, 2013

I'd be happy to add that. I get the appeal with regard to site URL and several other variables. @benbalter, think we could add an "jekyll_env" option which defaults to "development" and can be overwritten as "production" in GH Pages's config?

Contributor

benbalter commented Jul 20, 2013

Great question. Two possible routes:

1. Detecting GitHub Pages

When Pages builds a Jekyll site, there are certain settings we override by default. It's not currently documented, but there's already a flag we use internally which you're welcome to use:

GH_ENV: gh_pages

If you look for site.GH_ENV, you should be able to tell if you're on Pages, although, we can't guarantee that will be there forever. If there's a better flag, glad to add it to the documentation. Easy enough fix.

2. Detecting Development

The other, arguably smarter route, that more closely matches the original behavior would be to pass a development config file. Let's say your standard _config.yml has environment: production in it. Awesome. That will be available to you on Pages (or whenever you run jekyll build). Now let's say you also have a _config_dev.yml with environment: development. To develop, you'd run jekyll serve --config _config.yml,_config_dev.yml, and you'd get site.environoment as development locally and production in "production". Bonus points, you can even script it.

An env var

Seems simple enough to expose something if Jekyll is running as jekyll serve which presumably is always going to be locally. If the site's on localhost:4000, Jekyll should be able to detect that and expose that within site, right?

. My use case is for Bootstrap where I want to show our ads and analytics code only in production and not in local development.

@mdo ping me, more than glad to help out with this.

parkr reopened this Jul 20, 2013

Contributor

robwierzbowski commented Jul 21, 2013

We use @benbalter's option 2 in the Yeoman generator for Jekyll and it works quite well (code here).

Owner

mattr- commented Jul 21, 2013

I'm 👍 on the double config file option.

Owner

parkr commented Oct 18, 2013

What do we think about this?

This was referenced Nov 24, 2013

Owner

parkr commented Jul 31, 2014

Use {{ jekyll.environment }}.

parkr closed this Jul 31, 2014

@parkr sorry to dig up an old issue, but i'm wondering if you know whether {{ jekyll.environment }} is set to "production" on github pages?

Owner

parkr commented Jun 9, 2015

@WickyNilliams Yes. You can test by building a simple site with just that one liquid variable. Deleting the repo will remove the site, so tests are cheap.

parkr locked and limited conversation to collaborators Jun 9, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.