Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Commit

Permalink
git deployment made easy
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Sep 8, 2009
0 parents commit 6f5052a
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 0 deletions.
97 changes: 97 additions & 0 deletions README.markdown
@@ -0,0 +1,97 @@
Capistrano strategy for smart git deployment
============================================

Let's set up a straightforward, [Heroku][]-style, push-based deployment, shall we? The goal is that our deployments look like this:

$ git push origin production

Assumptions are that you are using git for your Rails app and Passenger on the server. For now, we're going to deploy on a single host.


Setup steps
-----------

1. Create a git remote for where you'll push the code on your server. The name of this remote in the examples is "origin", but it can be whatever you wish ("online", "website", or other).

$ git remote add origin user@example.com:/path/to/myapp

The "/path/to/myapp" is the directory where your code will reside. It doesn't have to exist; it will be created for you during this setup.

2. Create/overwrite the following files in your project:

**config/deploy.rb** (entire file):

# set to the name of git remote you intend to deploy to
set :remote, "origin"
# specify the deployment branch
set :branch, "master"
# sudo will only be used to create the deployment directory
set :use_sudo, true
# the remote host is read automatically from your git remote specification
server remote_host, :app, :web, :db, :primary => true

**Capfile**:

require 'git_deploy'
load 'config/deploy'

Test it by running `cap -T`. You should see several deploy tasks listed.

3. Run the setup task:

$ cap deploy:setup

This will initialize a git repository in the target directory, install the push hook and push the branch you specified to the server.

4. Login to your server to perform necessary one-time administrative operations. This might include:
* set up the Apache/nginx virtual host for this application;
* check out the branch which you will push production code into (often this is "production");
* check your config/database.yml and create or import the production database.


Deployment
----------

After you've set everything up, visiting "http://example.com" in your browser should show your app up and running. Subsequent deployments are done simply by pushing to the branch that is currently checked out on our server (see step 4.). The branch is by default "master", but it's suggested to have production code in another branch like "production" or other. This, of course, depends on your git workflow.

We've reached our goal; our deployment now looks like:

$ git push origin production

In fact, running "cap deploy" does exactly this. So what does it do?

The "deploy:setup" task installed a couple of hooks in the remote git repository: "post-receive" and "post-reset". The former is a git hook which is invoked after every push to your server, while the latter is a *custom* hook that's called asynchronously by "post-receive" when we updated the deployment branch. This is how your working copy on the server is kept up-to-date.

Thus, on first push your server automatically:

1. creates the "log" and "tmp" directories;
2. copies "config/database.example.yml" or "config/database.yml.example" to "config/database.yml".

On every subsequent deploy, the "post-reset" script analyzes changes and:

1. clears cached css and javascript assets if any versioned files under "public/stylesheets" and "public/javascripts" have changed, respectively;
2. runs "rake db:migrate" if new migrations have been added;
3. sync submodule urls if ".gitmodules" file has changed;
4. initialize and update submodules;
5. touches "tmp/restart.txt" if app restart is needed.

Finally, these are the conditions that dictate an app restart:

1. css/javascript assets have been cleared;
2. the database has migrated;
3. one or more files/submodules under "app", "config", "lib", "public", or "vendor" changed.


In the future
-------------

Next steps for this library are:

* Support for deployment on multiple hosts. This is a slightly different strategy based on git pull instead of push; something in-between regular "remote cache" strategy and the aforementioned
* Better configurability
* Steps forward to supporting more existing 3rd-party Capistrano tasks, like that of the EngineYard gem
* Support for multiple environments on the same server: production, staging, etc.
* Automatic submodule conflict resolving


[heroku]: http://heroku.com/
1 change: 1 addition & 0 deletions deps.rip
@@ -0,0 +1 @@
git://github.com/capistrano/capistrano.git 2.5.9
80 changes: 80 additions & 0 deletions lib/git_deploy.rb
@@ -0,0 +1,80 @@
require 'capistrano/recipes/deploy/scm/git'

Capistrano::Configuration.instance(true).load do
def _cset(name, *args, &block)
unless exists?(name)
set(name, *args, &block)
end
end

_cset(:application) { abort "Please specify the name of your application, set :application, 'foo'" }
_cset :remote, "origin"
_cset :branch, "master"
_cset(:revision) { branch }
_cset(:source) { Capistrano::Deploy::SCM::Git.new(self) }

_cset(:remote_url) { `#{ source.local.scm('config', "remote.#{remote}.url") }`.chomp }
_cset(:remote_host) { remote_url.split(':', 2).first }
_cset(:deploy_to) { exists?(:repository) ? "/u/apps/#{application}" : remote_url.split(':', 2).last }
_cset(:run_method) { fetch(:use_sudo, true) ? :sudo : :run }

# If :run_method is :sudo (or :use_sudo is true), this executes the given command
# via +sudo+. Otherwise is uses +run+. If :as is given as a key, it will be
# passed as the user to sudo as, if using sudo. If the :as key is not given,
# it will default to whatever the value of the :admin_runner variable is,
# which (by default) is unset.
def try_sudo(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
command = args.shift
raise ArgumentError, "too many arguments" if args.any?

as = options.fetch(:as, fetch(:admin_runner, nil))
via = fetch(:run_method, :sudo)
if command
invoke_command(command, :via => via, :as => as)
elsif via == :sudo
sudo(:as => as)
else
""
end
end

namespace :deploy do
desc "Deploys your project."
task :default do
push
end

task :push do
system source.local.scm('push', remote, "#{revision}:#{branch}")
end

desc "Prepares servers for deployment."
task :setup do
command = ["#{try_sudo} mkdir -p #{deploy_to}"]
command << "#{try_sudo} chown $USER #{deploy_to}" if fetch(:run_method, :sudo) == :sudo
command << "cd #{deploy_to}"
command << "chmod g+w ."
command << "git init"
command << "git config receive.denyCurrentBranch ignore"
run command.join(' && ')

install_hooks
push
end

task :install_hooks do
dir = File.dirname(__FILE__) + '/hooks'
remote_dir = "#{deploy_to}/.git/hooks"

top.upload "#{dir}/post-receive.rb", "#{remote_dir}/post-receive"
top.upload "#{dir}/post-reset.rb", "#{remote_dir}/post-reset"
run "chmod +x #{remote_dir}/*"
end

desc "Restarts your Passenger application."
task :restart, :roles => :app do
run "touch #{deploy_to}/tmp/restart.txt"
end
end
end
48 changes: 48 additions & 0 deletions lib/hooks/post-receive.rb
@@ -0,0 +1,48 @@
#!/usr/bin/ruby
if ENV['GIT_DIR'] == '.'
# this means the script has been called as a hook, not manually.
# get the proper GIT_DIR so we can descend into the working copy dir;
# if we don't then `git reset --hard` doesn't affect the working tree.
Dir.chdir('..')
ENV['GIT_DIR'] = '.git'
end

# find out the current branch
head = File.read('.git/HEAD').chomp
# abort if we're on a detached head
exit unless head.sub!('ref: ', '')

oldrev = newrev = nil
null_ref = '0' * 40

# read the STDIN to detect if this push changed the current branch
while newrev.nil? and gets
# each line of input is in form of "<oldrev> <newrev> <refname>"
revs = $_.split
oldrev, newrev = revs if head == revs.pop
end

# abort if there's no update, or in case the branch is deleted
exit if newrev.nil? or newrev == null_ref

# update the working copy
`git reset --hard`

if oldrev == null_ref
# this is the first push; this branch was just created
require 'fileutils'
FileUtils.mkdir_p %w(log tmp)
config = 'config/database.yml'

unless File.exists?(config)
# install the database config from the example file
example = ['config/database.example.yml', config + '.example'].find { |f| File.exists? f }
FileUtils.cp example, config if example
end
else
# run the post-reset hook
log = ">>log/deploy.log"
command = [%(echo "==== $(date) ====" #{log})]
command << %(nohup .git/hooks/post-reset #{oldrev} #{newrev} 2>&1 #{log} &)
system command.join(' && ')
end
66 changes: 66 additions & 0 deletions lib/hooks/post-reset.rb
@@ -0,0 +1,66 @@
#!/usr/bin/ruby
RAILS_ENV = 'production'
oldrev, newrev = ARGV

# get a list of files that changed
changes = `git diff #{oldrev} #{newrev} --diff-filter=ACDMR --name-status`.split("\n")

# make a hash of files that changed and how they changed
changes_hash = changes.inject(Hash.new { |h, k| h[k] = [] }) do |hash, line|
modifier, filename = line.split(/\d+/, 2)
hash[modifier] << filename
hash
end

# create an array of files added, copied, modified or renamed
modified_files = %w(A C M R).inject([]) { |files, bit| files.concat changes_hash(bit) }
added_files = changes_hash['A'] # added
deleted_files = changes_hash['D'] # deleted
changed_files = modified_files + deleted_files # all

class Array
# scans the list of files to see if any of them are under the given path
def any_in_dir?(dir)
if Array === dir
exp = %r{^(?:#{dir.join('|')})/}
any? { |file| file =~ exp }
else
dir += '/'
any? { |file| file.index(dir) == 0 }
end
end
end

cached_assets_cleared = false

# detect modified asset dirs
asset_dirs = %w(public/stylesheets public/javascripts).select do |dir|
# did any on the assets under this dir change?
changed_files.any_in_dir?(dir)
end

unless asset_dirs.empty?
# clear cached assets (unversioned/ignored files)
deleted_assets = `git ls-files -z --other -- #{asset_dirs.join(' ')} | xargs -0 rm -v`.split("\n")
unless deleted_assets.empty?
puts "Cleared: #{deleted_assets.join(', ')}"
cached_assets_cleared = true
end
end

# run migrations when new ones added
if new_migrations = added_files.any_in_dir?('db/migrate')
system %(rake db:migrate RAILS_ENV=#{RAILS_ENV})
end

# sync submodule remote urls in case of changes
system %(git submodule sync) if modified_files.include?('.gitmodules')
# initialize new & update existing submodules
system %(git submodule update --init)

# determine if app restart is needed
if cached_assets_cleared or new_migrations or changed_files.any_in_dir?(%w(app config lib public vendor))
require 'fileutils'
# tell Passenger to restart this app
FileUtils.touch 'tmp/restart.txt', :verbose => true
end

0 comments on commit 6f5052a

Please sign in to comment.