Skip to content

Commit d768ff9

Browse files
committed
Delegate JEKYLL_ENV into netlify CONTEXT
1 parent 2ce5fdb commit d768ff9

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

lib/jekyll/netlify/environment.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module Jekyll
2+
module Netlify
3+
# :no_doc:
4+
class Environment
5+
attr_reader :jekyll_env
6+
7+
def initialize
8+
@netlify_context = ENV['CONTEXT']
9+
if netlify?
10+
@jekyll_env = 'production'
11+
else
12+
@jekyll_env = Jekyll.env
13+
end
14+
end
15+
16+
def prefixed_env
17+
if suffix_context?
18+
@jekyll_env
19+
else
20+
[@jekyll_env, @netlify_context].join('-')
21+
end
22+
end
23+
24+
def netlify?
25+
ENV['DEPLOY_URL'] && (!ENV['JEKYLL_ENV'] || ENV['JEKYLL_ENV'].empty?)
26+
end
27+
28+
private
29+
30+
def production_context?
31+
@netlify_context == 'production'
32+
end
33+
34+
def netlify_context_blank?
35+
(!@netlify_context || @netlify_context.empty?)
36+
end
37+
38+
def suffix_context?
39+
@jekyll_env && ((@jekyll_env == @netlify_context) ||
40+
netlify_context_blank? ||
41+
production_context?)
42+
end
43+
end
44+
end
45+
end

lib/jekyll/netlify/generator.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
require_relative 'environment'
12
module Jekyll
23
module Netlify
34
# Netlify plugin generator
45
class Generator < Jekyll::Generator
56
safe true
67

78
def generate(site)
9+
env = Environment.new
810
if netlify?
11+
ENV['JEKYLL_ENV'] = env.jekyll_env
12+
site.config['environment'] = env.jekyll_env
913
site.config['netlify'] = load_netlify_env
10-
if production?
11-
ENV['JEKYLL_ENV'] = 'production'
12-
site.config['environment'] = 'production'
13-
end
14+
site.config['netlify']['environment'] = env.prefixed_env
1415
else
1516
site.config['netlify'] = false
1617
end

test/test_netlify.rb

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Jekyll::NetlifyTest < Minitest::Test
3939
end
4040
end
4141

42-
context 'netlify production deploy' do
42+
context 'netlify production context' do
4343
setup do
4444
Jekyll.instance_variable_set(
4545
:@logger, Jekyll::LogAdapter.new(Jekyll::Stevenson.new, :error)
@@ -79,6 +79,7 @@ class Jekyll::NetlifyTest < Minitest::Test
7979

8080
should 'be production' do
8181
assert_equal 'production', @site.config['environment']
82+
assert_equal 'production', @netlify['environment']
8283
end
8384

8485
should 'not be a pull request' do
@@ -92,6 +93,39 @@ class Jekyll::NetlifyTest < Minitest::Test
9293
end
9394
end
9495
end
96+
context 'netlify deploy-preview context' do
97+
setup do
98+
Jekyll.instance_variable_set(
99+
:@logger, Jekyll::LogAdapter.new(Jekyll::Stevenson.new, :error)
100+
)
101+
102+
ENV.clear
103+
ENV['CONTEXT'] = 'deploy-preview'
104+
ENV['JEKYLL_ENV'] = 'staging'
105+
106+
ENV['PULL_REQUEST'] = 'false'
107+
ENV['DEPLOY_URL'] = 'https://578ab634d5d5cf960d620--open-api.netlify.com'
108+
ENV['DEPLOY_PRIME_URL'] = 'https://beta--open-api.netlify.com'
109+
110+
config = Jekyll.configuration(
111+
source: jekyll_test_site,
112+
destination: File.join(jekyll_test_site, '_site'),
113+
)
114+
@site = Jekyll::Site.new(config)
115+
@site.read
116+
@site.generate
117+
end
118+
119+
context 'info' do
120+
setup do
121+
@netlify = @site.config['netlify']
122+
end
123+
should 'be staging-deploy-preview' do
124+
assert_equal 'staging', @site.config['environment']
125+
assert_equal 'staging-deploy-preview', @netlify['environment']
126+
end
127+
end
128+
end
95129

96130
context 'netlify unrecognised pull request' do
97131
setup do
@@ -131,8 +165,9 @@ class Jekyll::NetlifyTest < Minitest::Test
131165
assert_equal 'https://example.com', @netlify['url']
132166
end
133167

134-
should 'not be production' do
135-
assert_operator @site.config['environment'], :!=, 'production'
168+
should 'be production' do
169+
assert_equal 'production', @site.config['environment']
170+
assert_equal 'production-deploy-preview', @netlify['environment']
136171
end
137172

138173
should 'be a pull request' do

0 commit comments

Comments
 (0)