Skip to content

Instantly share code, notes, and snippets.

@sidonath
Forked from mislav/deploy.rb
Created June 12, 2009 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidonath/128760 to your computer and use it in GitHub Desktop.
Save sidonath/128760 to your computer and use it in GitHub Desktop.
# copy to your Capistrano recipe file
namespace :deploy do
task :clear_cached_assets, :roles => :web do
dir = "#{current_release}/public"
run %(rm #{dir}/javascripts/all.js #{dir}/stylesheets/master.css)
end
end
class Cap < Thor
desc "commit [HEAD]", "cap deploy:upload files from one or more commits"
method_options 'no-restart' => :boolean, 'environment' => :optional, 'working' => :optional
def commit(refspec = 'HEAD')
if options['working']
diffmode = true
refspec = String === options['working'] ? (' -- ' + options['working']) : ''
else
diffmode = refspec.index('..')
end
msg = sha = nil
log_cmd = diffmode ? 'git diff' : "git show --pretty=format:'%h %s'"
# only select added, copied, modified or renamed files
log_cmd << " --diff-filter=ACMR --name-only #{refspec}"
log = `#{log_cmd}`.split("\n")
unless diffmode
sha, msg = log.shift.split(' ', 2)
end
# ignore tests, specs and submodules
files = log.reject { |file| file =~ /^(test|spec|features)\// or File.directory?(file) }
if files.empty?
$stderr.puts "Nothing to deploy"
exit(1)
end
cmd = %(cap deploy:upload FILES='#{files.join(",")}')
if options['environment']
cmd << %( -S branch=#{options['environment']})
end
if files.any? { |file| file =~ /^public\/(stylesheets|javascripts)\// }
# uses a custom capistrano task
cmd << ' deploy:clear_cached_assets'
end
unless options['no-restart']
cmd << ' deploy:restart'
end
if msg and sha
puts %(Deploying commit [#{sha}]: #{msg})
else
puts %(Deploying #{files.size} file#{files.size == 1 ? '' : 's'})
end
system cmd
end
end
This is a Thor task that wraps the Capistrano "cap deploy:upload" command.
It uses git that provides a list of files to be uploaded.
After uploading, the web application is restarted so that changes may take effect.
## Usage
thor cap:commit
Deploys files from the last (HEAD) commit.
thor cap:commit HEAD^
Deploys files from the previous commit.
thor cap:commit A..B
Deploys files that have changed in a diff between commits A to B.
thor cap:commit origin/production..
Deploys files that have changed since the last state of "origin/production" branch.
thor cap:commit --working
Deploys files currently modified in your working copy (i.e. not yet commited).
thor cap:commit --working=app/views
Deploy only currently modified files under "app/views/" directory.
thor cap:commit --environment=production
thor cap:commit --e production
Overrides the Capistrano variable "branch" to the value of "environment" parameter.
thor cap:commit --no-restart
Don't restart the web application after uploading files.
## Notes
If any of the files under "public/stylesheets/" or "public/javascripts" are
uploaded, this task also invokes the custom "deploy:clear_cached_assets" Capistrano
task. After the application is restarted, Rails re-generates the cached assets.
Files under "spec/" and "features/" directories are not deployed since they
don't affect the production environment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment