Skip to content

Commit 0d931a5

Browse files
authored
Surface failures from nested rake/shell tasks (elastic#17310)
Previously when rake would shell out the output would be lost. This made debugging CI logs difficult. This commit updates the stack with improved message surfacing on error.
1 parent e748488 commit 0d931a5

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

rakelib/artifacts.rake

+25-3
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,34 @@ namespace "artifact" do
125125
result
126126
end
127127

128-
# execute Kernel#system call,checking the exist status of the executed command and eventually reporting as exception
128+
##
129+
# @override safe_system([env,] command... [,options])
130+
# execute Kernel#system call,checking the exit status of the executed command and eventually reporting as exception
129131
def safe_system(*args)
130-
if !system(*args)
131-
status = $?
132+
command = args.dup # avoid mutating input for reporting
133+
env = command.size > 1 && command.first.kind_of?(Hash) ? command.shift : {}
134+
options = command.size > 1 && command.last.kind_of?(Hash) ? command.pop : {}
135+
fail("unsupported options #{options}") unless options.empty?
136+
137+
# Normalize command to a single string from either a multi-word string
138+
# or an array of individual words
139+
command = command.size > 1 ? Shellwords.join(command.map(&:to_s)) : command.first.to_s
140+
141+
# prepend the environment
142+
env.each do |k,v|
143+
command.prepend("#{Shellwords.escape(k.to_s)}=#{Shellwords.escape(v.to_s)} ")
144+
end
145+
146+
output = `#{command} 2>&1`
147+
status = $?
148+
149+
if !status.success?
150+
puts "Command failed: #{args.inspect}"
151+
puts "Output: #{output}"
132152
raise "Got exit status #{status.exitstatus} attempting to execute #{args.inspect}!"
133153
end
154+
155+
true
134156
end
135157

136158
desc "Generate rpm, deb, tar and zip artifacts"

rubyUtils.gradle

+10-4
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,16 @@ void rake(File projectDir, File buildDir, String task, String... args) {
159159
jruby.runScriptlet("require 'rake'; require 'time'")
160160
def rakeArgs = args ? "'${args.join("','")}'" : ""
161161
jruby.runScriptlet("""
162-
rake = Rake.application
163-
rake.init
164-
rake.load_rakefile
165-
rake['${task}'].invoke(${rakeArgs})
162+
begin
163+
rake = Rake.application
164+
rake.init
165+
rake.load_rakefile
166+
rake['${task}'].invoke(${rakeArgs})
167+
rescue => e
168+
puts "Rake task error: #{e.class}: #{e.message}"
169+
puts "Backtrace: #{e.backtrace.join("\\n")}"
170+
raise e
171+
end
166172
"""
167173
)
168174
}

0 commit comments

Comments
 (0)