Skip to content

Instantly share code, notes, and snippets.

@rsutphin
Last active April 18, 2018 13:08
Show Gist options
  • Save rsutphin/9010923 to your computer and use it in GitHub Desktop.
Save rsutphin/9010923 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link
Author

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
Copy link

webhat commented Mar 25, 2014

👍 Thanks for this!

@rsutphin
Copy link
Author

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

@glaszig
Copy link

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