How can we use environment variables in a Jekyll configuration file?

Is there a way that I can use a single bash environment variable (say $FOO ) in my Jekyll _config.yml file?

I tried using:

 foo = <%= ENV['FOO'] %> 

But this did not work, since the Ruby code was not interpreted.

Used Versions:

  • Ruby: 2.1.2
  • Jekyll: 2.5.3
+5
source share
6 answers

@Elryco's answer is close, but not entirely correct, at least for my installation. It took a few trial and error, but in the end it worked.

Please note that you need the jekyll-contentful-data-import gem (v1.7.0 or higher) for this solution to work.

Bash environment (e.g. ~/.bash_profile ):

 export CONTENTFUL_ACCESS_TOKEN=foo export CONTENTFUL_SPACE_ID=bar 

In _config.yml specify them as:

 contentful: spaces: - example: space: ENV_CONTENTFUL_SPACE_ID access_token: ENV_CONTENTFUL_ACCESS_TOKEN 

This is the same as written in the Github documentation.

+1
source

Recently I had to try to do this. Turns out you can't put environment variables directly in the Jekyll configuration file, but you can write a rake task that will take the environment variables and apply them to your configuration.

Here is an example:

 # Rakefile require 'jekyll' task default: %w[build] desc "Build the site" task :build do config = Jekyll.configuration({ url: ENV["SITE_URL"], }) site = Jekyll::Site.new(config) Jekyll::Commands::Build.build(site, config) end 
+1
source

If your goal is to use environment variables as fluid elements {{ site.something }} , you can get this thing in your Gemfile :

 gem 'jekyll-environment-variables', group: :jekyll_plugins 

And then you can use {{ site.env.HOME }} and expect it to be converted to something like /home/ubuntu in the HTML output.

Disclosure : I am the owner of the gem, and I use it personally for a long time.

+1
source

Unfortunately, direct access to them in liquid tags is absent, at least officially.

But I wrote a wrapper script that reads the environment variables before running jekyll and adds it to the _config.yml file and removes the post post assembly.

  echo "secret-variable: $PASSWORD" >> _config.yml bundle exec jekyll build -d target sed '$d' _config.yml //this is to delete the last line 

Now I can use site.secret-variable anywhere on the liquid labels.

I know that this is not the right way to do this. But so writes own ruby โ€‹โ€‹script.

0
source

Personally, I find using the ruby โ€‹โ€‹Jekyll plugin more appropriate and portable. A very simple but effective solution is available here.

The basic idea is that ruby โ€‹โ€‹will have access to ENV variables, so you can use the small ruby โ€‹โ€‹plugin to load all the information you need from the environment into your site.config fluid. And you can define default values.

Please note that the example provided in the link is not the most relevant, since the prod / staging environment was already proposed by Jekyll initially with options for the build command .

0
source

If your bash environment variables are declared as follows

 export ENV_ACCESS_TOKEN=xxxxx export ENV_SPACE_ID=yyyyyy 

You can do this in your config.yml

 space: ENV_SPACE_ID # Required access_token: ENV_ACCESS_TOKEN # Required 
-4
source

Source: https://habr.com/ru/post/1247950/


All Articles