Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add/update git tag from local machine using ansible playbook?

I'm using the following ansible playbook to deploy my applications and I would like to add a tagging role so I can automatically add/update a tag to mark the current commit as the one that has been deployed.

Attempt

My current attempt is as follow, based on How can I move a tag on a git branch to a different commit?:

---
- name: Removes the tag in local repository.
  shell: git tag -d {{git_deploy_tag}}
  tags: [tagging]

- name: Removes the tag in remote repository.
  shell: git push origin :refs/tags/{{git_deploy_tag}}
  tags: [tagging]

- name: Adds the tag to different commit (HEAD).
  shell: git tag {{git_deploy_tag}} HEAD
  tags: [tagging]

- name: Pushes the changes to the remote repository.
  shell: git push origin {{git_branch}} --tags
  tags: [tagging]

Problem

This role is run on the remote host that doesn't have access to the git repository, and I intend to keep it so. I was unable to run the role on my local machine following Run command on the Ansible host.

Question

How do I run the tagging role locally (other roles should run on the remote). Fabric script have a local() method

like image 787
Édouard Lopez Avatar asked Nov 19 '25 16:11

Édouard Lopez


1 Answers

Add your localhost to the ansible inventory and split your playbook in multiple plays, what you want to be run locally first and what you want to run remotely.

What follows ins an example only, but might work as a base for you. Also be aware ansible runs tasks in parallel, there is an option to chose how many parallel tasks to run, you need executed one task at time.

---
- hosts: local

tasks:
  - name: Removes the tag in local repository.
    shell: git tag -d {{git_deploy_tag}}
    tags: [tagging]

- hosts: remote

tasks:
  - name: Removes the tag in remote repository.
    shell: git push origin :refs/tags/{{git_deploy_tag}}
    tags: [tagging]
like image 85
E. Celis Avatar answered Nov 21 '25 07:11

E. Celis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!