'Ansible/Git code deployment, how do I get just code?

(SOLVED) Everything I'm doing with my ansible deployment is functional up to the point where I fetch the Git bare repo to the remote server - this works but then I'm stuck.

I know you can use a post-receive hook to issue a command that copies the site code to the right place on the server, because I've done it before, but that wasn't automated with ansible.

git --work-tree={{ target_dir }} --git-dir={{ git_bare_dir }} checkout -f

The problem is that creating the bare repo using ansible means there's no hook so the raw code doesn't get copied.

Which means the site-specific composer stuff can't run because it's not in place.

I have tried adding an ansible task after the git clone:

    - name: Checkout the source code from the bare repo.
      command:
        cmd: "git --work-tree={{ target_path }} --git-dir={{ git_bare_dir }} checkout -f"
        chdir: "{{ git_bare_dir }}"

But it doesn't work - there's an error message that says it's not a git repo. I've tried different ways of doing this. Running it from the command line (in the right directory) on the server gives the same error.

If it's relevant (probably not) I'm deploying to a Digital Ocean droplet.

SOLUTION: (I'm adding the solution here because I can't "self-answer" yet.)

I'm not sure this is the best option but I used this:

    - name: Copy a bare repo from Git to the remote server.
      git:
        repo: "{{ git_repo_url }}" # Use ssh: url, so we're not asked for name/password
        version: "{{ git_repo_version }}"
        dest: "{{ target_path }}"
        separate_git_dir: "{{ git_bare_dir }}"
        force: yes
        single_branch: yes
        depth: 1
        accept_hostkey: yes
        key_file: "{{ github_deploy_key }}"

Although the ansible names are the same, it's not a bare repo. The code gets copied to dest while the report itself is at git_bare_repo (which is way outside the accessible part of the server).



Solution 1:[1]

The problem is that creating the bare repo using ansible means there's no hook so the raw code doesn't get copied.

An alternative approach is to:

  • automate the creation of the bare repository
  • copy (builtin copy module) a hooks folder from your local Ansible server to the target bare repo folder, which would include the relevant hooks pre-populated.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonC