How to setup a remote git server and deploy from it with capistrano

1. Install Git on ALL computers (local, remote git, and remote web server)

A. For installation on a mac or windows computer, google the web.

B. For remote git server and remote web server (assumes you’re on ubuntu hardy or something similar)

mkdir ~/sources
cd ~/sources
wget http://kernel.org/pub/software/scm/git/git-1.6.0.tar.bz2
sudo apt-get build-dep git-core
tar xjf git-1.6.0.tar.bz2
cd git-1.6.0/
./configure
make
sudo make install

2. SSH into your git server and setup the remote repository

mkdir ~/git
cd ~/git
mkdir example.git
cd exmplae.git
git init

3. On your local computer create your local repository for your app

mkdir example
cd example
git init
touch README
git add README
git commit -m 'Initial import'
git remote add origin git@REMOTE_SERVER:git/example.git
git push origin master

4. Now you can deploy app with capistrano from your local machine

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#example deploy.rb
 
set :runner, "username"
set :use_sudo, false
 
# =============================================================================
# CUSTOM OPTIONS
# =============================================================================
set :user, "username"
set :application, "example"
set :domain, "domain.com"
 
role :app, domain, :cron => true
role :web, domain
role :db,  domain, :primary => true
 
# =============================================================================
# DATABASE OPTIONS
# =============================================================================
set :rails_env,       "production"
 
# =============================================================================
# DEPLOY TO
# =============================================================================
 
set :deploy_to, "/home/#{user}/apps/#{application}"
 
# =============================================================================
# REPOSITORY
# =============================================================================
set :repository,  "ssh://username@REMOTE_SERVER:3444/home/username/git/#{application}.git" # If using a NON-STANDARD PORT instead of default 22. For git server.
set :scm, "git"
set :branch, "master"
 
# =============================================================================
# SSH OPTIONS
# =============================================================================
default_run_options[:pty] = true
ssh_options[:paranoid] = false
ssh_options[:keys] = %w(/Users/username/.ssh/id_rsa)
ssh_options[:port] = 3333 # If using a NON-STANDARD PORT instead of default 22. For deployment server.
 
 
# =============================================================================
# RAKE TASKS & OTHER SERVER TASKS
# =============================================================================
 
namespace :cron do
  task :start, :roles => :app, :only => {:cron => true} do
    cron_tab = "#{shared_path}/cron.tab"
    run "mkdir -p #{shared_path}/log/cron"
 
    require 'erb'
    template = File.read("config/cron.erb")
    file = ERB.new(template).result(binding)
    put file, cron_tab, :mode => 0644
 
    # merge with the current crontab
    # fails with an empty crontab, which is acceptable
    run "crontab -l >> #{cron_tab}" rescue nil
 
    # install the new crontab
    run "crontab #{cron_tab}"
  end
end
 
namespace :cron do
  task :stop, :roles => :app, :only => {:cron => true} do
    cron_tmp = "#{shared_path}/cron.old"
    cron_tab = "#{shared_path}/cron.tab"
 
    begin
      # dump the current cron entries
      run "crontab -l > #{cron_tmp}"
 
      # remove any lines that contain the application name
      run "awk '{if ($0 !~ /#{application}/) print $0}' " +
        "#{cron_tmp} > #{cron_tab}"
 
      # replace the cron entries
      run "crontab #{cron_tab}"
    rescue
      # fails with an empty crontab, which is acceptable
    end
 
    # clean up
    run "rm -rf #{cron_tmp}"
  end
end
 
desc 'Fix images from disappearing'
task :fix_images, :roles => :app do
  %w{images}.each do |share|
    run "rm -rf #{release_path}/public/#{share}"
    run "mkdir -p #{shared_path}/system/#{share}"
    run "ln -nfs #{shared_path}/system/#{share} #{release_path}/public/#{share}"
  end
end
 
desc "Create symlink to public_html/#{domain}/public"
task :symlinkify do
  run "rm -rf /home/#{user}/public_html/#{domain}/public; ln -s #{current_path}/public /home/#{user}/public_html/#{domain}"
end
 
desc "Reload Apache"
task :reload_apache do
  sudo "/etc/init.d/apache2 reload"
end
 
# Edit this accordingly.
after "deploy", "deploy:migrations"
after "deploy:migrations", "deploy:cleanup"
after "deploy:cleanup", "fix_images"
after "fix_images", "symlinkify"
after "symlinkify", "reload_apache"

Resources:

Comments