I wanted to do this in windows, and found the best way was to use Directory Monitor to check for changes then when it detected a change have it run:
Program: cmd.exe
Params: /C C:\pathToBatchFile.bat
That batch file contained:
c:
cd c:\gitRepoDirectory\
(if exist "%PROGRAMFILES(X86)%" (
"%PROGRAMFILES(X86)%\git\bin\sh.exe" --login -i -c "git commit -am AutoCommitMessage"
) else (
"%PROGRAMFILES%\git\bin\sh.exe" --login -i -c "git commit -am AutoCommitMessage"
))
I also tried having another command in there to add files ("%PROGRAMFILES(X86)%\git\bin\sh.exe" --login -i -c "git add *.*"
), but I don't think I got that working properly.
I also made a post-commit hook containing:
#!/bin/sh
git.exe pull -v --progress "origin"
git.exe push --progress "origin" master:master
curl.exe -s https://webserverdomain.com/updateFromGitHook.x?r=repoName
(If there were any conflicts then it would abort the pull and abort the push, but there wasn't any clear way to tell that had happened - in the end we abandoned the whole idea because of this one flaw.)
That curl command told my server that it needed to do a pull on the code. All that was needed to handle it in php was:
<?
$r = $_GET['r'];
if (!empty($c)) {
//use system instead of exec if you want the output to go back to the git client
exec("cd /path/to/repo/parent/$r; sudo git reset --hard HEAD; sudo git pull;");
echo "\n\nServer: Updated\n\n";
} else {
echo "\n\nServer: UPDATE FAILED\n\n";
}
?>
The only problem with that was it needed to be run by the root user instead of the apache user, so I also had to make a file in /etc/sudoers.d/
containing:
www-data ALL = NOPASSWD: /usr/bin/git
For me, I think that worked pretty solidly. Directory Monitor can be configured to run on startup and start minimized, and it can watch several different folders