Skip to content

Instantly share code, notes, and snippets.

@pablomarti
Created May 14, 2013 22:32
Show Gist options
  • Save pablomarti/5580205 to your computer and use it in GitHub Desktop.
Save pablomarti/5580205 to your computer and use it in GitHub Desktop.

I use this Capistrano task so I don't have manually do git push before cap deploy. It includes some error checking to make sure I'm on the right branch (master) and haven't got any uncommitted changes.

Simply add the code below to config/deploy.rb, then run cap deploy:push to test, and cap deploy to deploy as usual.

namespace :deploy do
desc "Push local changes to Git repository"
task :push do
# Check for any local changes that haven't been committed
# Use 'cap deploy:push IGNORE_DEPLOY_RB=1' to ignore changes to this file (for testing)
status = %x(git status --porcelain).chomp
if status != ""
if status !~ %r{^[M ][M ] config/deploy.rb$}
raise Capistrano::Error, "Local git repository has uncommitted changes"
elsif !ENV["IGNORE_DEPLOY_RB"]
# This is used for testing changes to this script without committing them first
raise Capistrano::Error, "Local git repository has uncommitted changes (set IGNORE_DEPLOY_RB=1 to ignore changes to deploy.rb)"
end
end
# Check we are on the master branch, so we can't forget to merge before deploying
branch = %x(git branch --no-color 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \\(.*\\)/\\1/').chomp
if branch != "master" && !ENV["IGNORE_BRANCH"]
raise Capistrano::Error, "Not on master branch (set IGNORE_BRANCH=1 to ignore)"
end
# Push the changes
if ! system "git push #{fetch(:repository)} master"
raise Capistrano::Error, "Failed to push changes to #{fetch(:repository)}"
end
end
end
if !ENV["NO_PUSH"]
before "deploy", "deploy:push"
before "deploy:migrations", "deploy:push"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment