Skip to content

Commit

Permalink
Add a git_repo role
Browse files Browse the repository at this point in the history
This role accepts a single complex data structure to
clone a git repository with multiple remotes and branches.

(cherry picked from commit 2fae914)
  • Loading branch information
wbclark committed Jan 22, 2024
1 parent b06a0ae commit 497028d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
7 changes: 7 additions & 0 deletions roles/git_repo/tasks/branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: "Configure repository branch"
block:
- name: "Create local branch"
ansible.builtin.command:
cmd: "git branch -f {{ branch }} {{ remote.name }}/{{ branch }}"
chdir: "{{ git_repo_repository.dir }}/{{ git_repo_repository.name }}"
15 changes: 15 additions & 0 deletions roles/git_repo/tasks/clone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: "Clone repository"
ansible.builtin.git:
repo: "{{ remote.url }}"
remote: "{{ remote.name }}"
version: "{{ (remote.branches[0]) if (remote.branches is defined) else 'HEAD' }}"
dest: "{{ git_repo_repository.dir }}/{{ git_repo_repository.name }}"
accept_newhostkey: yes

- name: "Configure additional branches for initial remote"
ansible.builtin.include_tasks: branch.yml
when: (remote.branches is defined) and (remote.branches | length > 1)
loop: "{{ remote.branches[1:] }}"
loop_control:
loop_var: branch
32 changes: 32 additions & 0 deletions roles/git_repo/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
- name: "Configure git repository"
ansible.builtin.include_tasks: "{{ 'clone.yml' if ansible_loop.first else 'remote.yml' }}"
loop: "{{ git_repo_repository.remotes }}"
loop_control:
loop_var: remote
extended: yes
label: "{{ remote.name }}"

- name: "Local bundle config"
when: git_repo_repository.install_gems is defined and git_repo_repository.install_gems
block:
- name: "Create .bundle/gems"
ansible.builtin.file:
path: "{{ git_repo_repository.dir }}/{{ git_repo_repository.name }}/.bundle/gems"
state: directory
- name: "Configure bundler to use .bundle/gems"
ansible.builtin.command:
cmd: "bundle config --local path .bundle/gems"
chdir: "{{ git_repo_repository.dir }}/{{ git_repo_repository.name }}"
- name: "Install Gems"
community.general.bundler:
state: present
chdir: "{{ git_repo_repository.dir }}/{{ git_repo_repository.name }}"

- name: "Create venv and install python packages"
ansible.builtin.pip:
name: "{{ git_repo_repository.python_packages }}"
virtualenv: "{{ git_repo_repository.dir }}/{{ git_repo_repository.name }}/env"
virtualenv_command: "python3 -m venv"
virtualenv_site_packages: yes
when: (git_repo_repository.python_packages is defined) and (git_repo_repository.python_packages | length >= 1)
26 changes: 26 additions & 0 deletions roles/git_repo/tasks/remote.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
- name: "Configure additional remote for repository"
block:
- name: "Configure remote url"
community.general.git_config:
scope: local
repo: "{{ git_repo_repository.dir }}/{{ git_repo_repository.name }}"
name: "remote.{{ remote.name }}.url"
value: "{{ remote.url }}"
- name: "Configure remote fetch"
community.general.git_config:
scope: local
repo: "{{ git_repo_repository.dir }}/{{ git_repo_repository.name }}"
name: "remote.{{ remote.name }}.fetch"
value: '+refs/heads/*:refs/remotes/{{ remote.name }}/*'
- name: "Fetch remote"
ansible.builtin.command:
cmd: "git fetch {{ remote.name }}"
chdir: "{{ git_repo_repository.dir }}/{{ git_repo_repository.name }}"

- name: "Configure branches for additional remote"
ansible.builtin.include_tasks: branch.yml
when: remote.branches is defined
loop: "{{ remote.branches }}"
loop_control:
loop_var: branch

0 comments on commit 497028d

Please sign in to comment.