Search code examples
jekyll

How to build Jekyll project production with Windows command line


I try to build my jekyll project on production mode using JEKYLL_ENV variable but it doesn't work.

Jekyll documentation specifies to set a production environment in the build command :

JEKYLL_ENV=production jekyll build

But on Windows, this type of syntax is not correct. I used this following syntax, but it looks not working:

jekyll build JEKYLL_ENV=production

I also set 'manually' this environment variable but doesn't take effect :

setx JEKYLL_ENV production & jekyll build

and

set JEKYLL_ENV=production & jekyll build

Solution

  • I ran into this as well with my Windows/Jekyll setup. My workaround is to have production and development config files and set the environment variable in each file.

    // _config.yml
    environment: production
    ...<other config settings>...
    
    --------
    
    // _config_dev.yml
    environment: development
    

    Your prod environment should run jekyll build which automatically uses _config.yml. Your dev environment should run jekyll <command> --config _config.yml,_config_dev.yml. In the Jekyll config docs, "Settings in later [config] files override settings in earlier files." So, you can set the variable in prod and dev config files and use --config _config.yml,_config_dev.yml to set the variable in dev.

    To do stuff with it in Jekyll, use Liquid statements to check for the environment variable. Config files set site variables, so check site.environment

    // some file that will be processed by Jekyll
    {% if site.environment == "production" %}
        <do prod stuff>
    {% elsif site.environment == "development" %}
        <do dev stuff>
    {% endif %}