saimonmoore (owner)

Fork Of

Revisions

gist: 8563 Download_button fork
public
Public Clone URL: git://gist.github.com/8563.git
Embed All Files: show embed
git.rake #
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
# by bryan helmkamp with slight modification by chris wanstrath
# and a bit of refactoring by saimon moore
# from http://www.brynary.com/2008/8/3/our-git-deployment-workflow
 
module GitCommands
  extend self
  
  def diff_staging
    execute('git fetch')
    puts `git diff origin/production origin/staging`
  end
  
  def tag_staging(branch_name)
    verify_working_directory_clean
    execute(
      'git fetch',
      'git branch -f staging origin/staging',
      'git checkout staging',
      "git reset --hard origin/#{branch_name}",
      'git push -f origin staging',
      'git checkout master',
      'git branch -D staging')
  end
  
  def tag_production
    verify_working_directory_clean
    execute(
    'git fetch',
    'git branch -f production origin/production',
    'git checkout production',
    'git reset --hard origin/staging',
    'git push -f origin production',
    'git checkout master',
    'git branch -D production')
  end
 
  def branch_production(branch_name)
    verify_working_directory_clean
    execute(
    'git fetch',
    'git branch -f production origin/production',
    'git checkout production',
    "git branch #{branch_name}",
    "git checkout #{branch_name}",
    "git push origin #{branch_name}")
  end
  
protected
 
  def verify_working_directory_clean
    return if `git status` =~ /working directory clean/
    raise "Must have clean working directory"
  end
  
  def execute(*commands)
    commands.each do |command|
      puts "Executing: '#{command}'"
      raise "Command failed!" unless system(command)
    end
  end
end
 
 
namespace :tag do
  desc <<-DESC
Update the staging branch to prepare for a staging deploy.
Defaults to master. Optionally specify a BRANCH=name
DESC
  
  task :staging, :branch do |t, args|
    branch_name = args.branch || ENV['BRANCH'] || "master"
    GitCommands.tag_staging(branch_name)
  end
 
  desc "Update the remove production branch to prepare for a release"
  task :production => ['diff:staging'] do
    GitCommands.tag_production
  end
end
 
namespace :diff do
  desc "Show the differences between the staging branch and the production branch"
  task :staging do
    GitCommands.diff_staging
  end
end
 
namespace :branch do
  desc "Branch from production for tweaks or bug fixs. Specify BRANCH=name"
  task :production, :branch do |t, args|
    branch_name = args.branch || ENV['BRANCH']
    raise "You must specify a branch name using BRANCH=name" unless branch_name
    GitCommands.branch_production(branch_name)
  end
end