Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A capistrano 3 task to tag the repo after every deploy
# Requires Capistrano 3.2 or later
namespace :deploy do
after :finishing, :tag_and_push_tag do
on roles(:app) do
within release_path do
set(:current_revision, capture(:cat, 'REVISION'))
# release path may be resolved already or not
resolved_release_path = capture(:pwd, "-P")
set(:release_name, resolved_release_path.split('/').last)
end
end
run_locally do
user = capture(:git, "config --get user.name")
email = capture(:git, "config --get user.email")
tag_msg = "Deployed by #{user} <#{email}> to #{fetch :stage} as #{fetch :release_name}"
tag_name = "#{fetch :stage }-#{fetch :release_name}"
execute :git, %(tag #{tag_name} #{fetch :current_revision} -m "#{tag_msg}")
execute :git, "push --tags origin"
end
end
end
@rsutphin

This comment has been minimized.

Copy link
Owner Author

@rsutphin rsutphin commented Feb 14, 2014

Based on a stackoverflow answer for cap2. The cap3 version has to be longer because cap3 doesn't (yet?) have current_revision or release_name.

On the plus side, that means that this task can be run outside a deploy if you prefer.

@rsutphin

This comment has been minimized.

Copy link
Owner Author

@rsutphin rsutphin commented Feb 14, 2014

Caveat: this will only work if you have modified capistrano to not use git archive when setting up release directories. If you're using the default cap3 git support, you'll need to find a different way to determine current_revision.

@rsutphin

This comment has been minimized.

Copy link
Owner Author

@rsutphin rsutphin commented Feb 17, 2014

Caveat: this will only work if you have modified capistrano to not use git archive when setting up release directories. If you're using the default cap3 git support, you'll need to find a different way to determine current_revision.

Updated so that this is no longer the case. The current version will work with stock cap3's git integration.

If you have modified how capistrano works with git (for instance, to add git submodule support), you may need to change either the write_revision task or the way :current_revision is determined.

@webhat

This comment has been minimized.

Copy link

@webhat webhat commented Mar 25, 2014

👍 Thanks for this!

@rsutphin

This comment has been minimized.

Copy link
Owner Author

@rsutphin rsutphin commented May 28, 2014

Update: As of capistrano 3.2, the git:write_revision task is not necessary — cap now does this itself.

@glaszig

This comment has been minimized.

Copy link

@glaszig glaszig commented Jan 13, 2015

you should add .strip to user and email. else the tag's message might look a little weird (for me at least, git version 1.9.3 (Apple Git-50)).

Deployed by Jon Doe
 <jon@doe.com>
> to production as 20150113204036
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment