我们如何在Jekyll配置文件中使用环境变量?

时间:2022-08-27 23:14:32

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

有没有办法在我的Jekyll的_config.yml文件中使用一个我的bash环境变量(比如$ FOO)?

I've tried using:

我尝试过使用:

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

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

但它没有工作,因为Ruby代码没有被解释。

Versions used:

使用的版本:

  • Ruby: 2.1.2
  • Ruby:2.1.2
  • Jekyll: 2.5.3
  • 杰基尔:2.5.3

4 个解决方案

#1


1  

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.

我最近不得不尝试自己这样做。事实证明,您无法将环境变量直接放入Jekyll配置文件中,但您可以编写一个rake任务,该任务将获取环境变量并将其应用于您的配置。

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

#2


0  

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.

@elryco的答案很接近但不太正确,至少在我的设置上如此。它花了一些试验和错误,但这终于奏效了。

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

Bash环境(例如〜/ .bash_profile):

export CONTENTFUL_ACCESS_TOKEN=foo
export CONTENTFUL_SPACE_ID=bar

In _config.yml, reference them as:

在_config.yml中,将它们引用为:

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.

这与Github文档中的内容相同。

#3


0  

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.

但是我编写了一个包装器脚本,它在jekyll启动之前读取环境变量并将其附加到_config.yml文件并删除变量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.

现在我可以在液体标签的任何地方使用site.secret-variable。

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

我知道这不是正确的方法,但编写自定义ruby脚本也是如此。

#4


-3  

If your bash environment variables are declared like this

如果你的bash环境变量是这样声明的

export ENV_ACCESS_TOKEN=xxxxx
export ENV_SPACE_ID=yyyyyy

You can get it like this in your config.yml

你可以在config.yml中得到这样的结果

space: ENV_SPACE_ID # Required
access_token: ENV_ACCESS_TOKEN # Required

#1


1  

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.

我最近不得不尝试自己这样做。事实证明,您无法将环境变量直接放入Jekyll配置文件中,但您可以编写一个rake任务,该任务将获取环境变量并将其应用于您的配置。

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

#2


0  

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.

@elryco的答案很接近但不太正确,至少在我的设置上如此。它花了一些试验和错误,但这终于奏效了。

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

Bash环境(例如〜/ .bash_profile):

export CONTENTFUL_ACCESS_TOKEN=foo
export CONTENTFUL_SPACE_ID=bar

In _config.yml, reference them as:

在_config.yml中,将它们引用为:

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.

这与Github文档中的内容相同。

#3


0  

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.

但是我编写了一个包装器脚本,它在jekyll启动之前读取环境变量并将其附加到_config.yml文件并删除变量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.

现在我可以在液体标签的任何地方使用site.secret-variable。

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

我知道这不是正确的方法,但编写自定义ruby脚本也是如此。

#4


-3  

If your bash environment variables are declared like this

如果你的bash环境变量是这样声明的

export ENV_ACCESS_TOKEN=xxxxx
export ENV_SPACE_ID=yyyyyy

You can get it like this in your config.yml

你可以在config.yml中得到这样的结果

space: ENV_SPACE_ID # Required
access_token: ENV_ACCESS_TOKEN # Required