Created
July 31, 2013 13:29
-
-
Save craineum/6121966 to your computer and use it in GitHub Desktop.
Engineyard deployment rake task for multiple SaS CI solutions. I have used this successfully for Tddium and CircleCI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup: | |
# add environment variables onto CI | |
# EY_API_TOKEN xxxxxxxx (take EY_API_TOKEN value from ~/.eyrc) | |
# CI true (checks to see if we are on the CI machine) | |
# Get the Deploy key from CI system or set on CI system | |
# Tddium run: 'tddium account' to get public key to put on engine yard | |
# CircleCI: generate a private/public key set then add via their web interface the ssh keys | |
# Tddium will automatically run rake tddium:post_build_hook | |
# Using circle.yml to call rake ci:deploy | |
require 'timeout' | |
require 'etc' | |
require 'base64' | |
ENV['CI_DEPLOYMENT_ENV'] = 'development' | |
ENV['CI_DEPLOYMENT_BRANCH'] = 'master' | |
def current_branch | |
`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3-`.strip | |
end | |
def ci? | |
tddium? || ENV['CI'].present? | |
end | |
def tddium? | |
ENV['TDDIUM'].present? | |
end | |
namespace :tddium do | |
task :post_build_hook do | |
Rake::Task["ci:deploy"].execute | |
end | |
end | |
namespace :ci do | |
desc "Run deploy script" | |
task :deploy do | |
print_status_info | |
if deploy? | |
Rake::Task["deploy:dev"].execute | |
end | |
end | |
private | |
def print_status_info | |
puts "ci? " + ci?.inspect | |
puts "passed? " + passed?.inspect | |
puts "on_tracked_branch? " + on_tracked_branch?.inspect | |
puts "deploy? " + deploy?.inspect | |
end | |
def deploy? | |
ci? && on_tracked_branch? && passed? | |
end | |
def passed? | |
ENV['TDDIUM_BUILD_STATUS'].present? ? ENV['TDDIUM_BUILD_STATUS'] == 'passed' : true | |
end | |
def on_tracked_branch? | |
current_branch == tracked_branch | |
end | |
def tracked_branch | |
ENV['CI_DEPLOYMENT_BRANCH'] | |
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 #{ENV['CI_DEPLOYMENT_ENV']} -r #{current_branch}" | |
end | |
def write_eyrc | |
puts "Writing .eyrc key" | |
content = "---\napi_token: #{ENV['EY_API_TOKEN']}" | |
File.open("#{home_dir}/.eyrc", "wb+") do |f| | |
f.write(content) | |
f.chmod(0600) | |
end | |
end | |
def home_dir | |
Etc.getpwuid.dir | |
end | |
def deploy | |
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