{"action":"created","issue":{"url":"https://api.github.com/repos/aws/aws-codedeploy-agent/issues/7","labels_url":"https://api.github.com/repos/aws/aws-codedeploy-agent/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/aws/aws-codedeploy-agent/issues/7/comments","events_url":"https://api.github.com/repos/aws/aws-codedeploy-agent/issues/7/events","html_url":"https://github.com/aws/aws-codedeploy-agent/issues/7","id":58038802,"number":7,"title":"Must explicitly specify gemfile if using bundler inside hook scripts","user":{"login":"ejhayes","id":310233,"avatar_url":"https://avatars.githubusercontent.com/u/310233?v=3","gravatar_id":"","url":"https://api.github.com/users/ejhayes","html_url":"https://github.com/ejhayes","followers_url":"https://api.github.com/users/ejhayes/followers","following_url":"https://api.github.com/users/ejhayes/following{/other_user}","gists_url":"https://api.github.com/users/ejhayes/gists{/gist_id}","starred_url":"https://api.github.com/users/ejhayes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ejhayes/subscriptions","organizations_url":"https://api.github.com/users/ejhayes/orgs","repos_url":"https://api.github.com/users/ejhayes/repos","events_url":"https://api.github.com/users/ejhayes/events{/privacy}","received_events_url":"https://api.github.com/users/ejhayes/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-18T08:15:21Z","updated_at":"2015-03-11T18:29:16Z","closed_at":null,"body":"I noticed this issue with on of my hook scripts and didn't seem to find this gotcha mentioned in the docs.  Maybe this is an obivous thing to know, but I figured I'd put a note here just incase someone else runs into this:\r\n\r\nHere's what I was attempting to run in my hook script:\r\n\r\n```\r\n...\r\nbundle --deployment\r\n...\r\n```\r\n\r\nSeemed pretty normal except I kept getting error messages like this:\r\n\r\n```\r\n...\r\n[stdout]The --deployment flag requires a Gemfile.lock. Please make sure you have checked\r\n[stdout]your Gemfile.lock into version control before deploying.\r\n...\r\n```\r\n\r\nI went to go look at the source and found that a `Gemfile` is actually getting set here:\r\nhttps://github.com/aws/aws-codedeploy-agent/blob/master/lib/instance_agent.rb#L9\r\n\r\nThis means that bundler is set to use that `Gemfile` even if we are running bundler from a different directory in our hook scripts.  This issue was discussed in this ticket:\r\n\r\nhttps://github.com/bundler/bundler/issues/1981\r\n\r\nHere's the area where the codedeploy-agent shells out to the hook scripts:\r\n\r\nhttps://github.com/aws/aws-codedeploy-agent/blob/master/lib/instance_agent/codedeploy_plugin/hook_executor.rb#L113\r\n\r\n```\r\n...\r\nOpen3.popen3(script_command, :pgroup => true) do |stdin, stdout, stderr, wait_thr|\r\n  stdin.close\r\n  stdout_thread = Thread.new{stdout.each_line { |line| log_script(\"[stdout]\" + line.to_s, script_log_file)}}\r\n  stderr_thread = Thread.new{stderr.each_line { |line| log_script(\"[stderr]\" + line.to_s, script_log_file)}}\r\n  if !wait_thr.join(script.timeout)\r\n    Process.kill('-TERM', wait_thr.pid) #kill the process group instead of pid\r\n    raise Timeout::Error\r\n  end\r\n  stdout_thread.join\r\n  stderr_thread.join\r\n  exit_status = wait_thr.value.exitstatus\r\nend\r\n...\r\n```\r\n\r\nHere's some options I found that can resolve this issue:\r\n- Specify `BUNDLE_GEMFILE` before you call bundler in hook scripts.  Example: \r\n````\r\nBUNDLE_GEMFILE=$PWD/Gemfile bundle\r\n```\r\n- Take `BUNDLE_GEMFILE` out of the environment before `Open3.popen3` gets called. That way your environment will be clean before the hook script runs. The above snippet would then look something like this:\r\n```\r\n...\r\ntmp_gemfile = ENV.delete('BUNDLER_GEMFILE')\r\nOpen3.popen3(script_command, :pgroup => true) do |stdin, stdout, stderr, wait_thr|\r\n  stdin.close\r\n  stdout_thread = Thread.new{stdout.each_line { |line| log_script(\"[stdout]\" + line.to_s, script_log_file)}}\r\n  stderr_thread = Thread.new{stderr.each_line { |line| log_script(\"[stderr]\" + line.to_s, script_log_file)}}\r\n  if !wait_thr.join(script.timeout)\r\n    Process.kill('-TERM', wait_thr.pid) #kill the process group instead of pid\r\n    raise Timeout::Error\r\n  end\r\n  stdout_thread.join\r\n  stderr_thread.join\r\n  exit_status = wait_thr.value.exitstatus\r\nend\r\nENV['BUNDLER_GEMFILE'] = tmp_gemfile unless tmp_gemfile.nil?\r\n...\r\n```"},"comment":{"url":"https://api.github.com/repos/aws/aws-codedeploy-agent/issues/comments/78340282","html_url":"https://github.com/aws/aws-codedeploy-agent/issues/7#issuecomment-78340282","issue_url":"https://api.github.com/repos/aws/aws-codedeploy-agent/issues/7","id":78340282,"user":{"login":"suryanarayanan","id":598138,"avatar_url":"https://avatars.githubusercontent.com/u/598138?v=3","gravatar_id":"","url":"https://api.github.com/users/suryanarayanan","html_url":"https://github.com/suryanarayanan","followers_url":"https://api.github.com/users/suryanarayanan/followers","following_url":"https://api.github.com/users/suryanarayanan/following{/other_user}","gists_url":"https://api.github.com/users/suryanarayanan/gists{/gist_id}","starred_url":"https://api.github.com/users/suryanarayanan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/suryanarayanan/subscriptions","organizations_url":"https://api.github.com/users/suryanarayanan/orgs","repos_url":"https://api.github.com/users/suryanarayanan/repos","events_url":"https://api.github.com/users/suryanarayanan/events{/privacy}","received_events_url":"https://api.github.com/users/suryanarayanan/received_events","type":"User","site_admin":false},"created_at":"2015-03-11T18:29:16Z","updated_at":"2015-03-11T18:29:16Z","body":"Hi,\r\n\r\nThanks for reporting this. This is a known issue on our end and we have a fix for it. It will be available in the next release of codedeploy-agent.\r\n\r\nThanks,\r\nSurya."}}