How to check out most recent git tag using Ansible?
Solution 1
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
Solution 2
This is a bit of an old question, but there's a github_release
module for this if one happens to be pulling from GitHub. That obviously doesn't help everyone, but it helped me:
- name: Install annoyingly unpackaged python module requirement
pip:
state: latest
umask: "0022"
name: github3.py
- name: Find current release tag
github_release:
user: "{{ github_user }}"
repo: "{{ github_repo }}"
action: latest_release
register: release_data
- name: Fetch current release
git:
repo: "https://github.com/{{ github_user }}/{{ github_repo }}.git"
dest: "{{ source_dir }}"
version: "{{ release_data.tag }}"
Matt V.
Matt Vance is a Web developer, technology consultant, and freelance writer living in Austin, Texas. He has written for Macworld magazine, Lifehacker.com, and DZone.
Updated on June 11, 2022Comments
-
Matt V. almost 2 years
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?
-
Bret about 9 yearsI have not tested that code. If its helpful and needs some tweaks, please edit those fixes in.
-
VladoPortos about 3 yearsDoes not work with python 2.7, github3.py does not supports it :(
-
dannysauer about 3 yearsSince python2 reached EOL in Jan 2020, that's not terribly shocking.