Skip to content

Instantly share code, notes, and snippets.

@rafi
Last active August 10, 2019 11:51
Show Gist options
  • Save rafi/a7ecce7d008782175b95 to your computer and use it in GitHub Desktop.
Save rafi/a7ecce7d008782175b95 to your computer and use it in GitHub Desktop.
Ansible prepare a detached git repository and deploy
# -----------------------------------------------
# ENSURE GIT REPOSITORIES
- name: Git | Ensure server repository
sudo_user: git
git: repo={{ upstream }}
dest={{ repo_dir }}
bare=yes
update=no
tags: git
- name: Git | Ensure repository set properly
lineinfile: dest={{ repo_dir }}/config
regexp="^\s*bare.?="
line=" bare = false"
tags: git
- name: Git | Ensure proper worktree
lineinfile: dest={{ repo_dir }}/config
regexp='^\s*worktree.?='
line=" worktree = {{ work_dir }}"
tags: git
- name: Git | Ensure work dir
file: path={{ work_dir }} state=directory
owner=git group=developers mode=0775
tags: git
# -----------------------------------------------
# GIT FETCH
- name: Git | Fetching new changes
shell: chdir={{ repo_dir }} git fetch --prune && git fetch --tags
tags: git
# -----------------------------------------------
# GIT CHECKOUT
- name: Git | Checkout version
command: chdir={{ repo_dir }} git checkout -f {{ version }}
tags: git
# -----------------------------------------------
# GIT SUBMODULES
- name: Submodules | Syncing
command: chdir={{work_dir}} git --git-dir={{repo_dir}} submodule sync
tags:
- update
- git
- name: Submodules | Updating
command: chdir={{work_dir}} git --git-dir={{repo_dir}} submodule update -f --init --recursive
tags:
- update
- git
@rafi
Copy link
Author

rafi commented Jul 10, 2014

Example of vars:

upstream: ssh://[email protected]/user/app.git
repo_dir: /home/git/app.git
work_dir: /srv/http/app

Example of usage:

ansible-playbook -i hosts deploy.yml -e "version=0.5.9"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment