kanlukasz
kanlukasz

Reputation: 1314

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?

Upvotes: 3

Views: 975

Answers (2)

Angel Estrada
Angel Estrada

Reputation: 1

I know there is already an accepted response, but seems the issue comes 'cause we are trying to run with bash instead of shell I change the post-receive hooks to the following:

#!/bin/bash

TARGET="/dir_path/live.git"
GIT_DIR="/dir_path/project.git"

 git --work-tree=$TARGET  --git-dir=$GIT_DIR  checkout -f

Upvotes: -2

kanlukasz
kanlukasz

Reputation: 1314

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!

Upvotes: 0

Related Questions