Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically push changes to Git repository before deploying with Capistrano (cap deploy)
# I use this Capistrano task so I don't have manually run '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 it, 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
@pencilcheck

This comment has been minimized.

Copy link

commented May 16, 2014

constant Capistrano::Error not found

@davejamesmiller

This comment has been minimized.

Copy link
Owner Author

commented Aug 18, 2014

It may need modifying for Capistrano 3 - I don't use it any more so I can't test it.

@MichalRemis

This comment has been minimized.

Copy link

commented Nov 2, 2016

Just replace raise Capistrano::Error, with abort for Capistrano 3

@mib32

This comment has been minimized.

Copy link

commented Mar 9, 2018

Lifesaver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.