Skip to content

Instantly share code, notes, and snippets.

@wkj
Forked from wkrsz/gist:2761871
Created February 28, 2013 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkj/5058827 to your computer and use it in GitHub Desktop.
Save wkj/5058827 to your computer and use it in GitHub Desktop.
# 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)
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
namespace :tddium do
desc "Run post-build script"
task :post_build_hook do
print_status_info
if deploy?
Rake::Task["deploy:dev"].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
"develop"
end
end
namespace :deploy do
desc "Deploy master branch to dev server"
task :dev do
deploy_with_timeout
end
private
def deploy_with_timeout
Timeout::timeout(600) do
deploy
end
end
def ey_deploy_cmd
"ey deploy -m -e healthism_dev -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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment