In Jekyll, how to exclude files based on the JEKYLL_ENV? - jekyll

This seems like a very basic question but I can't find the answer :-S
I exclude like this:
exclude:
- "*.json"
- "Gemfile*"
- "*.txt"
- vendor
- README.md
- somefile.html
Pretty straightforward. To create the production build I run: $ JEKYLL_ENV=production bundle exec jekyll build
How can I exclude the somefile.html file only when I run the production ENV?

Perhaps you could try using a specific config file for production and one for development. In the production you could exclude the files using exclude as described here.
Then run:
jekyll build --trace --config _config.yml,_config_dev.yml
or
jekyll build --trace --config _config.yml,_config_prod.yml
In the _config.yml you'd set generic settings, and in the config with the environment suffix you'd set a environment specific configuration.
The trace flag is optional, it will help setting it up since it will show occuring errors.
optional: -t, --trace Show the full backtrace when an error occurs

Related

`jekyll build` has error but gitlab ci still continues

I have a Jekyll website running on Gitlab Pages, and one of my pages contained an error.
This error is found and reported by Jekyll, but the website is still deployed, just without that page.
In Gitlab's job output I see the following:
$ jekyll build -d public
...
Bundle complete! 5 Gemfile dependencies, 29 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux-musl]
Configuration file: /builds/TheOddler/mjb/_config.yml
Source: /builds/TheOddler/mjb
Destination: /builds/TheOddler/mjb/public
Incremental build: disabled. Enable with --incremental
Generating...
Error: YAML Exception reading /builds/TheOddler/mjb/_work/immaculate.md: (<unknown>): did not find expected key while parsing a block mapping at line 2 column 1
Jekyll Feed: Generating feed for posts
done in 1.104 seconds.
Auto-regeneration: disabled. Use --watch to enable.
Uploading artifacts for successful job 00:09
Uploading artifacts...
So it shows Error: YAML Exception ... but then still continues as if everything is OK.
The job in my .gitlab-ci.yml file looks like this:
build-jekyll:
stage: build
image: jekyll/jekyll:4.2.0
script:
- jekyll build -d public
artifacts:
paths:
- public
only:
- master
Why does this happen? And is there a way to make the process fail, or at least report this as a warning so I know something went wrong?
Thanks!
This happens because Jekyll, dispite the error in the output, returns with exit code 0. You could try to add the --strict_front_matter option to let Jekyll return a non-zero exit code.

jekyll serve throws 'no implicit conversion of Hash into Integer ' error

I am following Michael Hartl's Learn Enough CSS course. My current folder layout is as follow:
- _layouts
- _site
index.html
where index.html is:
---
layout: test
---
and I have test.html in _layouts as:
Hello again, world.
Whenever I run jekyll serve, I get this error:
Error reading file /Users/pj/Documents/LETGD/repos/pohjie.github.io/_layouts/test.html: no implicit conversion of Hash into Integer
Error reading file /Users/pj/Documents/LETGD/repos/pohjie.github.io/index.html: no implicit conversion of Hash into Integer
Does anyone have any idea what's happening? I'm using M1 MacBook, not sure if that is a possible cause as I spent a lot of time install Ruby as well.
Thanks!
While downgrading certainly works, it may be pretty annoying and (depending on where you need this) problematic. If you just want a simple workaround, you can make use of the fact that jekyll build still works with Ruby 3 and just serve the page separately:
bundler exec jekyll build && bash -c 'cd _site && python -m http.server 3000'
The only downside of this is that you lose the automatic reload. If you change anything, you need to restart jekyll. But you can run this in a Ruby 3 environment without fiddling with the environment itself.
Quoting from this source:-
Github-Pages uses Jekyll 3.9, which isn’t compatible with Ruby 3.
Downgrading to Ruby 2.7 should avoid the problem.
This worked for me.
Downgrading to Ruby 2.7 is an option (as others have said) which I didn't feel like doing, so I did this instead:
Apply the patch to pathutil
From Liviu Stefan's answer:
Ruby 3.0 deprecated using the last argument as keyword parameters. A double splat ** has to be added before the variable for the behavior to be supported.
Here's how I applied the patch locally:
sudo sed -i.bak 's/, kwd/, **kwd/' $(gem which pathutil)
After that I got another error:
/var/lib/gems/3.0.0/gems/jekyll-3.9.0/lib/jekyll/commands/serve/servlet.rb:3:in `require': cannot load such file -- webrick (LoadError)
Which I fixed by running (from bundle exec jekyll serve: cannot load such file):
bundle add webrick
Ruby 3.0 deprecated using the last argument as keyword parameters. A double splat ** has to be added before the variable for the behavior to be supported.
It's fairly straightforward to amend locally; the relevant patch is found: here
Which needs to be applid to:
/home/<your_user_name>/gems/gems/pathutil-0.16.2/lib/pathutil.rb

Can a Jekyll plugin / generator be disabled for non-production environment?

I have an custom generator written for creating archive pages (per year, month and day) for my Jekyll blog. But that generator takes some time, can I disable a generator for non-production environment or maybe from the plugins entry in the config?
UPDATE: The plugin is just placed in the _pluginsfolder, it is not delivered / distributed as gem package!
I am looking for a kind of blacklisting of generators (and maybe also filters or other plugins) for development environment to save building time.
Local Plugins
For switching local plugins, you could use two different folders, and switch between them with a combination of multiple config files overwriting your plugins_dir.
You would set the a different value for plugins_dir field in the _config-dev.yml file which would overwrite your value in _config.yml (or the default _plugins if unset):
$ bundle exec jekyll build --config _config.yml,_config-dev.yml
This way you can have two folders with your development and production plugins separate. This has a maintenance cost when you use a plugin across both environments.
Gemfile Plugins
For switching gem-based plugins, you could use a different Gemfile for development without the production plugins:
BUNDLE_GEMFILE=Gemfile-dev bundle exec jekyll build
This gives you a lot of flexibility at the cost of maintaining two files. You'd want to ensure the versions of plugins across both are the same.
Alternatively, you could use an additional config file for development. You would set the a different value for plugins field in the _config-dev.yml file which would overwrite your value in _config.yml. You'd need to ensure your plugins are not set in the :jekyll_plugins group in your Gemfile for this to work (as this would shortcut the config setting):
$ bundle exec jekyll build --config _config.yml,_config-dev.yml
General Performance
If your site has a large number of posts, it's likely that your biggest time saving would be made by processing less of them at development time. You can do this with the limit_posts command line option (https://jekyllrb.com/docs/configuration/options/#build-command-options):
$ bundle exec jekyll build --limit_posts 5
For general build time improvements, I'd highly recommend profiling your site to find the best place to optimise:
$ bundle exec jekyll build --profile

Liquid Exception: invalid byte sequence in US-ASCII in _layouts/redirect.html

I was using gitlab build of jekyll project, all of a sudden started receiving following error. Not able to solve after a lot of trials. What to do in .gitlab-ci.yml file to resolve this (*error not appearing in local machine, both .gitlab-ci.yml and locally uses "grunt build" command).
Liquid Exception: invalid byte sequence in US-ASCII in _layouts/redirect.html
This solution worked for me.
Put:
before_script:
- apt-get update >/dev/null
- apt-get install -y locales >/dev/null
- echo "en_US UTF-8" > /etc/locale.gen
- locale-gen en_US.UTF-8
- export LANG=en_US.UTF-8
- export LANGUAGE=en_US:en
- export LC_ALL=en_US.UTF-8
In before_script directive in your .gitlab-ci.yml.
Solved this very tricky issue by following:
Disable & Re-enable GitLab CI Runner
Deleted Gemfile.lock from the Runner (Commit)
Deleted folders node_modules and bower_components
Re-compiled project using last successful .gitlab-ci.yml and GemFile
Replaced .gitlab-ci.yml and GemFile to latest versions

Jekyll: Specify whether a plugin is active in the development environment

In my Jekyll site, I'm using the minify-html plugin. However, it causes the site build time to increase from 2-20 seconds to nearly 5 minutes, which is unacceptable during development. According to the documentation:
After installing, Jekyll's HTML output will be minified by default. If you configure env in your Jekyll configuration, HTML will be minified only when
env is set to production.
env: production
This documentation is useless as it doesn't explain how to configure env. As can be expected, setting env: production in _config.yml has no effect, as it doesn't specify what should vary based on environment. I've spent quite a lot of time searching the internet for how to make this work, but I've turned up nothing. As far as I can tell, env, along with any other method to vary by environment, is totally undocumented in Jekyll.
How can I set minify-html to only run when I call JEKYLL_ENV=production jekyll build?
PS: In case it's relevant, I installed minify-html by adding it to my Gemfile and running bundle.
no env variable set in `_config.yml``=> minification
env: production => minification
env: yolo or whatever or dev => no minification
You can have a development command, using a development config file that will override the default _config.yml. See command options
In a new _config_dev.yml, set :
env: dev
To serve minified : jekyll serve.
To serve unminified : jekyll serve --config _config.yml,_config_dev.yml