Skip to content

Commit 9afea85

Browse files
committed
Improve error handling in some rake tasks
* Ensure rake fails if `jekyll build` call fails (this is expected behavior in CI/CD). * Fix SIGINT handling in the preview task.
1 parent 8735d62 commit 9afea85

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

Rakefile

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ def jekyll(command = 'build', source = SOURCE_DIR, destination = OUTPUT_DIR, *ar
5151

5252
amended_config = "#{SOURCE_DIR}/_config_amended.yml"
5353
File.write(amended_config, YAML.dump(@config_data))
54-
system("set -x; bundle exec jekyll #{command} --source #{source} --destination #{destination} #{args.join(' ')} --config #{amended_config} #{verbose_mode}")
54+
status = system("set -x; bundle exec jekyll #{command} --source #{source} --destination #{destination} #{args.join(' ')} --config #{amended_config} #{verbose_mode}")
5555
FileUtils.rm(amended_config)
56+
57+
status
5658
end
5759

5860
desc 'Stash all directories but one in a temporary location. Run a preview server on localhost:4000.'
@@ -95,13 +97,14 @@ task :preview, :filename do |_t, args|
9597
# put our file list index in place
9698
File.write('index.markdown', preview_index)
9799

100+
# Trap SIGINT, so user can stop jekyll with Ctrl+C
101+
original_handler = trap('INT') { puts "\n(SIGINT received – waiting for Jekyll to shut down...)" }
102+
98103
# Run our preview server, watching ... watching ...
99104
jekyll('serve', SOURCE_DIR, PREVIEW_DIR)
100-
101-
# When we kill it with a ctl-c ...
102-
puts "\n\n*** Shut down the server."
103-
105+
ensure
104106
# Clean up after ourselves (in a separate task in case something goes wrong and we need to do it manually)
107+
trap('INT', original_handler) if original_handler # Restore original SIGINT handler
105108
Rake::Task['unpreview'].invoke
106109
end
107110

@@ -207,7 +210,10 @@ task :generate do
207210

208211
system("mkdir -p #{OUTPUT_DIR}")
209212
system("rm -rf #{OUTPUT_DIR}/*")
210-
jekyll
213+
214+
# Call jekyll and fail if it's failed
215+
# Shall we `ensure` the cleanup below or keep it as is for debugging?
216+
raise 'Jekyll failed' unless jekyll('build')
211217

212218
# Rake::Task['symlink_latest_versions'].invoke
213219

@@ -259,7 +265,7 @@ end
259265

260266
desc 'Serve generated output on port 9292'
261267
task :serve do
262-
system('rackup')
268+
raise 'rackup failed' unless system('rackup')
263269
end
264270

265271
desc 'Generate docs and serve locally'

0 commit comments

Comments
 (0)