7

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?

Matt V.
  • 9,703
  • 10
  • 35
  • 56

3 Answers3

10

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:
repo=git@github.com: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:
repo=git@github.com:username/reponame.git
dest={{ path }}
version: latest_tag.stdout
Community
  • 1
  • 1
Bret
  • 214
  • 2
  • 9
  • 1
    I have not tested that code. If its helpful and needs some tweaks, please edit those fixes in. – Bret Mar 20 '15 at 00:43
3

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 }}"
dannysauer
  • 3,793
  • 1
  • 23
  • 30
1

Solved it with curl...sample is about docker-compose

- name: Get version number of latest docker-compose
  shell:
    cmd: curl --silent 'https://api.github.com/repos/docker/compose/releases/latest' |  grep '"tag_name":' | cut -d'"' -f4
  register: docker_compose_latest_svn
  changed_when: false
  args:
    warn: no

- set_fact:
    docker_compose_latest: "{{ docker_compose_latest_svn.stdout }}"
  when:
    - docker_compose_latest_svn.stdout is defined
VladoPortos
  • 563
  • 5
  • 21