Skip to content

Commit

Permalink
Expose the commit hash as an environment variable to hook scripts
Browse files Browse the repository at this point in the history
This patch exposes the commit hash as `BUNDLE_COMMIT`
when we are deploying from Github.

GHI #36
  • Loading branch information
jcbhl authored and mwjones-aws committed Sep 15, 2022
1 parent 6a3e34a commit af5b989
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
12 changes: 11 additions & 1 deletion lib/instance_agent/plugins/codedeploy/command_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,23 @@ def get_revision_envs(deployment_spec)
case deployment_spec.revision_source
when 'S3'
return get_s3_envs(deployment_spec)
when 'GitHub', 'Local File', 'Local Directory'
when 'GitHub'
return get_github_envs(deployment_spec)
when 'Local File', 'Local Directory'
return {}
else
raise "Unknown revision type '#{deployment_spec.revision_source}'"
end
end

private
def get_github_envs(deployment_spec)
# TODO(CDAGENT-387): expose the repository name and account, but we'll likely need to go through AppSec before doing so.
return {
"BUNDLE_COMMIT" => deployment_spec.commit_id
}
end

private
def get_s3_envs(deployment_spec)
return {
Expand Down
63 changes: 58 additions & 5 deletions test/instance_agent/plugins/codedeploy/command_executor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def s3_env_vars()
}
end

def github_env_vars()
return {
"BUNDLE_COMMIT" => @githubRevision["CommitId"]
}
end

context 'The CodeDeploy Plugin Command Executor' do
setup do
@test_hook_mapping = { "BeforeBlockTraffic"=>["BeforeBlockTraffic"],
Expand Down Expand Up @@ -69,6 +75,11 @@ def s3_env_vars()
"Key" => "mykey",
"BundleType" => "tar"
}
@githubRevision = {
'Account' => 'account',
'Repository' => 'repository',
'CommitId' => 'commitid',
}
@file_exists_behavior = "RETAIN"
@agent_actions_overrides_map = {"FileExistsBehavior" => @file_exists_behavior}
@agent_actions_overrides = {"AgentOverrides" => @agent_actions_overrides_map}
Expand Down Expand Up @@ -126,10 +137,15 @@ def s3_env_vars()
end
end

context "when executing a valid command" do
context "when executing a valid non-hardcoded command" do
setup do
@command.command_name = "Install"
@command_executor.stubs(:install)
@command.command_name = "ValidateService"
@command_executor.stubs(:validate_service)

@app_spec = mock("parsed application specification")
File.stubs(:exist?).with("#@archive_root_dir/appspec.yml").returns(true)
File.stubs(:read).with("#@archive_root_dir/appspec.yml").returns("APP SPEC")
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC").returns(@app_spec)
end

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

should "not be a noop command" do
assert_false @command_executor.is_command_noop?(@command.command_name, @deployment_spec)
context "when the bundle is from github" do
setup do
@deployment_spec = generate_signed_message_for({
"DeploymentId" => @deployment_id.to_s,
"DeploymentGroupId" => @deployment_group_id.to_s,
"ApplicationName" => @application_name,
"DeploymentCreator" => @deployment_creator,
"DeploymentGroupName" => @deployment_group_name,
"Revision" => {
"RevisionType" => "GitHub",
"GitHubRevision" => @githubRevision
}
})

@hook_executor_constructor_hash = {
:lifecycle_event => @command.command_name,
:application_name => @application_name,
:deployment_id => @deployment_id,
:deployment_group_name => @deployment_group_name,
:deployment_group_id => @deployment_group_id,
:deployment_creator => @deployment_creator,
:deployment_type => @deployment_type,
:deployment_root_dir => @deployment_root_dir,
:last_successful_deployment_dir => nil,
:most_recent_deployment_dir => nil,
:app_spec_path => 'appspec.yml',
:revision_envs => github_env_vars()}
@mock_hook_executor = mock
@command_executor.unstub(:validate_service)
@command_executor.stubs(:last_successful_deployment_dir).returns(nil)
@command_executor.stubs(:most_recent_deployment_dir).returns(nil)
end

should "create a hook executor with the commit hash as an environment variable" do
HookExecutor.expects(:new).with(@hook_executor_constructor_hash).returns(@mock_hook_executor)
@mock_hook_executor.expects(:execute)

@command_executor.execute_command(@command, @deployment_spec)
end
end

context "when failed to create root directory" do
Expand Down
8 changes: 4 additions & 4 deletions test/instance_agent/plugins/codedeploy/hook_executor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class HookExecutorTest < InstanceAgentTestCase

include InstanceAgent::Plugins::CodeDeployPlugin

def create_hook_executor(map = {})
def create_hook_executor(revision_envs = nil)
HookExecutor.new ({:lifecycle_event => @lifecycle_event,
:application_name => @application_name,
:deployment_id => @deployment_id,
Expand All @@ -17,8 +17,8 @@ def create_hook_executor(map = {})
:deployment_root_dir => @deployment_root_dir,
:last_successful_deployment_dir => @last_successful_deployment_dir,
:most_recent_deployment_dir => @most_recent_deployment_dir,
:app_spec_path => @app_spec_path}
.merge(map))
:app_spec_path => @app_spec_path,
:revision_envs => revision_envs})
end

context "testing hook executor" do
Expand Down Expand Up @@ -246,7 +246,7 @@ def create_hook_executor(map = {})
setup do
revision_envs = {"TEST_ENVIRONMENT_VARIABLE" => "ONE", "ANOTHER_ENV_VARIABLE" => "TWO"}
@child_env.merge!(revision_envs)
@hook_executor = create_hook_executor(:revision_envs => revision_envs)
@hook_executor = create_hook_executor(revision_envs)
end

should "call popen with the environment variables" do
Expand Down

0 comments on commit af5b989

Please sign in to comment.