How to build Jekyll project production with Windows command line - jekyll

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

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 %}

On windows you should run two commands:
the first command set env to production
set JEKYLL_ENV=production
the second command run jekyll build or jekyll server
jekyll build
when you use development evn, run this command again:
set JEKYLL_ENV=development

This worked for me:
set JEKYLL_ENV=production | jekyll build
Running the two commands separately like Thxopen suggested worked. Then I tried running the two commands on one line using the separator character as mentioned here, and it worked nicely.

Related

Context variable for Jekyll in Netlify

Jekyll to detect the build waits the JEKYLL_ENV variable: https://jekyllrb.com/docs/configuration/environments/
Netlify sets the CONTEXT variable: https://www.netlify.com/docs/continuous-deployment/?_ga=2.28134843.62454114.1555938051-984068889.1555938051#environment-variables
As result Jekyll doesn't see that build i
Netlify's CONTEXT variable is not intended to be the same as Jekyll's JEKYLL_ENV. So, you'll want to set JEKYLL_ENV separately, probably using netlify.toml? Something like this may work:
# this will be the default for every branch other than the production branch
[build.environment]
JEKYLL_ENV="development"
# only for production branch, override to production
[context.production.environment]
JEKYLL_ENV="production"
I had the same issue and sorted it by adding JEKYLL_ENV in the environment variables in Netlify settings page. No need to add a toml file.
I successfully tested three different ways to set the JEKYLL_ENV environment variable, which can then be accessed in Liquid templates as jekyll.environment:
netlify.toml: Set the env var in the build command directly
# global context
[build]
publish = "_site/"
command = "JEKYLL_ENV=\"netlify\" jekyll build"
netlify.toml: Set a build environment
# global context
[build]
publish = "_site/"
command = "jekyll build"
environment = { JEKYLL_ENV = "netlify" }
netlify.toml: Run a custom build script
# global context
[build]
publish = "_site/"
command = "sh netlify.sh"
netlify.sh: Set the env var in the build command
JEKYLL_ENV="netlify" jekyll build

Jekyll custom theme (running locally)

I'm new to Jekyll and I am trying to get a custom theme up and running. Here's is what I've done so far:
Created my Jekyll site. CD to the directory I wanted to install it and ran
bundle exec jekyll serve
These files were created and I was able to see the site locally at the default 4000 port.
I then tried following the instructions here for installing your own theme. I entered this in my terminal:
$ jekyll new-theme skull_and_roses
As the instructions indicated it built out a new directory...
It also added a directory in the _site directory, not sure if that is correct:
I then followed these instructions:
But when I go to run it:
bundle exec jekyll serve --watch
I get an error:
The skull_and_roses theme could not be found.
Like I said, this is my first run at Jekyll so any help would be appreciated.
I also use jekyll theme template (It is a nice template with friendly manual) and customize it to set up my own github page recently.
Beside create repo on github use username (username.github.io), What I did on my mac (locally) are:
set up env for using Jekyll, you can reference: https://jekyllrb.com/docs/
$ git clone https://github.com/username/username.github.io.git (assuming you have already create the repo).
$ cd username.github.io
$ git clone <theme github repo>
put all the theme files into the root of your website files (dir we create in step2)
usually the theme template will have Gemfile, if really not, you can try to create Gemfile and type in:
source 'https://rubygems.org'
gem 'github-pages', group: :jekyll_plugins
After you confirm you have Gemfile, Run
$ bundle install
$ bundle exec jekyll serve
Now, you can enter localhost:4000 or 127.0.0.1:4000 to check the theme can run on local serve.
Then you can mainly modify _config.yml file, like title, author, and other from the theme template instructions. You can check it locally(localhost:4000 or 127.0.0.1:4000) whenever you update something and you want to check the result. Usually changing in _config.yml, you need to restart the jekyll serve (using ctrl+c to stop and run $ bundle exec jekyll serve to restart the service to check the modification. You may need to modify more than _config.yml file to meet your own requirement, at least like about.md or add your own posts in _post.
After you finish modification from theme template and make your own github page you like. You can push the local repo to remote repo (master branch of username.github.io). Btw, if you work locally, you can use branch to test features you want to add, checking result locally and then merge to master when you are satisfied with the result.
Finally, you can check: https://username.github.io and enjoy your own github page.
For your question about _site and other things you may want to know, you may also want to check followings:
Creating and Hosting a Personal Site on GitHub
Quick start & tutorials on jekyll

Run jekyll build Command with Different url Flag

My current _config.yml file looks like this:
#Site settings
...
baseurl: "" # the subpath of your site, e.g. /blog/
url: "http://10.0.1.9:3000" # the base hostname & protocol for your site
...
# Build settings
markdown: kramdown
safe: true
lsi: false
highlighter: true
keep_files: [public]
exclude: [src, lib, node_modules, bower.json, Gemfile, gulpfile.js, package.json, README.md]
I've got my url currently set to my local server, where I serve my Jekyll site for local development.
When building for production however, I have to keep manually changing this url to the url of my remote server before running jekyll build. Is there a way I can pass my remote url alongside the jekyll build command to build a site with the correct remote paths?
Something like so:
jekyll build --url mysite.com
Any help is appreciated with this. Thanks in advance!
Put your production url in _config.yml eg: url: toto.com.
Create a _config_dev.yml that will be used to override values in development.
In you case
url: "http://10.0.1.9:3000"
Development build is launched with :
jekyll build --config _config.yml,_config_dev.yml
Values in the last config file in the command will override those in first file.
and production build with jekyll build.
See Jekyll documentation http://jekyllrb.com/docs/configuration/#build-command-options configuration paragraphe.

How to detemine if jekyll running locally or in production site?

There is a config param in jekyll called production_url. I can't find any information on how to use it.
Ideally i would like to be able to generate permalinks with local url when it is being run with serve param and with production url when run with build param.
How could I do that?
When you build your Jekyll website, it’s possible to specify the environment it’s using for the build with the JEKYLL_ENV environment variable:
$ JEKYLL_ENV=production jekyll build
If you don’t set JEKYLL_ENV explicitly, it defaults to development.
{% if jekyll.environment == "production" %}
// Production environment.
{% endif %}
Github Pages automatically sets the environment to production.
I don't see the variable production_url in the current release (v1.4.1), so this may be a dated question--but I was just looking for this answer myself. There is a baseurl property that can be set with a flag and so to change the path to your files, but it only adjusts the relative path.
jekyll serve --baseurl '/blog'
What you can do is to use the -config option to specify a configuration file for development.
Jekyll Documentation
Your production configuration variables are defined in _config.yml. One option is to create a separate configuration file for development.
--config _config-dev.yml
You can also (as I do) override variables defined in a second configuration file.
--config _config.yml,_config-dev.yml
If you use the liquid templates for site links like this:
<link rel="stylesheet" href="{{ site.base_url }}/stylesheets/blog.css">
then you can override the base_url property during local devlopment
base_url: http://localhost:4000
and run jekyll in "Development"
jekyll serve -w --config _config.yml,_config-dev.yml
jekyll serve will call jekyll build, so you can't really use those two as a way to output different URL schemes.
I built a Jekyll plugin that does this with a Liquid Filter and one user defined variable in your _config.yml called mode.
You set the mode to development or production and the plugin takes care of the rest in your templates.
So in your template you could have a URL such as:
<img src="{{ '/img/dog.jpg' | to_absurl }}" />
When mode is development, upon Jekyll build/serve you will get a relative URL:
<img src="/img/dog.jpg" />
Locally this would be accessed as: http://0.0.0.0:4000/img/dog.jpg
When mode is production, upon Jekyll build/serve you will get an absolute URL:
<img src="http://www.domain.tld/img/dog.jpg" />
The http://www.domain.tld is what you have set in _config.yml -> url variable.
You can see more details on the plugin here:
http://jhaurawachsman.com/2013/jekyll-url-helper-filter-plugin/
This also worked for me:
$ JEKYLL_ENV=production jekyll serve

Auto regeneration is not working with _config.yml?

I installed jekyll and run it from cli:
jekyll --server --auto
But when I change _config.yml I can't see that my changes where applied and I have to restart server every time.
I really need to restart server every time when I change _config.yml? Or there is some workaround?
I believe you are correct; that changes to _config.yml always require restarting the server.
After all, _config.yml provides options that you can override in the call to Jekyll. So it is unclear how auto should behave if an option is called on the command line run of jekyll which overrides some of the _config.yml settings, and then the config is edited. Which would take precedence then? the _config.yml or the original command line argument?
If you have the server running, you don't need to restart it. A (little bit) quicker way to is to run jekyll build or bundle exec jekyll build after editing your _config.yml file.
Parker Moore, maintainer of Jekyll, confirms that _config.yml changes are not autogenerated or picked up by a running, watching Jekyll server.
No I remember why we can't do this: if you change either source or destination, you're eff'd, so we said 👎 to this suggestion. Definitely use _data if you're using custom data. Otherwise, a quick ^C and restart should be 👌
https://github.com/jekyll/jekyll/issues/2302#issuecomment-43160557
I really need to restart server every time when I change _config.yml?
Or there is some workaround?
There is some workaround:
1) Install watchy.
npm i -D watchy
2) Restart on config change (for example as a package.json script)
"scripts": {
"restart": "watchy -w _config.yml -- bundle exec jekyll serve --drafts --watch",
(--drafts is unrelated, but I am assuming you do this locally in dev/authoring mode...)