Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing git commands via php over http

OS - Ubuntu 14.04

I am working on deployment using GIT webhooks for it.

I have added the deployment keys to git repo and now I want to trigger the git pull origin master command when a push happens from my local repo.

This is the test.php file I call via browser:

<?php       
    //echo "THis is a test file on a test repo for testing the deploy functionality using github webhooks!!!!";
    echo exec('whoami');
    echo exec('sh -x /var/www/proj/test/git.sh');
?>

This is the git.sh shell file:

#!/bin/bash
cd /var/www/proj-dir/test
git pull origin master

When I run this on terminal using php test.php I get the correct result as expected:

 ubuntu From github.com:repo/test
 * branch            master     -> FETCH_HEAD
 Already up-to-date.

ubuntu for whoami followed by the git pull output.

Now here's the issue when I call the same http://example.com/test.php via the browser it shows the user or whoami output as www-data, which is apache user, but I tried updating the permissions of the php file to execute and changing the user to www-data but did not work.
checked the apache logs and when I execute through the browser I get a permission error

  Please make sure you have the correct access rights and the repository exists. + cd /var/www/proj/deploy-test + git pull origin master 

Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

What updates do I need to make so the file works via browser request?

If I need to update the sudoers file what should be the updates?

Update
I added the .ssh keys to the var/www/ dir as that is the home for apache user. But I still get the

git pull origin master
error: cannot open .git/FETCH_HEAD: Permission denied

Also I added a line for the www-data user to be able to execute the sh file.

 www-data ALL=(anthony) NOPASSWD: /var/www/mysite/vendor/tmd/auto-git-pull/scripts/git-pull.sh

Reference here Still no luck
Updated the permissions for .git folder to www-data user

sudo chown www-data:www-data /var/www/proj/test/.git

like image 642
KillABug Avatar asked Nov 10 '22 06:11

KillABug


1 Answers

This does not seem to be a PHP issue but a git config issue. Git is configured correctly for user ubuntu and not for user www-data.

You can try to make PHP run something as ubuntu, but that does not seem the easiest nor the correct. I would suggest configuring git correctly for the www-data user.

I suspect you can reproduce the issue by running on the terminal:

# become www-data user
sudo su www-data
# actions from your git.sh file
cd /var/www/proj-dir/test
git pull origin master

After verifying you do indeed have a reproduction scenario you can try to fix the problem for the www-data user.

Chances are there is a difference between the output of git config --list when run as either user. For more help on that score see https://git-scm.com/book/en/Getting-Started-First-Time-Git-Setup

It might als be that file permission / ownership differences are causing problems. To rule out, I suggest you use a git clone ... as user www-data to create a new clone and working copy in some other path in your filesystem. Then see again if the git pull now works as expected.

You may consider giving www-data its own working copy anyway (regardless of permission problems). This would also prevent having to deal with possible uncommitted changes in the working copy that result in merge problems. Handling these automatically / unattended from PHP / git.sh might be cumbersome.

The proposed workflow would then be: develop as ubuntu in your current working directory and add/commit/push your changes. Then have PHP do the pull as itself (as www-data).

like image 186
Paul van Leeuwen Avatar answered Nov 14 '22 23:11

Paul van Leeuwen