Skip to content

Instantly share code, notes, and snippets.

@fny
Last active February 16, 2016 22:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fny/975caff90a945a05172b to your computer and use it in GitHub Desktop.
Save fny/975caff90a945a05172b to your computer and use it in GitHub Desktop.
Elastic Beanstalk Deploy with Environment-specific Extensions
# This file lives in `app/.elasticbeanstalk/config.yml.erb`
branch-defaults:
master:
environment: <%= eb_environment %>
# environment: contigo-worker-env # Enable for worker
# Put global configs below!
#! /usr/bin/env ruby
# This file lives in `app/bin/deploy`
#
# Put common extensions in `app/.ebextensions/whatever.config`
# Put worker extensions in `app/.ebextensions/whatever.worker-config`
#
# Note the file extension is what's important!
require 'thor'
require 'fileutils'
APP_ENV = 'change-me-to-eb-app-env'
WORKER_ENV = 'change-me-to-eb-worker-env'
class DeployCLI < Thor
include Thor::Actions
def self.source_root
File.dirname(File.expand_path('../', __FILE__))
end
attr_reader :eb_environment
desc "app EB_ENVIRONMENT",
"Deploy the web application to the given EB_ENVIRONMENT"
def app(environment = APP_ENV)
disable_worker_extensions
deploy(environment)
end
desc "worker EB_ENVIRONMENT",
"Deploy the worker to the given EB_ENVIRONMENT"
def worker(environment = WORKER_ENV)
enable_worker_extensions
deploy(environment)
disable_worker_extensions
end
desc "cleanup", "Cleans up any state from a previous deploy."
def cleanup
disable_worker_extensions
set_environment
reset_last_commit_if_deploy
end
desc "set_ebenv EB_ENVIRONMENT",
"Sets the EB environment to the given EB_ENVIRONMENT"
def set_ebenv(environment)
set_environment environment
end
private
def deploy(environment)
set_environment(environment)
run %Q[git commit -am "Deploy at #{Time.now}"]
run 'eb deploy'
reset_last_commit_if_deploy
end
def enable_worker_extensions
worker_configs.each do |file|
FileUtils.copy file[:path], ".ebextensions/#{file[:name]}.config"
end
end
def disable_worker_extensions
worker_configs.each do |file|
FileUtils.remove_file ".ebextensions/#{file[:name]}.config", true
end
end
def last_commit_deploy?
last_commit_message = run('git log -1 --pretty=%B', capture: true)
last_commit_message =~ /\ADeploy at /
end
def reset_last_commit
run 'git reset --hard HEAD~1'
end
def reset_last_commit_if_deploy
reset_last_commit if last_commit_deploy?
end
def worker_configs
file_pattern = File.expand_path('../../.ebextensions/*.worker-config', __FILE__)
Dir.glob(file_pattern).map do |file|
{
path: file,
name: File.basename(file, '.worker-config')
}
end
end
def set_environment(environment = APP_ENV)
@eb_environment = environment
template '.elasticbeanstalk/config.yml.erb', '.elasticbeanstalk/config.yml', force: true
end
end
begin
DeployCLI.start(ARGV)
rescue SignalException => e
DeployCLI.new.cleanup
rescue Exception
puts
puts "***********************************************************************"
puts "WARNING: You might be in an incomplete deploy state."
puts "Check to see if a deploy commit has been made and reset it as necessary."
puts "You can automatically clean things up with `./bin/deploy cleanup`"
puts "***********************************************************************"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment