gitlocalhostremote-servergithooksgit-post-receive

Hook post-receive does not work after push from local to remote repository


Description of the problem:

I have a problem with starting the post-receive hook automatically in the git repository. After made push from the local to the remote repository, hook post-receive just doesn't execute.


Steps that I did:

  1. I create a (non-bare) repository on the local machine and create several commits
  2. I create a repository on the remote machine (bare), create a post-receive hook there and set it to chmod to 755
  3. On the local machine i add a remote repository (git remote add .....)
  4. I make a push that runs correctly
  5. Hook, unfortunately, does not fire

My environments:


Content of post-receive file at remote machine:

#!/bin/sh
git --work-tree=/home/xxxxxx/xxxxxx/public_html/prod --git-dir=/home/xxxxxx/xxxxxx/dev.git checkout -f


Other SO solutions?:

Yes, I've seen a lot, but none solve my problem. I've seen some answers with "unset GIT_DIR" advice, but I'm afraid it has nothing to do with it, because even just the echo test > log.txt does not work in post-receive. It looks like post-receive can't be started?


Solution

  • Ok, I've found what the problem was - the partition on the host is mounted as noexec. If so, the hooks cannot work.

    This is for security reasons. It's typical situation on shared hosting.


    My alternative solution:

    We can create a git alias where you can run a bash script with ssh logging and run the git command directly on the server

    How to do it?

    In the local repository configuration file we add an alias:

    [alias]
        run = "!sh ./hook.sh"
    

    (as you can see in the example, this alias will launch the hook.sh file)

    Now we create a hook.sh file with git commands

    #!/bin/bash
    
    ssh user@host -p [port] 'bash -s' <<-EOF
        git --work-tree=/home/xxxxxx/xxxxxx/public_html/prod --git-dir=/home/xxxxxx/xxxxxx/dev.git checkout -f
        exit
    EOF
    
    

    And now we just have to use the git run command

    Note: This is just a simple example. You have to test it on your envoirment!