Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check out most recent git tag using Ansible?

Is there an easy way to have Ansible check out the most recent tag on a particular git branch, without having to specify or pass in the tag? That is, can Ansible detect or derive the most recent tag on a branch or is that something that needs to be done separately using the shell module or something?

like image 759
Matt V. Avatar asked Mar 19 '15 19:03

Matt V.


People also ask

How do I pull latest tag?

JB. JB. Returns the latest tag in the current branch. To get the latest annotated tag which targets only the current commit in the current branch, use git describe --exact-match --abbrev=0 .

How do I check out a git tag?

In order to checkout a Git tag, use the “git checkout” command and specify the tagname as well as the branch to be checked out. Note that you will have to make sure that you have the latest tag list from your remote repository.

Does git tag tag the latest commit?

Usually, you want to name your tag following the successive versions of your software. Using this command, the latest commit (also called HEAD) of your current branch will be tagged. To verify that your Git tag was successfully created, you can run the “git tag” command to list your existing tags.


1 Answers

Ansible doesn't have checking out of the latest tag as a built in feature. It does have the update parameter for its git module which will ensure a particular repo is fully up to date with the HEAD of its remote.

---
- git:
[email protected]:username/reponame.git
dest={{ path }}
update=yes
force=no

Force will checkout the latest version of the repository overwriting uncommitted changes or fail if set to false and uncommitted changes exist.

See http://docs.ansible.com/git_module.html for more options on this module.

You could do two things at this point:

1) Have a separate branch with your tags on it, and just stay up to that using the update parameter.

2) You could also use the shell module and implement something similar to: Git Checkout Latest Tag

---
- name: get new tags from remote
  shell: "git fetch --tags"
  args:
    chdir: "{{ path }}"

- name: get latest tag name
  shell: "git describe --tags `git rev-list --tags --max-count=1`"
  args:
    chdir: "{{ path }}"
  register: latest_tag

And then use that result as a refspec with the git module

- git:
[email protected]:username/reponame.git
dest={{ path }}
version: latest_tag.stdout
like image 129
Bret Avatar answered Nov 15 '22 04:11

Bret