Skip to content

Instantly share code, notes, and snippets.

@mattgibson
Forked from wkrsz/gist:2761871
Created November 15, 2012 12:11
Show Gist options
  • Save mattgibson/4078354 to your computer and use it in GitHub Desktop.
Save mattgibson/4078354 to your computer and use it in GitHub Desktop.
Tddium post-build-hook to deploy to EngineYard
# Setup:
# tddium config:add suite EY_API_TOKEN xxxxxxxx
# (take EY_API_TOKEN value from ~/.eyrc)
# tddium config:add suite EY_DEPLOY_KEY yyyyyyy
# (EY_DEPLOY_KEY value is Base64-encoded content of your ~/.ssh/id_rsa OR any other ssh key authorised for a given EY env)
# To get a new key: $ ssh-keygen -t rsa
# To base 64 encode it without line breaks and copy to the clipboard: $ openssl base64 < ey-key | tr -d '\n' | pbcopy
# Now add the public key to Engine Yard via the GUI, * AND APPLY CHANGES TO THE ENVIRONMENT *
#
# Be aware that the command line tddium thing will connect to the remote suite based on the name of the local
# folder. Capitalisation matters as the variables will be added to the wrong suite otherwise.
#
# Also make sure that the engineyard gem is in the gem file for the test environment so that the
# command line deploy works.
#
# There is a bug at the moment which causes the gem to fail to install the termios dependency. Fix is this:
# gem 'termios', git: 'git://github.com/edison/ruby-termios.git'
# There is also a bug in Thor, which causes this error: "ey deploy requires at least 0 argument" Fix is this:
# gem 'thor', '~> 0.14.6'
# Bug is here: https://github.com/mislav/git-deploy/issues/31
require 'timeout'
require 'etc'
require 'base64'
def current_branch
`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3-`.strip
end
def tddium?
ENV['TDDIUM'].present?
end
def ey_environment
'narrative_production'
end
namespace :tddium do
desc "Run post-build script"
task :post_build_hook do
print_status_info
if deploy?
puts "About to run Rake task..."
Rake::Task["deploy:prod"].execute
end
end
private
def print_status_info
puts "tddium? " + tddium?.inspect
puts "passed? " + passed?.inspect
puts "on_tracked_branch? " + on_tracked_branch?.inspect
puts "deploy? " + deploy?.inspect
end
def deploy?
tddium? && on_tracked_branch? && passed?
end
def passed?
ENV['TDDIUM_BUILD_STATUS'] == 'passed'
end
def on_tracked_branch?
current_branch == tracked_branch
end
def tracked_branch
"master"
end
end
namespace :deploy do
desc "Deploy master branch to prod server"
task :prod do
puts "Beginning deployment"
if config_present?
deploy_with_timeout
end
end
private
def deploy_with_timeout
Timeout::timeout(600) do
deploy
end
end
def ey_deploy_cmd
"ey deploy -m -e #{ey_environment} -r #{current_branch}"
end
def write_deploy_key
#It works. Now someone please tell me how to do it properly.
if tddium?
puts "We're on Tddium: writing deployment key"
FileUtils.mkdir_p("#{home_dir}/.ssh")
File.open("#{home_dir}/.ssh/id_rsa", "w") do |f|
f.write(deploy_key)
f.chmod(0600)
end
end
end
def write_eyrc
puts "Writing .eyrc key"
content = "---\napi_token: #{ENV['EY_API_TOKEN']}"
File.open("#{home_dir}/.eyrc", "w") do |f|
f.write(content)
f.chmod(0600)
end
end
def home_dir
Etc.getpwuid.dir
end
def deploy_key
Base64.decode64(ENV['EY_DEPLOY_KEY'])
end
def deploy
write_deploy_key
write_eyrc
system ey_deploy_cmd
end
def config_present?
puts "No API token present" unless api_token?
puts "No deploy key present" unless deploy_key?
deploy_key? && api_token?
end
def deploy_key?
ENV['EY_DEPLOY_KEY'].present?
end
def api_token?
ENV['EY_API_TOKEN'].present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment