3

I have a GIT repo and i'm using PHP script to pull from master branch and update files on server. It's really simple solution just to test it out. I wanted to add a webhook on github to send callback after successful push so i could pull from this repo.

Script runs both ways

  • from command line
  • from HTTP request

The difference is that when i run it from HTTP it doesn't pull new changes, just displays that it is up to date and doesn't do anything even if i'm 100% sure that there are new changes in repo. Output comes from GIT command so i'm sure it works and apache has proper rights to run that script file and commands which are used inside it.

When i run it via command line (through the same script or just clean git command) everything's fine, script pulls new changes and display standard GIT output.

PHP code:

echo "Pulling changes from master branch...\n";
$result = shell_exec('cd /var/git-test && git reset --hard HEAD && git pull');
if($result !== null){
  echo $result;
}else{
  echo $result;
  echo "Error in Git pull comand";
}

How can i fix that?

lukasz
  • 867
  • 1
  • 8
  • 17
  • 1
    Have you tried to manually run script with the apache user ? – Ôrel Oct 07 '15 at 07:12
  • 1
    How do you know it "displays that it is up-to-date"? – markus Oct 07 '15 at 07:16
  • Just in case also the warning that shell_exec should be disabled, it's not safe to have it on. – markus Oct 07 '15 at 07:17
  • Problem solved: Apache user "www-data" should have had its own ssh key generated which could be later used to authenticate pull requests on github by using webhooks. – lukasz Oct 08 '15 at 07:15
  • @Ôrel yes, that helped, i tested it out by running it `sudo -u www-data php deploy.php` and i figured out that it displays some additional info which wasn't returned by shell_exec() which was kind'a confusing. Git pull coudnt be performed because www-data didn't have his ssh key registered on github. – lukasz Oct 08 '15 at 07:21

1 Answers1

1

sudo -u www-data php deploy.php showed that www-data user didn't have ssh key registered on github and pull couldn't be performed. I couldn't switch to www-data user so i generated his key without switching to it like i did with running the script sudo -u www-data ssh-keygen -t rsa. After registering his key on github it works like a charm.

Also pro tip! shell_exec() doesnt show all output which was visible when i run the script as another user in command line. It was really confusing and cost me a lot of time to figure out where the error was.

lukasz
  • 867
  • 1
  • 8
  • 17