Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'git pull' command work from terminal but not with php shell_exec() via git repository hook

I have create a webhook in my github repository which post on the hook url on my live server to run pull command for update my repo files on the server.

The problem is the hook file which i have created is in the /var/www/site/web/hookfile.php (the post request is going there. i am getting the body response also)

and my repo files are in /var/www/git-repo/

its not updating the git-repo when i push anything to my github repository. I run this command using terminal and its working.

cd /var/www/git-repo && git pull

But through my php file its not working

shell_exec('cd /var/www/git-repo && git pull')
like image 698
Harish Kumar Avatar asked Jun 10 '16 16:06

Harish Kumar


People also ask

How do I pull something from GitHub using terminal?

The simple command to PULL from a branch is: git pull 'remote_name' 'branch_name' .

How do I pull code from repository?

We can pull the repository by using the git pull command. The syntax is given below: $ git pull <options><remote>/<branchname> $ git pull origin master.

What is Shell_exec?

The shell_exec() function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix. If the command fails return NULL and the values are not reliable for error checking.


1 Answers

shell_exec() fail silently because only report STDOUT and not STDERR.

Try with:

echo shell_exec("cd /var/www/git-repo && /full/path/to/bin/git pull 2>&1");

Normally is a permission error, and could be fixed adding permission to the user that execute php (apache?)

chown -R www-agent:www-agent repository/

But could be also a connection error to the remote repository (authentication, ssh-keys, ...).

like image 52
Dario Avatar answered Sep 24 '22 09:09

Dario