Zack Burt
Zack Burt

Reputation: 8455

Using a PHP script as a post-commit hook, to trigger a bash script, to do a git-pull?

Here's the bash script:

#!/bin/bash

echo "$NODE_ENV"
(
echo $USER;
echo $PATH;
cd /opt/chat-staging/;
git pull;
NODE_ENV=staging;
echo "Set NODE_ENV to $NODE_ENV";
node leaderboard.js & node app.js;
echo "Started leaderboard.js";
echo "Started app.js";
)
echo "NODE_ENV is now $NODE_ENV"

Here's the PHP script:

<?php

exec('/bin/bash /opt/chat-staging/start_app.sh 2>&1', &$output, &$return_var);
print_r($output);
print $return_var;

?>

I have it set up as a github post-commit hook, but this is the output:

Array ( [0] => [1] => www-data [2] => /usr/bin [3] => /usr/lib/git-core/git-sh-setup: 90: sed: not found [4] => basename: write error: Broken pipe [5] => /usr/lib/git-core/git-sh-setup: 212: uname: not found [6] => Host key verification failed. [7] => fatal: The remote end hung up unexpectedly [8] => Set NODE_ENV to staging [9] => /opt/chat-staging/start_app.sh: line 11: node: command not found [10] => /opt/chat-staging/start_app.sh: line 11: node: command not found [11] => Started leaderboard.js [12] => Started app.js [13] => NODE_ENV is now ) 0

/opt/chat-staging/ is owned by the "www-pub"; in /etc/groups, I have "www-pub:x:1000:www-data,root".

So why is this not working? I even have an RSA key in /var/www/.ssh/id_rsa and the public key added as a GitHub deploy key.

EDIT, ADDENDUM

I added a git remote-v; to the script, right before "git pull".

root@li70-243:/opt/chat-staging# su - www-data
$ bash /opt/chat-staging/start_app.sh
origin  [email protected]:zackster/CompassionPit--Node-.git (fetch)
origin  [email protected]:zackster/CompassionPit--Node-.git (push)
Already up-to-date.

Whereas I still see PHP output like...

Array ( [0] => origin [email protected]:zackster/CompassionPit--Node-.git (fetch) [1] => origin [email protected]:zackster/CompassionPit--Node-.git (push) [2] => master [3] => * reputation [4] => /usr/lib/git-core/git-sh-setup: 90: sed: not found [5] => basename: write error: Broken pipe [6] => /usr/lib/git-core/git-sh-setup: 212: uname: not found [7] => /usr/lib/git-core/git-pull: 244: sed: not found [8] => Your configuration specifies to merge with the ref 'reputation' [9] => from the remote, but no such ref was fetched.

Upvotes: 1

Views: 1691

Answers (1)

rodneyrehm
rodneyrehm

Reputation: 13557

Your PHP script seems to work fine. What's not "working fine" is the exec(). You expect the PATH environment variable to be set to whatever your regular shell user has set. It is not. You need to set the environment for the bash script (and the tools therein) to work.

do echo $PATH in your shell and add that to your exec:

exec('export PATH=/usr/bin:/usr/local/bin:/some/other/paths && /bin/bash /opt/chat-staging/start_app.sh 2>&1', &$output, &$return_var);

and try your script again. You could also move that PATH to your shell script. You could even copy your .bashrc (or .bash_profile or whatever) and load that with source …/.bashrc.

instead of exec() you could also look into proc_open() with which you have much more control over the process to execute.


github explains how to hook into post receive events: http://help.github.com/post-receive-hooks/

Upvotes: 4

Related Questions