简体   繁体   中英

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

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]

The following works for me for a git task in Ansible 1.9 / 2.0

- name: Checkout a single file from project directory (into a tar archive)
  connection: local
  local_action:
    command git archive
        --remote={{ local_git_repo }}
        HEAD
        knownGoodSet.cfg
        -o /tmp/knownGoodSet.tar
  sudo: no

You will have a different git command of course, but I believe you need all of the following to get it working:

  • connection: local
  • local_action: command ... instead of shell: ...
  • sudo: no

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM