[英]How can we use environment variables in a Jekyll config file?
有没有办法可以在 Jekyll 的_config.yml
文件中使用我的 bash 环境变量(比如$FOO
)?
我试过使用:
foo = <%= ENV['FOO'] %>
但它不起作用,因为没有解释 Ruby 代码。
使用的版本:
如果您的目标是使用环境变量作为流动项{{ site.something }}
,您可能可以在Gemfile
获取此内容:
gem 'jekyll-environment-variables', group: :jekyll_plugins
然后你就可以使用{{ site.env.HOME }}
并期望它在输出 HTML 中被转换为/home/ubuntu
类的东西。
披露:我是宝石的所有者,很久以前我就一直在亲自使用它。
@elryco 的答案很接近,但不太正确,至少对于我的设置。 经过一些试验和错误,但这终于奏效了。 请注意,这仅适用于内容插件支持的某些环境变量。
请注意,您需要 gem jekyll jekyll-contentful-data-import
contentful jekyll-contentful-data-import
(v1.7.0 或更高版本)才能使该解决方案真正起作用。
Bash 环境(例如, ~/.bash_profile
):
export CONTENTFUL_ACCESS_TOKEN=foo
export CONTENTFUL_SPACE_ID=bar
在_config.yml
,将它们引用为:
contentful:
spaces:
- example:
space: ENV_CONTENTFUL_SPACE_ID
access_token: ENV_CONTENTFUL_ACCESS_TOKEN
这与Github 文档中所写的相同。
我最近不得不尝试自己做这件事。 事实证明,您不能将环境变量直接放入 Jekyll 配置文件中,但您可以编写一个rake
任务,该任务将获取环境变量并将它们应用到您的配置中。
下面是一个例子:
# 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
不幸的是,没有直接的方法可以在液体标签中访问它,至少不是官方的。
但是我写了一个包装脚本,它在 jekyll 启动之前读取环境变量并将其附加到_config.yml
文件并在构建后删除变量。
echo "secret-variable: $PASSWORD" >> _config.yml
bundle exec jekyll build -d target
sed '$d' _config.yml //this is to delete the last line
现在我可以在液体标签的任何地方自由使用site.secret-variable
。
我知道这不是正确的做法,但编写自定义 ruby 脚本也是如此。
我个人认为使用 ruby Jekyll 插件更合适和便携。 有一个非常简单而有效的解决方案可以在这里找到。
主要思想是 ruby 将可以访问 ENV 变量,因此您可以使用一个小的 ruby 插件将所有您想要的环境信息加载到您的site.config
液体数组中。 您也可以定义默认值。
请注意,链接中给出的示例并不是最相关的,因为 Jekyll 已经通过build 命令 options 原生提供了 prod/staging 环境。
现在可以在 Jekyll 的_config.yml
文件中使用 bash 环境变量(比如 $FOO)和GitHub 操作:
# _config.yml
title: FOO
创建一个 bash 脚本说sample.sh
以替换给定的输入字符串 FOO 并替换为另一个字符串
# 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,}
创建一个工作流文件,比如github-pages.yml
,将脚本放在 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
如果你的 bash 环境变量是这样声明的
export ENV_ACCESS_TOKEN=xxxxx
export ENV_SPACE_ID=yyyyyy
你可以在你的config.yml
得到它
space: ENV_SPACE_ID # Required
access_token: ENV_ACCESS_TOKEN # Required
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.