Skip to content

Commit af5b989

Browse files
jcbhlmwjones-aws
authored andcommitted
Expose the commit hash as an environment variable to hook scripts
This patch exposes the commit hash as `BUNDLE_COMMIT` when we are deploying from Github. GHI #36
1 parent 6a3e34a commit af5b989

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

lib/instance_agent/plugins/codedeploy/command_executor.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,23 @@ def get_revision_envs(deployment_spec)
231231
case deployment_spec.revision_source
232232
when 'S3'
233233
return get_s3_envs(deployment_spec)
234-
when 'GitHub', 'Local File', 'Local Directory'
234+
when 'GitHub'
235+
return get_github_envs(deployment_spec)
236+
when 'Local File', 'Local Directory'
235237
return {}
236238
else
237239
raise "Unknown revision type '#{deployment_spec.revision_source}'"
238240
end
239241
end
240242

243+
private
244+
def get_github_envs(deployment_spec)
245+
# TODO(CDAGENT-387): expose the repository name and account, but we'll likely need to go through AppSec before doing so.
246+
return {
247+
"BUNDLE_COMMIT" => deployment_spec.commit_id
248+
}
249+
end
250+
241251
private
242252
def get_s3_envs(deployment_spec)
243253
return {

test/instance_agent/plugins/codedeploy/command_executor_test.rb

+58-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ def s3_env_vars()
2525
}
2626
end
2727

28+
def github_env_vars()
29+
return {
30+
"BUNDLE_COMMIT" => @githubRevision["CommitId"]
31+
}
32+
end
33+
2834
context 'The CodeDeploy Plugin Command Executor' do
2935
setup do
3036
@test_hook_mapping = { "BeforeBlockTraffic"=>["BeforeBlockTraffic"],
@@ -69,6 +75,11 @@ def s3_env_vars()
6975
"Key" => "mykey",
7076
"BundleType" => "tar"
7177
}
78+
@githubRevision = {
79+
'Account' => 'account',
80+
'Repository' => 'repository',
81+
'CommitId' => 'commitid',
82+
}
7283
@file_exists_behavior = "RETAIN"
7384
@agent_actions_overrides_map = {"FileExistsBehavior" => @file_exists_behavior}
7485
@agent_actions_overrides = {"AgentOverrides" => @agent_actions_overrides_map}
@@ -126,10 +137,15 @@ def s3_env_vars()
126137
end
127138
end
128139

129-
context "when executing a valid command" do
140+
context "when executing a valid non-hardcoded command" do
130141
setup do
131-
@command.command_name = "Install"
132-
@command_executor.stubs(:install)
142+
@command.command_name = "ValidateService"
143+
@command_executor.stubs(:validate_service)
144+
145+
@app_spec = mock("parsed application specification")
146+
File.stubs(:exist?).with("#@archive_root_dir/appspec.yml").returns(true)
147+
File.stubs(:read).with("#@archive_root_dir/appspec.yml").returns("APP SPEC")
148+
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC").returns(@app_spec)
133149
end
134150

135151
should "create the deployment root directory" do
@@ -138,8 +154,45 @@ def s3_env_vars()
138154
@command_executor.execute_command(@command, @deployment_spec)
139155
end
140156

141-
should "not be a noop command" do
142-
assert_false @command_executor.is_command_noop?(@command.command_name, @deployment_spec)
157+
context "when the bundle is from github" do
158+
setup do
159+
@deployment_spec = generate_signed_message_for({
160+
"DeploymentId" => @deployment_id.to_s,
161+
"DeploymentGroupId" => @deployment_group_id.to_s,
162+
"ApplicationName" => @application_name,
163+
"DeploymentCreator" => @deployment_creator,
164+
"DeploymentGroupName" => @deployment_group_name,
165+
"Revision" => {
166+
"RevisionType" => "GitHub",
167+
"GitHubRevision" => @githubRevision
168+
}
169+
})
170+
171+
@hook_executor_constructor_hash = {
172+
:lifecycle_event => @command.command_name,
173+
:application_name => @application_name,
174+
:deployment_id => @deployment_id,
175+
:deployment_group_name => @deployment_group_name,
176+
:deployment_group_id => @deployment_group_id,
177+
:deployment_creator => @deployment_creator,
178+
:deployment_type => @deployment_type,
179+
:deployment_root_dir => @deployment_root_dir,
180+
:last_successful_deployment_dir => nil,
181+
:most_recent_deployment_dir => nil,
182+
:app_spec_path => 'appspec.yml',
183+
:revision_envs => github_env_vars()}
184+
@mock_hook_executor = mock
185+
@command_executor.unstub(:validate_service)
186+
@command_executor.stubs(:last_successful_deployment_dir).returns(nil)
187+
@command_executor.stubs(:most_recent_deployment_dir).returns(nil)
188+
end
189+
190+
should "create a hook executor with the commit hash as an environment variable" do
191+
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
192+
@mock_hook_executor.expects(:execute)
193+
194+
@command_executor.execute_command(@command, @deployment_spec)
195+
end
143196
end
144197

145198
context "when failed to create root directory" do

test/instance_agent/plugins/codedeploy/hook_executor_test.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class HookExecutorTest < InstanceAgentTestCase
66

77
include InstanceAgent::Plugins::CodeDeployPlugin
88

9-
def create_hook_executor(map = {})
9+
def create_hook_executor(revision_envs = nil)
1010
HookExecutor.new ({:lifecycle_event => @lifecycle_event,
1111
:application_name => @application_name,
1212
:deployment_id => @deployment_id,
@@ -17,8 +17,8 @@ def create_hook_executor(map = {})
1717
:deployment_root_dir => @deployment_root_dir,
1818
:last_successful_deployment_dir => @last_successful_deployment_dir,
1919
:most_recent_deployment_dir => @most_recent_deployment_dir,
20-
:app_spec_path => @app_spec_path}
21-
.merge(map))
20+
:app_spec_path => @app_spec_path,
21+
:revision_envs => revision_envs})
2222
end
2323

2424
context "testing hook executor" do
@@ -246,7 +246,7 @@ def create_hook_executor(map = {})
246246
setup do
247247
revision_envs = {"TEST_ENVIRONMENT_VARIABLE" => "ONE", "ANOTHER_ENV_VARIABLE" => "TWO"}
248248
@child_env.merge!(revision_envs)
249-
@hook_executor = create_hook_executor(:revision_envs => revision_envs)
249+
@hook_executor = create_hook_executor(revision_envs)
250250
end
251251

252252
should "call popen with the environment variables" do

0 commit comments

Comments
 (0)