Skip to content

Instantly share code, notes, and snippets.

@czettnersandor
Created March 14, 2012 17:01
Show Gist options
  • Save czettnersandor/2037891 to your computer and use it in GitHub Desktop.
Save czettnersandor/2037891 to your computer and use it in GitHub Desktop.
Simple git deployment over ssh with sudo with rake
require 'net/ssh'
namespace :deploy do
@host = '111.111.111.111'
@username = 'user'
@port = 2222
@password = ''
@destination = ''
desc 'Deploy to staging'
task :staging do
@destination = '/home/project/staging'
puts "Deploying to staging..."
perform
puts "Done!"
end
task :production do
@destination = '/home/project/public_html'
puts "Deploying to production..."
perform
end
def perform
Net::SSH.start(@host, @username, :port => @port) do |ssh|
deploy_command = [
"cd #{@destination}",
"sudo -u project git pull origin master",
].join(" && ")
# open a pty channel to be able to run sudo
ssh.open_channel do |channel|
channel.request_pty do |c, success|
raise "could not request pty" unless success
channel.exec( deploy_command )
channel.on_data do |c_, data|
if data = /\[sudo\]/
# We don't need password on this server to make sudo
# channel.send_data(@password + "\n")
end
puts data
end
end
end
ssh.loop
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment