From d5767e80002acc667f2283799b2c7efcafde15ad Mon Sep 17 00:00:00 2001 From: Jonathan Lehman Date: Wed, 1 Feb 2017 11:50:32 -0500 Subject: [PATCH] Run buildpacks without context of environment - Prevents other ENVs (running this gem in another bundler context or within a Rails app etc) from leaking into the build --- lib/slugbuilder/builder.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/slugbuilder/builder.rb b/lib/slugbuilder/builder.rb index f36f9e9..aa66eca 100644 --- a/lib/slugbuilder/builder.rb +++ b/lib/slugbuilder/builder.rb @@ -25,6 +25,7 @@ def initialize(repo:, git_ref:, stdout: $stdout) end def build(clear_cache: false, env: {}, prebuild: nil, postbuild: nil, slug_name: nil, buildpacks: Slugbuilder.config.buildpacks) + @old_env = ENV.to_h # clear environment from previous builds FileUtils.rm_rf(@env_dir) FileUtils.mkdir_p(@env_dir) @@ -36,7 +37,9 @@ def build(clear_cache: false, env: {}, prebuild: nil, postbuild: nil, slug_name: prebuild.call(repo: @repo, git_ref: @git_ref) if prebuild - build_and_release + with_clean_env do + build_and_release + end stitle("Setup completed in #{@setup_time} seconds") stitle("Build completed in #{@build_time} seconds") stext("Application compiled in #{@compile_time} seconds") @@ -58,11 +61,24 @@ def build(clear_cache: false, env: {}, prebuild: nil, postbuild: nil, slug_name: rescue => e stitle("Failed: #{e}\n#{e.backtrace.join("\n")}") return false + ensure + restore_env end private + def restore_env + ENV.delete_if { true } + ENV.update(@old_env) + end + + def with_clean_env + ENV.delete_if { true } + yield + restore_env + end + def wipe_cache FileUtils.rm_rf(@cache_dir) FileUtils.mkdir_p(@buildpacks_dir)