public
Last active

Simple Deploy/rollback Rakefile

  • Download Gist
Rakefile
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.