IrinaS
IrinaS

Reputation: 109

clone Git Repository

I wrote an ansible playbook which creates a new BitBucket repository. Now I want to configure the repository according to the instructions shown under the repository source in BitBucket:

You have an empty repository To get started you will need to run these commands in your terminal. New to Git? Learn the basic Git commands Working with your repository My code is ready to be pushed If you already have code ready to be pushed to this repository then run this in your terminal.

cd existing-project
git init
git add --all
git commit -m "Initial Commit"
git remote add origin http://xxxxxxx/scm/project/my-repo.git
git push -u origin master
My code is already tracked by Git
If your code is already tracked by Git then set this repository as your "origin" to push to.**

All done with the commands?
Refresh

Can I do this with the Git Module? Or how can I run those commands in ansible? I only managed to clone the repo with the Git Module.

Thanks

Upvotes: 1

Views: 663

Answers (2)

juanlumn
juanlumn

Reputation: 7125

As mentioned in the answer by @Dan Farrell Ansible Git doesn't seems to allow push.

As an initial approach you can try something like:

- name: Git push.
  tasks:
    - name: Set git origin
      shell: "git remote set-url origin https://gitUserName:[email protected]/storage/repo.git"

    - name: Push to origin.
      shell: "git push origin yourBranch" 

Upvotes: 2

erik258
erik258

Reputation: 16304

https://docs.ansible.com/ansible/latest/modules/git_module.html seemse to support pulling, but I don't see any way to push with that module.

If I recall correctly, the git module for ansible simply wraps the git executable, so it should be relatively easy to write your own shell or command tasks to execute the git commands you want.

You'd probably want to use the copy module with the content setting to create a new file (git does not allow "empty" commits as it only tracks changes, and thus cannot commit without changes), and then run the git add and git commit steps conditionally on the file changing.

It's not in my experience a common practice to create git repos and manage their contents with Ansible. I wonder at the utility of it ( you'd assumedly want to check the ansible itself into a repo to track changes to the ansible, so you end up doing manual git actions anyway.) But certainly possible.

Upvotes: 1

Related Questions