Last active
November 2, 2016 16:36
-
-
Save MichalRemis/c8c0320f72d4f246f3116eebede58d40 to your computer and use it in GitHub Desktop.
Ruby on Rails: Automatically push changes to Git repository before deploying with Capistrano (cap deploy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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$} | |
abort "Local git repository has uncommitted changes" | |
elsif !ENV["IGNORE_DEPLOY_RB"] | |
# This is used for testing changes to this script without committing them first | |
abort "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"] | |
abort "Not on master branch (set IGNORE_BRANCH=1 to ignore)" | |
end | |
# Push the changes | |
if ! system "git push #{fetch(:repository)} master" | |
abort "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