Skip to content

Instantly share code, notes, and snippets.

@jamiew
Forked from ndbroadbent/deploy.rake
Created January 1, 2013 19:42

Revisions

  1. @ndbroadbent ndbroadbent revised this gist Oct 2, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions deploy.rake
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,6 @@ task :deploy do
    raise "You have unstaged or uncommitted changes! Please only deploy from a clean working directory!"
    end

    current_revision = `git rev-parse master`.strip

    unless File.exists? deploy_repo_dir
    # Copy repo to tmp dir so we can continue working while it deploys
    @new_repo = true
  2. @ndbroadbent ndbroadbent revised this gist Oct 2, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion deploy.rake
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ task :deploy do

    begin
    # Check branch and working directory. Fail if not on clean master branch.
    branch = `branch_name=$(git symbolic-ref HEAD 2>/dev/null); branch_name=${branch_name##refs/heads/}; printf ${branch_name:-HEAD}`.strip
    branch = `branch_name=$(git symbolic-ref HEAD 2>/dev/null); branch_name=${branch_name##refs/heads/}; echo ${branch_name:-HEAD}`.strip
    if branch != 'master'
    raise "You have checked out the #{branch} branch, please only deploy from the master branch!"
    end
  3. @ndbroadbent ndbroadbent revised this gist Oct 2, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions deploy.rake
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ task :deploy do

    begin
    # Check branch and working directory. Fail if not on clean master branch.
    branch = `branch_name=$(git symbolic-ref HEAD 2>/dev/null); branch_name=${branch_name##refs/heads/}; echo -n ${branch_name:-HEAD}`
    branch = `branch_name=$(git symbolic-ref HEAD 2>/dev/null); branch_name=${branch_name##refs/heads/}; printf ${branch_name:-HEAD}`.strip
    if branch != 'master'
    raise "You have checked out the #{branch} branch, please only deploy from the master branch!"
    end
    @@ -41,7 +41,7 @@ task :deploy do

    # Allow git to see public/assets
    puts "Removing public/assets from .gitignore..."
    system 'sed -i "/^public\/assets/d" .gitignore'
    system 'sed -i '' "/^[\/?]public\/assets/d" .gitignore'
    system 'git add .gitignore; git commit -m "Allow git to commit public/assets"'
    else
    # Otherwise, we're already on the deploy branch, so fetch any new changes from original repo
  4. @ndbroadbent ndbroadbent revised this gist Sep 28, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion deploy.rake
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ task :deploy do
    system "git remote set-url origin ../.."
    system "git checkout -b #{deploy_branch}"

    # Allow git to see public/assets and public/*.html(.gz)
    # Allow git to see public/assets
    puts "Removing public/assets from .gitignore..."
    system 'sed -i "/^public\/assets/d" .gitignore'
    system 'git add .gitignore; git commit -m "Allow git to commit public/assets"'
  5. @ndbroadbent ndbroadbent revised this gist Sep 28, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions deploy.rake
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,7 @@ task :deploy do
    FileUtils.cp_r Rails.root.to_s, "/tmp/heroku_deploy"
    FileUtils.mv "/tmp/heroku_deploy", Rails.root.join('tmp')
    end

    # Change working directory to copied repo
    Dir.chdir(deploy_repo_dir)

  6. @ndbroadbent ndbroadbent revised this gist Sep 28, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion deploy.rake
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ task :deploy do
    # Copy repo to tmp dir so we can continue working while it deploys
    @new_repo = true
    puts "Copying repo to #{deploy_repo_dir}..."
    # (can't copy into self tmp dir first)
    # Can't copy into self, so copy to /tmp first
    FileUtils.cp_r Rails.root.to_s, "/tmp/heroku_deploy"
    FileUtils.mv "/tmp/heroku_deploy", Rails.root.join('tmp')
    end
  7. @ndbroadbent ndbroadbent revised this gist Sep 28, 2012. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions deploy.rake
    Original file line number Diff line number Diff line change
    @@ -21,9 +21,8 @@ task :deploy do

    current_revision = `git rev-parse master`.strip

    # First, copy repo to tmp dir so we can continue working while it deploys
    # (only copy if not already there)
    unless File.exists? deploy_repo_dir
    # Copy repo to tmp dir so we can continue working while it deploys
    @new_repo = true
    puts "Copying repo to #{deploy_repo_dir}..."
    # (can't copy into self tmp dir first)
  8. @ndbroadbent ndbroadbent revised this gist Sep 28, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion deploy.rake
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    require 'fileutils'

    # Warning: The following deploy task will completely overwrite whatever is deployed on Heroku.
    # Warning: The following deploy task will completely overwrite whatever is currently deployed to Heroku.
    # The deploy branch is rebased onto master, so the push needs to be forced.

    desc "Deploy app to Heroku after precompiling assets"
  9. @ndbroadbent ndbroadbent created this gist Sep 28, 2012.
    87 changes: 87 additions & 0 deletions deploy.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    require 'fileutils'

    # Warning: The following deploy task will completely overwrite whatever is deployed on Heroku.
    # The deploy branch is rebased onto master, so the push needs to be forced.

    desc "Deploy app to Heroku after precompiling assets"
    task :deploy do
    deploy_branch = 'heroku'
    remote = 'heroku'
    deploy_repo_dir = "tmp/heroku_deploy"

    begin
    # Check branch and working directory. Fail if not on clean master branch.
    branch = `branch_name=$(git symbolic-ref HEAD 2>/dev/null); branch_name=${branch_name##refs/heads/}; echo -n ${branch_name:-HEAD}`
    if branch != 'master'
    raise "You have checked out the #{branch} branch, please only deploy from the master branch!"
    end
    if `git status --porcelain`.present?
    raise "You have unstaged or uncommitted changes! Please only deploy from a clean working directory!"
    end

    current_revision = `git rev-parse master`.strip

    # First, copy repo to tmp dir so we can continue working while it deploys
    # (only copy if not already there)
    unless File.exists? deploy_repo_dir
    @new_repo = true
    puts "Copying repo to #{deploy_repo_dir}..."
    # (can't copy into self tmp dir first)
    FileUtils.cp_r Rails.root.to_s, "/tmp/heroku_deploy"
    FileUtils.mv "/tmp/heroku_deploy", Rails.root.join('tmp')
    end
    # Change working directory to copied repo
    Dir.chdir(deploy_repo_dir)

    # Create new deploy branch if starting with a fresh repo
    if @new_repo
    # Set remote to parent git dir, so we can fetch updates on future deploys
    system "git remote set-url origin ../.."
    system "git checkout -b #{deploy_branch}"

    # Allow git to see public/assets and public/*.html(.gz)
    puts "Removing public/assets from .gitignore..."
    system 'sed -i "/^public\/assets/d" .gitignore'
    system 'git add .gitignore; git commit -m "Allow git to commit public/assets"'
    else
    # Otherwise, we're already on the deploy branch, so fetch any new changes from original repo
    system "git fetch origin"

    # Rebase onto origin/master.
    # This step should never fail, because we are rebasing commits for files
    # that should be ignored by the master repo.
    unless system "git rebase origin/master"
    raise "Rebase Failed! Please delete tmp/heroku_deploy and start again."
    end
    end

    # Precompile assets
    Rake::Task['assets:precompile'].invoke

    # Add any extra tasks you want to run here, such as compiling static pages, etc.


    # Add all changes and new files
    system 'git add -A'

    commit_message = "Added precompiled assets"

    if @new_repo
    # Create new commit for new repo
    system "git commit -m '#{commit_message}'", :out => "/dev/null"
    else
    # If existing repo, amend previous assets commit
    system "git commit --amend -m '#{commit_message}'", :out => "/dev/null"
    end
    puts "Commit: #{commit_message}"


    # Force push deploy branch to Heroku
    puts "Pushing #{deploy_branch} branch to master on #{remote}..."
    IO.popen("git push #{remote} #{deploy_branch}:master -f") do |io|
    while (line = io.gets) do
    puts line
    end
    end
    end
    end