mislav (owner)

Forks

Revisions

gist: 128620 Download_button fork
public
Description:
Thor task for easier hotfixes with Capistrano & git (instructions below)
Public Clone URL: git://gist.github.com/128620.git
Embed All Files: show embed
deploy.rb #
1
2
3
4
5
6
7
8
# 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
 
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
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
 
Text only #
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
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.