slugbuilder 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8390ed6955989466405c4b13f75dda4c613e22ee
4
- data.tar.gz: 3ffbc59def65e419090be1033c442100597a4fbb
3
+ metadata.gz: cb87dc3ed77ea296b19898a00200f0469cb407d9
4
+ data.tar.gz: 58ccceb510e3d604513e1943dd89243313019bf1
5
5
  SHA512:
6
- metadata.gz: ca3c527f6d8dd812693a090f5475934821d31f20c88e58ca8f81df0ed81344b6361d6e0d54e33cdcf9c89092303b036cfb9475ca525195bced1dbc55303f64ca
7
- data.tar.gz: acfa79622b0d7b7fcbcd4036105fda5991baa4e4bb43c06d06e78fd9c56e96f79e3d71b468565638bba494e13e5f9566a219001f6d86b4a970bb309ed9a48916
6
+ metadata.gz: 6fe75e669008c12e2b150b3aa5ad8a2012124059acc6fc2ce39aab8c6558ad8ce0dbcee1b3d81db01925296888c9f9a1e31580c1708b0ab887f8ef2263934e60
7
+ data.tar.gz: 1d962f7f520feea5bda73cf3e10601028584d25e697cba73c8b6ef3b26a04966c9c4dce8525c0535faa5949b8fe7bfa9a940e3e50389bf86bc5e237f4d184646
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.2.0 (2017-2-1)
4
+
5
+ Fixed:
6
+
7
+ - Support multiple buildpacks such that they correctly pass environment variables to succeeding buildpacks [00c3010](../../commit/00c3010)
8
+ - Add support for buildpack compile's `ENV_DIR` argument. [06cbecd](../../commit/06cbecd)
9
+ - Handle errors on git commands [051e281](../../commit/051e281)
10
+ - Run buildpacks without context of preexisting environment [d5767e8](../../commit/d5767e8)
11
+
12
+ Added:
13
+
14
+ - Show error backtrace [21cfc90](../../commit/21cfc90)
15
+ - Set `REQUEST_ID` and `SOURCE_VERSION` environment variables [c8834c1](../../commit/c8834c1) and [8804f50](../../commit/8804f50)
16
+
3
17
  ## 1.1.0 (2017-1-27)
4
18
 
5
19
  Fixed:
data/README.md CHANGED
@@ -108,6 +108,7 @@ Alternatively, a block can be passed to the `initialize` method to the same effe
108
108
  - `repo` String: The git repo identifier
109
109
  - `git_ref` String: The git branchname or SHA
110
110
  - `git_sha` String: The git SHA (even if the git ref was a branch name)
111
+ - `request_id` String: The unique id of the build request
111
112
  - `stats` Hash:
112
113
  - setup `Float`: Amount of time spent in setup
113
114
  - build `Float`: Total amount of time spent in build (compile/build/slug)
@@ -11,6 +11,7 @@ module Slugbuilder
11
11
  @cache_dir = Shellwords.escape(Slugbuilder.config.cache_dir)
12
12
  @output_dir = Slugbuilder.config.output_dir
13
13
  @buildpacks_dir = File.join(@cache_dir, 'buildpacks')
14
+ @env_dir = File.join(@base_dir, 'environment')
14
15
  @repo = repo
15
16
  @git_ref = git_ref
16
17
  @git_dir = Shellwords.escape(File.join(@base_dir, 'git', repo))
@@ -24,6 +25,11 @@ module Slugbuilder
24
25
  end
25
26
 
26
27
  def build(clear_cache: false, env: {}, prebuild: nil, postbuild: nil, slug_name: nil, buildpacks: Slugbuilder.config.buildpacks)
28
+ @old_env = ENV.to_h
29
+ # clear environment from previous builds
30
+ FileUtils.rm_rf(@env_dir)
31
+ FileUtils.mkdir_p(@env_dir)
32
+
27
33
  @buildpacks = buildpacks
28
34
  @env = env
29
35
  @slug_file = slug_name ? "#{slug_name}.tgz" : Shellwords.escape("#{@repo.gsub('/', '.')}.#{@git_ref}.#{@git_sha}.tgz")
@@ -31,7 +37,9 @@ module Slugbuilder
31
37
 
32
38
  prebuild.call(repo: @repo, git_ref: @git_ref) if prebuild
33
39
 
34
- build_and_release
40
+ with_clean_env do
41
+ build_and_release
42
+ end
35
43
  stitle("Setup completed in #{@setup_time} seconds")
36
44
  stitle("Build completed in #{@build_time} seconds")
37
45
  stext("Application compiled in #{@compile_time} seconds")
@@ -45,22 +53,35 @@ module Slugbuilder
45
53
  output: build_output.join('')
46
54
  }
47
55
 
48
- postbuild.call(repo: @repo, git_ref: @git_ref, git_sha: @git_sha, stats: stats, slug: File.join(@output_dir, @slug_file)) if postbuild
56
+ postbuild.call(repo: @repo, git_ref: @git_ref, git_sha: @git_sha, request_id: @request_id, stats: stats, slug: File.join(@output_dir, @slug_file)) if postbuild
49
57
  if block_given?
50
- yield(repo: @repo, git_ref: @git_ref, git_sha: @git_sha, stats: stats, slug: File.join(@output_dir, @slug_file))
58
+ yield(repo: @repo, git_ref: @git_ref, git_sha: @git_sha, request_id: @request_id, stats: stats, slug: File.join(@output_dir, @slug_file))
51
59
  end
52
60
  return true
53
61
  rescue => e
54
- stitle("Failed: #{e}")
62
+ stitle("Failed: #{e}\n#{e.backtrace.join("\n")}")
55
63
  return false
64
+ ensure
65
+ restore_env
56
66
  end
57
67
 
58
68
 
59
69
  private
60
70
 
71
+ def restore_env
72
+ ENV.delete_if { true }
73
+ ENV.update(@old_env)
74
+ end
75
+
76
+ def with_clean_env
77
+ ENV.delete_if { true }
78
+ yield
79
+ restore_env
80
+ end
81
+
61
82
  def wipe_cache
62
83
  FileUtils.rm_rf(@cache_dir)
63
- FileUtils.mkdir_p(File.join(@cache_dir, 'buildpacks'))
84
+ FileUtils.mkdir_p(@buildpacks_dir)
64
85
  end
65
86
 
66
87
  def build_and_release
@@ -94,10 +115,12 @@ module Slugbuilder
94
115
  load_env_file("#{@cache_dir}/env")
95
116
  load_env_file("#{@build_dir}/.env")
96
117
  ENV['STACK'] = 'cedar-14'
118
+ @request_id = SecureRandom.urlsafe_base64(32)
119
+ ENV['REQUEST_ID'] = @request_id
120
+ ENV['SOURCE_VERSION'] = @git_sha
97
121
 
98
- @env.each do |k, v|
99
- ENV[k.to_s] = v.to_s
100
- end
122
+ # write user envs to files
123
+ write_user_envs(@env)
101
124
 
102
125
  ENV['HOME'] = @build_dir
103
126
  ENV['APP_DIR'] = @build_dir
@@ -110,7 +133,7 @@ module Slugbuilder
110
133
 
111
134
  def create_dirs
112
135
  FileUtils.mkdir_p(@base_dir)
113
- FileUtils.mkdir_p(File.join(@cache_dir, 'buildpacks'))
136
+ FileUtils.mkdir_p(@buildpacks_dir)
114
137
  FileUtils.mkdir_p(@output_dir)
115
138
  # clear old build
116
139
  FileUtils.rm_rf(@build_dir)
@@ -121,7 +144,7 @@ module Slugbuilder
121
144
  Dir.chdir(@git_dir) do
122
145
  # checkout branch or sha
123
146
  # get branch from origin so it is always the most recent
124
- rc = run("git fetch --all && (git checkout origin/#{@git_ref} || git checkout #{@git_ref}) 2>&1")
147
+ rc = run("git fetch --quiet --all && (git checkout --quiet origin/#{@git_ref} || git checkout --quiet #{@git_ref})")
125
148
  fail "Failed to fetch and checkout: #{@git_ref}" if rc != 0
126
149
  @git_sha = `git rev-parse HEAD`.strip
127
150
  end
@@ -129,7 +152,7 @@ module Slugbuilder
129
152
 
130
153
  def download_repo
131
154
  stitle("Fetching #{@repo}")
132
- rc = run("git clone git@#{Slugbuilder.config.git_service}:#{@repo}.git #{@git_dir} 2>&1")
155
+ rc = run("git clone --quiet git@#{Slugbuilder.config.git_service}:#{@repo}.git #{@git_dir}")
133
156
  fail "Failed to download repo: #{@repo}" if rc != 0
134
157
  end
135
158
 
@@ -153,13 +176,13 @@ module Slugbuilder
153
176
  if !existing_buildpacks.include?(buildpack_name)
154
177
  # download buildpack
155
178
  stitle("Fetching buildpack: #{buildpack_name}")
156
- rc = run("git clone --depth=1 #{buildpack_url} #{@buildpacks_dir}/#{buildpack_name} 2>&1")
179
+ rc = run("git clone --quiet --depth=1 #{buildpack_url} #{@buildpacks_dir}/#{buildpack_name}")
157
180
  fail "Failed to download buildpack: #{buildpack_name}" if rc != 0
158
181
  else
159
182
  # fetch latest
160
183
  stitle("Using cached buildpack. Ensuring latest version of buildpack: #{buildpack_name}")
161
184
  Dir.chdir("#{@buildpacks_dir}/#{buildpack_name}") do
162
- rc = run('git pull 2>&1')
185
+ rc = run('git pull --quiet')
163
186
  fail "Failed to update: #{buildpack_name}" if rc != 0
164
187
  end
165
188
  end
@@ -176,6 +199,10 @@ module Slugbuilder
176
199
  buildpack = File.join(@buildpacks_dir, buildpack_name)
177
200
  if run("#{buildpack}/bin/detect #{@build_dir}") == 0
178
201
  @compile_time += realtime { compile(buildpack) }
202
+
203
+ # load environment for subsequent buildpacks
204
+ load_export_env(File.join(buildpack, 'export'))
205
+
179
206
  release(buildpack)
180
207
  end
181
208
  end
@@ -183,14 +210,14 @@ module Slugbuilder
183
210
  end
184
211
 
185
212
  def compile(buildpack)
186
- rc = run_echo("#{buildpack}/bin/compile '#{@build_dir}' '#{@cache_dir}'")
213
+ rc = run_echo("#{buildpack}/bin/compile '#{@build_dir}' '#{@cache_dir}' '#{@env_dir}'")
187
214
  fail "Couldn't compile application using buildpack #{buildpack}" if rc != 0
188
215
  end
189
216
 
190
217
  def release(buildpack)
191
218
  # should create .release
192
219
  release_file = File.open("#{@build_dir}/.release", 'w')
193
- rc = run("#{buildpack}/bin/release '#{@build_dir}' '#{@cache_dir}'") do |line|
220
+ rc = run("#{buildpack}/bin/release '#{@build_dir}'") do |line|
194
221
  release_file.print(line)
195
222
  end
196
223
  release_file.close
@@ -267,6 +294,29 @@ module Slugbuilder
267
294
  end
268
295
  end
269
296
 
297
+ def load_export_env(file)
298
+ if File.exists?(file)
299
+ exports = IO.read(file).split('export')
300
+ exports.each do |line|
301
+ parts = line.split(/=/, 2)
302
+ next if parts.length != 2
303
+ name, val = parts
304
+ name.strip!
305
+ val = val.strip.split(/\n/).join.gsub('"', '')
306
+
307
+ ENV[name] = `echo "#{val}"`.strip
308
+ end
309
+ end
310
+ end
311
+
312
+ def write_user_envs(envs)
313
+ envs.each do |key, val|
314
+ File.open(File.join(@env_dir, key.to_s), 'w') do |file|
315
+ file.write(val.to_s)
316
+ end
317
+ end
318
+ end
319
+
270
320
  def load_env_file(file)
271
321
  if File.exists?(file)
272
322
  new_envs = IO.readlines(file)
@@ -277,7 +327,6 @@ module Slugbuilder
277
327
  parts = line.split(/=/, 2)
278
328
  next if parts.length != 2
279
329
 
280
- ENV[parts[0]] = parts[1]
281
330
  @env[parts[0]] = parts[1]
282
331
  end
283
332
  end
@@ -1,3 +1,3 @@
1
1
  module Slugbuilder
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slugbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Panoply Dev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-27 00:00:00.000000000 Z
11
+ date: 2017-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler