Skip to content

Commit 6aa99fd

Browse files
author
Eric Hayes
committed
Add support for using deployment spec values within appspec.yml file. These same values are also allowed to be used in the hook scripts: https://blogs.aws.amazon.com/application-management/post/Tx1PX2XMPLYPULD/Using-CodeDeploy-Environment-Variables
1 parent 81ffec2 commit 6aa99fd

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require 'instance_agent/plugins/codedeploy/application_specification/file_info'
33
require 'instance_agent/plugins/codedeploy/application_specification/linux_permission_info'
44
require 'instance_agent/plugins/codedeploy/application_specification/mode_info'
5+
require 'ostruct'
6+
require 'erb'
57

68
module InstanceAgent
79
module Plugins
@@ -23,7 +25,10 @@ def initialize(yaml_hash, opts = {})
2325
@permissions = parse_permissions(yaml_hash['permissions'] || [])
2426
end
2527

26-
def self.parse(app_spec_string)
28+
def self.parse(app_spec_template_string, deployment_spec)
29+
# make deployment_spec keys available to the yaml template
30+
app_spec_string = ERB.new(app_spec_template_string || '').result(OpenStruct.new(deployment_spec).instance_eval { binding })
31+
2732
new(YAML.load(app_spec_string))
2833
end
2934

lib/instance_agent/plugins/codedeploy/command_executor.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ def most_recent_deployment_dir(deployment_group)
199199
def default_app_spec(deployment_spec)
200200
default_app_spec_location = File.join(archive_root_dir(deployment_spec), app_spec_path)
201201
log(:debug, "Checking for app spec in #{default_app_spec_location}")
202+
app_spec = ApplicationSpecification::ApplicationSpecification.parse(File.read(default_app_spec_location), {
203+
:application_name => deployment_spec.application_name,
204+
:deployment_id => deployment_spec.deployment_id,
205+
:deployment_group_name => deployment_spec.deployment_group_name,
206+
:deployment_group_id => deployment_spec.deployment_group_id,
207+
:deployment_root_dir => deployment_root_dir(deployment_spec),
208+
:last_successful_deployment_dir => last_successful_deployment_dir(deployment_spec.deployment_group_id)
209+
})
202210
validate_app_spec_hooks(ApplicationSpecification::ApplicationSpecification.parse(File.read(default_app_spec_location)), deployment_spec.all_possible_lifecycle_events)
203211
end
204212

lib/instance_agent/plugins/codedeploy/hook_executor.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,19 @@ def parse_app_spec
227227
http://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file.html
228228
MESSAGE
229229
end
230-
@app_spec = InstanceAgent::Plugins::CodeDeployPlugin::ApplicationSpecification::ApplicationSpecification.parse(File.read(app_spec_location))
230+
@app_spec = InstanceAgent::Plugins::CodeDeployPlugin::ApplicationSpecification::ApplicationSpecification.parse(File.read(app_spec_location), deployment_spec)
231+
end
232+
233+
private
234+
def deployment_spec
235+
{
236+
:application_name => @application_name,
237+
:deployment_id => @deployment_id,
238+
:deployment_group_name => @deployment_group_name,
239+
:deployment_group_id => @deployment_group_id,
240+
:deployment_root_dir => @current_deployment_root_dir,
241+
:last_successful_deployment_dir => @last_successful_deployment_dir
242+
}
231243
end
232244

233245
private

test/instance_agent/plugins/codedeploy/command_executor_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def generate_signed_message_for(map)
152152
stubs(:read).
153153
with("#@archive_root_dir/appspec.yml").
154154
returns("APP SPEC")
155-
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC").returns(@app_spec)
155+
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC", {}).returns(@app_spec)
156156
end
157157

158158
should "create an appropriate Installer" do
@@ -201,7 +201,7 @@ def generate_signed_message_for(map)
201201
app_spec_hooks = {'UnknownHook' => nil}
202202
app_spec.expects(:hooks).returns(app_spec_hooks)
203203
File.stubs(:read).with("#@archive_root_dir/appspec.yml").returns("APP SPEC")
204-
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC").returns(app_spec)
204+
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC", {}).returns(app_spec)
205205
unknown_hooks = app_spec_hooks.merge(@test_hook_mapping)
206206
assert_raised_with_message("appspec.yml file contains unknown lifecycle events: #{unknown_hooks.keys}", ArgumentError) do
207207
@command_executor.execute_command(@command, deployment_spec)
@@ -229,7 +229,7 @@ def generate_signed_message_for(map)
229229
app_spec_hooks = InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller::DEFAULT_HOOK_MAPPING.merge({'ExampleLifecycleEvent' => nil, 'SecondLifecycleEvent' => nil})
230230
app_spec.expects(:hooks).twice.returns(app_spec_hooks)
231231
File.stubs(:read).with("#@archive_root_dir/appspec.yml").returns("APP SPEC")
232-
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC").returns(app_spec)
232+
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC", {}).returns(app_spec)
233233
assert_raised_with_message("You specified a lifecycle event which is not a default one and doesn't exist in your appspec.yml file: CustomHookNotInAppspec", ArgumentError) do
234234
@command_executor.execute_command(@command, deployment_spec)
235235
end

0 commit comments

Comments
 (0)