How can we use environment variables in a Jekyll config file?
Asked Answered
P

7

10

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

I've tried using:

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

But it didn't work as the Ruby code wasn't interpreted.

Versions used:

  • Ruby: 2.1.2
  • Jekyll: 2.5.3
Psychobiology answered 26/4, 2016 at 13:31 Comment(5)
You could try building a rake task to build your _config.yml programmatically when you publish something to your Jekyll site.Ivy
I thought of it but it's not an ideal solution and i'm curious to know if my question has an answer :)Psychobiology
Fair enough -- that's why I commented and didn't answer ;)Ivy
@DirtyHenry If it's debug vs. release what you are after, consider a selective yml override for debug mode.Section
@FrankNocke it was more of a "how to publish something publicly - on GitHub - and not expose private data" issue. Publishing the name of your environment variable is fine. Publishing its content is not cool.Psychobiology
G
7

If your goal is to use environment variables as liquid items {{ site.something }}, you might be able to get this thing in your Gemfile a go:

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

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

Disclosure: I am the owner of the gem and I've been using it personally since long ago.

Glossator answered 15/7, 2019 at 15:54 Comment(1)
FYI, here's another gem that does what this one does, but also reads from a .env file: rubygems.org/gems/jekyll-dotenv/versions/0.1.0Originate
C
3

The answer by @elryco is close but not quite right, at least for my setup. It took some trial and error, but this finally worked. Note this only works for certain env vars supported by the contentful plugin.

Note that you need the gem jekyll-contentful-data-import (v1.7.0 or up) for this solution to actually work.

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

export CONTENTFUL_ACCESS_TOKEN=foo
export CONTENTFUL_SPACE_ID=bar

In _config.yml, reference them as:

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

This is the same as what's written in the Github documentation.

Cyrillic answered 2/5, 2017 at 1:58 Comment(2)
Not sure, what variables or versions this is limited to (I am on a fresh jekyll/github-pages stack), but i.e. title: ENV_HOME is not working on localhost. (and the HOME environment variable is clearly set).Section
The code checking out the ENV_ belongs to the jekyll-contentful-data-import plugin. So it's not available for every jekyll user.Psychobiology
L
2

I recently had to try and do this myself. It turns out you can't put environment variables directly into a Jekyll config file, but you can write a rake task that will take environment variables and apply them to your config.

Here's 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
Link answered 25/1, 2018 at 22:46 Comment(0)
R
1

Unfortunately there is no direct way of accessing it in liquid tags, At Least not officially.

But I wrote a wrapper script which reads environment variables before jekyll starts and appends it to _config.yml file and deletes the variable post build.

  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'm free to use site.secret-variable anywhere in the liquid tags.

I know that this not the right way of doing it, But so is writing a custom ruby script.

Reddick answered 18/1, 2018 at 21:50 Comment(0)
A
0

I personally find the use of a ruby Jekyll plugin more appropriate and portable. There's a very simple yet effective solution available here.

The main idea is ruby will have access to the ENV variables so you can use a small ruby plugin to load into your site.config liquid array all the information you want from the environment. And you can define default values as well.

Please note that the example given in the link isn't the most relevant since the prod/staging environment is already offered by Jekyll natively with the build command options.

Armageddon answered 4/5, 2019 at 14:48 Comment(0)
B
0

It is now possible to use bash environment variable (say $FOO) in Jekyll's _config.yml file with GitHub Actions:

# _config.yml

title: FOO

Create a bash script say sample.sh to replace for a given input string FOO and replace with another string

# github/workflows/sample.sh

export FOO=XYZ
while IFS='' read -r a; do
    echo "${a//FOO/$FOO}"
done < /_config.yml > /_config.yml.t
mv /_config.yml{.t,}

Create a workflow file, say github-pages.yml, put the script before Build with Jekyll:

# Sample workflow for building and deploying a Jekyll site to GitHub Pages
name: Deploy Jekyll with GitHub Pages dependencies preinstalled

on:
  # Runs on pushes targeting the default branch
  push:
    branches:
      - 'master'
      - 'mybranch'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow one concurrent deployment
concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Setup Pages
        uses: actions/configure-pages@v2
      - name: Utilize FOO
        run: |
          bash .github/workflows/sample.sh
      - name: Build with Jekyll
        uses: actions/jekyll-build-pages@v1
        with:
          source: ./
          destination: ./_site
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v1

  # Deployment job
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v1
Byelostok answered 16/9, 2022 at 14:30 Comment(0)
G
-4

If your bash environment variables are declared like this

export ENV_ACCESS_TOKEN=xxxxx
export ENV_SPACE_ID=yyyyyy

You can get it like this in your config.yml

space: ENV_SPACE_ID # Required
access_token: ENV_ACCESS_TOKEN # Required
Gilbreath answered 14/12, 2016 at 7:29 Comment(7)
What version of Jekyll are you using? I can't reproduce that behavior with Jekyll 3.3.1. The liquid templace will just output ENV_SPACE_ID.Psychobiology
This totally doesn't work - at least out of the box.Bestraddle
This solution works for environment variables in the config.yml Jekyll 3.3.1. To use env variables in your templates you should use another solution. @DirtyHenryGilbreath
@Gilbreath I just did another test and I confirm it doesn't work. Are you using a special version of Ruby? Or a special command-line invocation? Would you mind sharing a project you made it work for?Psychobiology
@DirtyHenry if you declare your environment variable correctly then you can access it with printenv. If they are visible here they are also available for your _config.ymlGilbreath
@Gilbreath Can you share your code on how you do it? Cause it seems mgarciaisaia and I agree it doesn't work. You might miss something in your explanation.Psychobiology
Can confirm that this doesn't work (ruby 2.6.3p62).Chellean

© 2022 - 2024 — McMap. All rights reserved.