Alexey
Alexey

Reputation: 1424

How to check out the previous tag on a specific branch?

Our automated installation script is supposed to work like this:

  1. clone some specific tag
  2. run the installation script
  3. if something goes wrong, run the rollback script

The rollback script is supposed to work like this:

  1. run some rollback subscripts
  2. check out the previous tag from production branch
  3. run some subscripts from the previous release

How can I do the bolded part? I know how to get the previous commit (~1), but how to get the one that was tagged?

Upvotes: 0

Views: 877

Answers (2)

jingx
jingx

Reputation: 4014

git describe --tags --abbrev=0 <commit> will give you the most recent tag from the commit.

Upvotes: 2

Mark Adelsberger
Mark Adelsberger

Reputation: 45659

You'll need some sort of convention to make this work. So far you know that you want to find a tag reachable from a branch, and that it should be the first tag you find (not counting the one for the current release? not clear to me if that one is reachable from production... but anyway...)

That's not enough to go on for git.

One solution would be to always have a specific tag on the "previous release". Rollback step 2 would do git checkout prev_release, and release step 4 would be "if we didn't roll back, move the prev_release tag".

Or if your build server keeps its repo around and always has it checked out to the most recent production release, then you could use the reflog. git checkout -. If you're re-cloning before checking out the tag for the new release, this won't work. (Unclear to me because you said "clone some specific tag", which is imprecise language. You check out a specific tag, which you might do with the -b option of clone.)

Or if your tag names follow a pattern, you could calculate the previous tag name (but generally this isn't a reliable idea if the tags are version numbers).

Or if you use something like gitflow, where every commit on your production branch is a release, you can just say production (or production^ if you've already merged the new release). This is one of the very nice things about gitflow.

Upvotes: 1

Related Questions