This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

fix issues with agent using wrong env during invocation (#1101)

  • Loading branch information...
snatchev committed Jul 25, 2018
1 parent 54e278b commit 41ae8c79636e68498408b966ae90463865e38f6b
Showing with 58 additions and 28 deletions.
  1. +12 −4 agent/client.rb
  2. +24 −19 agent/invocation/recipes.rb
  3. +22 −5 spec/agent/invocation/recipes_spec.rb
View
@@ -6,8 +6,17 @@ module Agent
##
# A sample client that can be used to make a request to the service.
class Client
def initialize(host)
@stub = Proto::Agent::Stub.new("#{host}:#{PORT}", :this_channel_is_insecure)
##
# the host that the client is connecting to
attr_reader :host
# the port that the client is connecting to
attr_reader :port
def initialize(host, port = PORT)
@host = host
@port = port
@stub = Proto::Agent::Stub.new("#{@host}:#{@port}", :this_channel_is_insecure)
end
def request_spawn(bin, *params, env: {})
@@ -16,12 +25,11 @@ def request_spawn(bin, *params, env: {})
end
def request_run_fastlane(bin, *params, env: {})
command = Proto::Command.new(bin: bin, parameters: params, env: env)
command = Proto::Command.new(bin: bin, parameters: params.compact, env: env)
@stub.run_fastlane(Proto::InvocationRequest.new(command: command))
end
end
end
@file && @file.close
end
if $0 == __FILE__
@@ -15,24 +15,25 @@ def output_queue=(value)
def setup_repo(git_url, git_sha)
dir = Dir.mktmpdir("fastlane-ci")
Dir.chdir(dir)
logger.debug("Changing into working directory #{dir}.")
# TOOD: need Git Credentials for private repos.
sh("git clone --depth 1 #{git_url} repo")
Dir.chdir("repo")
# TODO: need Git Credentials for private repos.
Dir.mkdir(File.join(dir, "repo"))
Dir.chdir(File.join(dir, "repo"))
sh("git checkout #{git_sha}")
# We only want to fetch a single sha. This is how we can do that:
sh("git init .")
sh("git remote add origin #{git_url}")
sh("git fetch --depth=1 origin #{git_sha}")
sh("git checkout FETCH_HEAD")
sh("gem install bundler --no-doc")
sh("bundle install --deployment")
end
def run_fastlane(command)
command_string = "#{command.bin} #{command.parameters.join(' ')}"
logger.debug("invoking #{command_string}")
# TODO: send the env to fastlane.
sh(command_string)
sh(command_string, env: command.env.to_h)
true
end
@@ -63,19 +64,23 @@ def archive_artifacts(artifact_path)
#
# this command will either execute successfully or raise an exception.
def sh(*params, env: {})
@output_queue.push(params.join(" "))
stdin, stdouterr, thread = Open3.popen2e(*params)
stdin.close
##
# ensure our command is executed without the config of fastlane.ci
Bundler.with_clean_env do
@output_queue.push(params.join(" "))
stdin, stdouterr, thread = Open3.popen2e(env, *params)
stdin.close
# `gets` on a pipe will block until the pipe is closed, then returns nil.
while (line = stdouterr.gets)
logger.debug(line)
@output_queue.push(line)
end
# `gets` on a pipe will block until the pipe is closed, then returns nil.
while (line = stdouterr.gets)
logger.debug(line)
@output_queue.push(line)
end
exit_status = thread.value.exitstatus
if exit_status != 0
raise SystemCallError.new(line, exit_status)
exit_status = thread.value.exitstatus
if exit_status != 0
raise SystemCallError.new(line, exit_status)
end
end
end
end
@@ -6,24 +6,41 @@
class RecipesIncludingClass
include FastlaneCI::Agent::Recipes
end
subject { RecipesIncludingClass.new }
before do
@recipes_including_class = RecipesIncludingClass.new
@recipes_including_class.output_queue = queue
subject.output_queue = queue
end
it "shell commands put the command, and stdout and stderr on the output queue" do
@recipes_including_class.sh("echo foo")
subject.sh("echo foo")
expect(queue.pop).to eq("echo foo")
expect(queue.pop).to eq("foo\n")
@recipes_including_class.sh("echo error 1>&2")
subject.sh("echo error 1>&2")
expect(queue.pop).to eq("echo error 1>&2")
expect(queue.pop).to eq("error\n")
end
it "makes sure the bundler env is clean when shelling out" do
ENV["BUNDLER_GEMFILE"] = "/foo/bar/Gemfile"
subject.sh("env")
expect(queue.pop).to_not(include("BUNDLER_GEMFILE")) until queue.empty?
end
it "raises an exception if a command exits non-zero" do
expect do
@recipes_including_class.sh("false")
subject.sh("false")
end.to raise_error(SystemCallError)
end
it "runs a command with the environment" do
command = FastlaneCI::Proto::Command.new(bin: "env", env: { "FASTLANE_CI_ARTIFACTS" => "/tmp/ci/artifacts" })
subject.run_fastlane(command)
rows = []
rows << queue.pop until queue.empty?
expect(rows).to include("FASTLANE_CI_ARTIFACTS=/tmp/ci/artifacts\n")
end
end

0 comments on commit 41ae8c7

Please sign in to comment.