-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a git_repo role #1580
Merged
Merged
Add a git_repo role #1580
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
This role clones a set of git repositories, handling multiple remotes and managing different branches for each remote. It also provides basic support for bundle installs for Ruby projects, and pip installs within a virtual environment for Python projects. The role aims to present a single, simple interface in Ansible variables for managing a collection of locally cloned git repositories. | ||
|
||
Role Variables | ||
-------------- | ||
|
||
The main data structure for the role is the list of `git_repositories`. Each repository in the list requires the following attributes: | ||
|
||
- `name`: The local name of the repository. | ||
- `dir`: The parent directory to clone the repository from. | ||
- `remotes`: List of repository remotes, each with `name`, `url`, and `branches`. | ||
|
||
Repositories may also have the following optional attributes: | ||
|
||
- `install_gems`: Boolean to indicate if Ruby gems should be installed. This assumes the cloned repository provides a Gemfile. | ||
- `python_packages`: A list of python packages to install in a venv within the repository. | ||
|
||
Each remote requires the following attributes: | ||
|
||
- `name`: Name of the remote. | ||
- `url`: URL of the remote. | ||
- `branches`: A list of branches in the remote repository that will be created in the local clone. | ||
|
||
Examples | ||
-------- | ||
|
||
Basic example of the `git_repositories` variable, configuring two repositories, each with a single remote and a single branch: | ||
|
||
```yaml | ||
git_repositories: | ||
- name: 'foreman' | ||
dir: '/home/vagrant' | ||
remotes: | ||
- name: 'origin' | ||
url: 'git@github.com/my_github_user/foreman.git' | ||
branches: | ||
- 'develop' | ||
- name: 'katello' | ||
dir: '/home/vagrant' | ||
remotes: | ||
- name: 'origin' | ||
url: 'git@github.com/my_github_user/katello.git' | ||
branches: | ||
- 'master' | ||
``` | ||
|
||
Example configuring a repository with multiple remotes and branches and installing gems from the project's Gemfile. The checkout will be of the first branch on the first remote when performing the bundle install: | ||
|
||
```yaml | ||
git_repositories: | ||
- name: 'foreman' | ||
dir: '/home/vagrant' | ||
remotes: | ||
- name: 'myfork' | ||
url: 'git@github.com/my_github_user/foreman.git' | ||
branches: | ||
- 'develop' | ||
- 'exciting-new-feature' | ||
- 'fix-annoying-bug' | ||
- name: 'upstream' | ||
url: 'git@github.com/theforeman/foreman.git' | ||
branches: | ||
- '3.9-stable' | ||
- '3.8-stable' | ||
install_gems: true | ||
``` | ||
|
||
Installing Python Packages in a Virtual Environment: | ||
|
||
```yaml | ||
git_repositories: | ||
- name: 'rpm-packaging' | ||
dir: '/home/vagrant' | ||
remotes: | ||
- name: 'downstream' | ||
url: 'git@gitlab.example.com/systems_management/rpm-packaging.git' | ||
branches: | ||
- 'STREAM' | ||
- 'VERSION-1' | ||
- 'VERSION-2' | ||
- 'VERSION-3' | ||
python_packages: | ||
- 'ansible' | ||
- 'obal' | ||
- 'tito' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
dependencies: | ||
- role: git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
- name: "Create local repository branch tracking specified remote branch" | ||
ansible.builtin.command: | ||
cmd: "git branch -f {{ branch }} {{ remote.name }}/{{ branch }}" | ||
chdir: "{{ git_repo_repository.dir }}/{{ git_repo_repository.name }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried to use the git_config module for this sort of thing, which was suggested by the maintainers of the git module as that module doesn't support everything that we need to do here, but branch creation was the one area where I could never make git_config work.
Doing it with the command module smells somewhat hacky, but it's not only worked very reliably, it also ends up being quite a bit simpler.