我正在使用Jekyll构建一个网站,将其托管在GitHub Pages上。我希望能够在部署到GitHub Pages时设置JEKYLL_ENV为“production”(JEKYLL_ENV=production
),以便我可以执行以下操作:
{% if jekyll.environment == "production" %}
{% include googleAnalytics.html %}
{% endif %}
and
{% if jekyll.environment == "production" %}
<link rel="stylesheet" href="/assets/css/main.min.css">
{% else %}
<link rel="stylesheet" href="/assets/css/main.css">
{% endif %}
我读到GitHub Pages应该会自动将JEKYLL_ENV
设置为production
,但是当我在网页中插入{{ jekyll.environment }}
时,无论是在本地还是在托管的网站上,都显示development
。这可能与我在使用这个和这个部署前构建网站有关。
我的gulpfile.js
var gulp = require('gulp');
var ghPages = require('gulp-gh-pages');
var browserSync = require('browser-sync');
var sass = require('gulp-sass');
var prefix = require('gulp-autoprefixer');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var cp = require('child_process');
...
/**
* Build the Jekyll Site
*/
gulp.task('jekyll-build', function (done) {
browserSync.notify(messages.jekyllBuild);
return cp.spawn('jekyll', ['build'], {stdio: 'inherit'})
.on('close', done);
});
...
/**
* Deploy to gh-pages
*/
gulp.task('deploy', ['jekyll-build'], function() {
return gulp.src('./_site/**/*')
.pipe(ghPages());
});
基本上,当我运行
gulp deploy
时,我会取出 _site
目录并将其推送到 gh-pages
分支。
我不知道这是否导致环境设置为 development
,但我的想法是,我要做的是在 deploy
任务中运行 jekyll-build
任务,而是制作并运行一个新的 jekyll-build-prod
任务,并将 JEKYLL_ENV
设置为 production
。
这是我遇到的问题。我不确定如何使用child_process spawns
来设置环境(它已经在这个样板中编写)。我见过这个命令建议:$ JEKYLL_ENV=production jekyll build
。我似乎只需要修改我的jekyll-build
任务来协调它。
如果有更简单的方法,我很乐意听听。
感谢任何帮助。
编辑:
我尝试包括两个文件,一个_config.yml
其中我设置了environment:prod
,另一个_config_dev.yml
其中我设置了environment:dev
,并更新了我的gulp任务:/**
* Build the Jekyll Site
*/
gulp.task('jekyll-build', function (done) {
browserSync.notify(messages.jekyllBuild);
return cp.spawn('jekyll', ['build', '--config', '_config.yml,_config_dev.yml'], {stdio: 'inherit'})
.on('close', done);
});
/**
* Build the Jekyll Site for production
*/
gulp.task('jekyll-build-prod', function (done) {
browserSync.notify(messages.jekyllBuild);
return cp.spawn('jekyll', ['build', '--config', '_config.yml'], {stdio: 'inherit'})
.on('close', done);
});
然而,当我运行部署任务并检查托管站点时,它仍然显示
环境
为开发
。即使我在_config.yml
和_config_dev.yml
中都包含environment: prod
,它仍然说是development
。
_config.yml
和_config_dev.yml
并更新我的 gulp 任务(请参见我的编辑),但似乎_config.yml
没有被读取。 - user2909019index.html
文件中,我放置了{{ jekyll.environment }}
,并尝试了jekyll build
、jekyll build --config _config.yml
和jekyll serve
,但它们都会得到developement
的结果,即使我将_config.yml
和_config_dev.yml
都设置为production
。 - user2909019JEKYLL_ENV=production jekyll build
,它可以工作。但是,我不知道为什么配置文件没有起作用。 - user2909019jekyll serve
。请注意逗号:bundle exec jekyll serve --config _config.yml,_config_dev.yml
- Mike Stewart