Skip to content

Commit 299d66f

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 41e3296 commit 299d66f

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'ostruct'
2+
require 'erb'
3+
14
module InstanceAgent
25
module Plugins
36
module CodeDeployPlugin
@@ -15,7 +18,10 @@ def initialize(yaml_hash, opts = {})
1518
@permissions = parse_permissions(yaml_hash['permissions'] || [])
1619
end
1720

18-
def self.parse(app_spec_string)
21+
def self.parse(app_spec_template_string, deployment_spec)
22+
# make deployment_spec keys available to the yaml template
23+
app_spec_string = ERB.new(app_spec_template_string || '').result(OpenStruct.new(deployment_spec).instance_eval { binding })
24+
1925
new(YAML.load(app_spec_string))
2026
end
2127

lib/instance_agent/plugins/codedeploy/command_executor.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,14 @@ def last_successful_deployment_dir(deployment_group)
165165
def default_app_spec(deployment_spec)
166166
default_app_spec_location = File.join(archive_root_dir(deployment_spec), app_spec_path)
167167
log(:debug, "Checking for app spec in #{default_app_spec_location}")
168-
app_spec = ApplicationSpecification::ApplicationSpecification.parse(File.read(default_app_spec_location))
168+
app_spec = ApplicationSpecification::ApplicationSpecification.parse(File.read(default_app_spec_location), {
169+
:application_name => deployment_spec.application_name,
170+
:deployment_id => deployment_spec.deployment_id,
171+
:deployment_group_name => deployment_spec.deployment_group_name,
172+
:deployment_group_id => deployment_spec.deployment_group_id,
173+
:deployment_root_dir => deployment_root_dir(deployment_spec),
174+
:last_successful_deployment_dir => last_successful_deployment_dir(deployment_spec.deployment_group_id)
175+
})
169176
end
170177

171178
private

lib/instance_agent/plugins/codedeploy/hook_executor.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def script_absolute_path(script)
179179
def parse_app_spec
180180
app_spec_location = File.join(@deployment_archive_dir, @app_spec_path)
181181
log(:debug, "Checking for app spec in #{app_spec_location}")
182-
@app_spec = ApplicationSpecification::ApplicationSpecification.parse(File.read(app_spec_location))
182+
@app_spec = ApplicationSpecification::ApplicationSpecification.parse(File.read(app_spec_location), deployment_spec)
183183
end
184184

185185
private
@@ -204,6 +204,18 @@ def mapping_between_hooks_and_deployments
204204
"ValidateService"=>CURRENT}
205205
end
206206

207+
private
208+
def deployment_spec
209+
{
210+
:application_name => @application_name,
211+
:deployment_id => @deployment_id,
212+
:deployment_group_name => @deployment_group_name,
213+
:deployment_group_id => @deployment_group_id,
214+
:deployment_root_dir => @current_deployment_root_dir,
215+
:last_successful_deployment_dir => @last_successful_deployment_dir
216+
}
217+
end
218+
207219
private
208220
def description
209221
self.class.to_s

test/instance_agent/plugins/codedeploy/command_executor_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def generate_signed_message_for(map)
140140
stubs(:read).
141141
with("#@archive_root_dir/appspec.yml").
142142
returns("APP SPEC")
143-
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC").returns(@app_spec)
143+
ApplicationSpecification::ApplicationSpecification.stubs(:parse).with("APP SPEC", {}).returns(@app_spec)
144144
end
145145

146146
should "idempotently create the instructions directory" do

0 commit comments

Comments
 (0)