Rana
Rana

Reputation: 6154

Git post-receive hook error on server

I am trying to have a git 'post-receive' on my server. I am using the following code on the hook file:

#!/bin/bash
#CONFIG
LIVE="/home/ubuntu/public_html/testing"

read oldrev newrev refname
if [ $refname = "refs/heads/master" ]; then
  echo "===== DEPLOYING TO LIVE SITE ====="
  unset GIT_DIR  
  cd $LIVE
 # ssh-agent $BASH
 # ssh-add /home/ubuntu/.ssh/ubuntu
  git pull --verbose origin  master || echo "git-pull: returned error code"     
  echo "===== DONE ====="
fi

It is showing the following error mesasge whenever I am trying to push something from my local pc to server:

===== DEPLOYING TO LIVE SITE =====
remote: error: cannot open .git/FETCH_HEAD: Permission denied
remote: 
remote: git-pull: returned error code
remote: ===== DONE =====

Can anyone please help what can be the issue?

Just to confirm, I have tried with the same user credential to run the command via ssh terminal and it worked fine.

Thanks in advance.

Upvotes: 3

Views: 1298

Answers (2)

Rana
Rana

Reputation: 6154

At last, I did able to solve it by changing the ownership of the directory(/home/ubuntu/public_html/testing) to the user who is commiting/running hook.

Upvotes: 2

Peter O'Callaghan
Peter O'Callaghan

Reputation: 6186

Whenever I've received error messages about permissions and FETCH_HEAD, it's because I've accidentally made a pull as root whilst tinkering around with something, which creates files in the .git directory owned by root, which my normal ssh user can't overwrite.

I usually run ls -laR .git | grep root (I'm sure there's a better way, I'm no sysadmin) to check if there are any files owned by root. Simply chown'ing them back allows me to carry on as normal.

Upvotes: 0

Related Questions