Skip to content

Instantly share code, notes, and snippets.

@chengguangnan
Last active December 20, 2015 20:58
Show Gist options
  • Save chengguangnan/6193745 to your computer and use it in GitHub Desktop.
Save chengguangnan/6193745 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.

Change Log

  • Use branch fetch(:branch) rather than master.
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 != fetch(:branch) && !ENV["IGNORE_BRANCH"]
raise Capistrano::Error, "Not on #{fetch(:branch)} branch (set IGNORE_BRANCH=1 to ignore)"
end
# Push the changes
if ! system "git push"
raise Capistrano::Error, "Failed to push changes to #{fetch(:repository)}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment