Skip to content

Instantly share code, notes, and snippets.

@matthewsimo
Created March 29, 2012 19:57
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 matthewsimo/2243053 to your computer and use it in GitHub Desktop.
Save matthewsimo/2243053 to your computer and use it in GitHub Desktop.
Simple Deploy/rollback Rakefile
# rake deploy[env,branch] # Notice no spaces in arguments!
require 'net/ssh'
# Config for stage env
@stage_ssh = "dev.server.com"
@stage_path = "/path/to/staging/root/"
@stage_user = "your_username"
@stage_pass = "your_password"
@stage_port = port
# Config for prod env
@prod_ssh = "dev.server.com"
@prod_path = "/path/to/production/root/"
@prod_user = "your_username"
@prod_pass = "your_password"
@prod_port = port
# Add your config for your custom env here:
# @custom_ssh = "servername.com"
# @custom_path = "/custom/path/to/root/"
# @custom_user = "your_custom_user"
# @custom_pass = "your_custom_pass"
# @custom_port = 22 #change as necessary
def assign_target (environment)
if environment === "prod"
@target_ssh = @prod_ssh
@target_path = @prod_path
@target_user = @prod_user
@target_pass = @prod_pass
@target_port = @prod_port
elsif environment === "stage"
@target_ssh = @stage_ssh
@target_path = @stage_path
@target_user = @stage_user
@target_pass = @stage_pass
@target_port = @stage_port
else # If you need to add a custom environment, assign target variables in a new elsif here.
abort("You are trying to connect to an unknown environment, add it to the deploy task or try stage/prod.")
end
end
desc "Deploy Task, defaults to env: stage & branch: master."
task :deploy, [:env, :branch] do |cmd, args|
args.with_defaults(:env => "stage", :branch => "master")
assign_target args[:env]
puts "Deploying #{args.branch} branch to #{args.env} via git pull on #{@target_ssh}."
Net::SSH.start( @target_ssh, @target_user, {:port => @target_port, :password => @target_pass}) do |ssh|
puts "SSH Connected, fetching git head"
output = ssh.exec!("cd #{@target_path} && git pull origin #{args.branch}")
end
end
desc "Rollback Task, defaults to env: stage & branch: master."
task :rollback, [:env, :branch] do |cmd, args|
args.with_defaults(:env => "stage", :branch => "master")
assign_target args[:env]
puts "Rolling back #{args.branch} branch to #{args.env} via git pull on #{@target_ssh}."
Net::SSH.start( @target_ssh, @target_user, {:port => @target_port, :password => @target_pass}) do |ssh|
puts "SSH Connected, reverting git head"
output = ssh.exec!("cd #{@target_path} && git revert HEAD")
end
puts "Now go sort it out on the server."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment