I want to be able to execute a php hook on post-receive hook, to copy files from the git repo to web folder on the same server and only run if it was pushed was made on a master branch ignoring other branches. Below is what I've got so far.
!/usr/bin/php
<?php
exec("git archive master | tar -x -C /var/www/", $output);
?>
Basically, im not sure how to access git arguments using php.
Don't forget a post-receive hook doesn't take arguments: it reads data on stdin.
The "post-receive" script is run after receive-pack has accepted a pack and the repository has been updated. It is passed arguments in through stdin in the form:
<oldrev> <newrev> <refname>
So you will need to read said arguments to extract the branch (the example is in bash but you can adapt it)
Related
I'm working on a php application that pulls github repositories to the server. Github webhooks call the php files.
I want to execute a cmd command using php. I assume I need apache permissions but I don't know how to give them.
The php code below creates an mkdir.bat and a gitclone.bat and runs them. The mkdir runs successfully, it creates an empty folder, but the gitclone doesn't create any folders or files. When I run the gitclone manually it does create folders and files.
file_put_contents("mkdir.bat", "mkdir test");
exec("mkdir.bat");
file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat");
I executed your code in my computer
file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat 2>&1",$o);
print_r($o);
the extra code is to check the output from cmd and it returned "git command not exsists"
so you only have to add a line as
<?php
putenv("PATH=C:\Program Files\Git\cmd");
file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat 2>&1",$o);
print_r($o);
?>
This was working properly.
i searched on the internet for hours to find a good solution but all the topics that allready exits dont help for me
i try to build a webhook to update my website by doing a git pull that is called afther a git hook when commits pushed to my live branche. But it isn't working when i try to call git pull by requesting my php script in the browser.
apache is running as www-data, all the rights a set up well.
the php script is running good when i try to run it as www data user with su - www-data and than php webHook.php
but when i call it in the browser, i get no response. I guess apache is blocking
this is the command i use in the php script:
echo shell_exec('cd /home/my-site/public_html && git pull');
the result is empty and the git repository is not pulled at all.
but if i try this:
echo shell_exec('cd /home/my-site/public_html && git');
i got this result
usage: git [--version] [--exec-path[=]] [--html-path] [--man-path] [--info-path] [-p|--p..........
so everything is working, except the git pull command.
the respository is saved on another server with atlassian stash. and i saved the password with git config --global credential.helper "cache --timeout=360000000"
I am using GIT on my server and I am trying to get a PHP file to be executed each time I update my repository. I'm trying to use my post-update hook to achieve this.
this is the code I tried:
#!/bin/sh
echo
echo "**** Pulling changes into Prime [Hub's post-update hook]"
echo
cd $HOME/www || exit
unset GIT_DIR
git pull hub master
exec git-update-server-info
php /path/to/directory/file.php
I can't seem to get the PHP to execute. Anyone able to shine any light on this?
exec never returns. Anything you put after the exec call is dead code.
Remove the exec, or place it before your php line if that's the last thing that needs to be done. (And after doing error checking if necessary obviously.)
So for instance
...
git-update-server-info
exec php /path/to/directory/file.php
Or just simply
...
git-update-server-info
php /path/to/directory/file.php
(or move the statements around if your php script can be called before the git command.)
I want to use PHP writing git hooks but have some problem.
I use Windows 7 LAMP packet and git bash.
So, if I run the next script (pre-commit hook) through git shell:
https://gist.github.com/713716
it works fine and there is 123 on the screen. But if I use:
git commit
I have the next error:
error: cannot spawn .git/hooks/pre-commit: No such file or directory
So, what the problem is?
I rarely work on Windows systems but it might have something to do with either making sure the pre-commit file is executable by every user (per Phil's comment), or something to do with git invoking a php command.
Looks like this problem may exist elsewhere but not easily reproduceable.
It's janky, but it might work if you create a new file somewhere with a php extension and invoke that file from the pre-commit hook.
#pre-commit
C:/WebServers/usr/local/php5/php C:/path/to/123.php
#123.php
<?php echo 123 ?>
ok i know this is stupid but atleast im trying :)
$result = shell_exec('C:/cygwin/bin/bash.exe /c --login -i git');
var_dump($result);
somehow i cant get git command from cygwin, anyone ?
Adam Ramadhan
edit*
it should give
usage: git [--version]
[--exec-path[=GIT_EXEC_PATH]]
[--html-path]
[-p|--paginate|--no-pager] [--no-replace-objects]
[--bare] [--git-dir=GIT_DIR]
[--work-tree=GIT_WORK_TREE]
[--help] COMMAND [ARGS]
The most commonly used git commands
are: add Add file contents
to the index bisect Find by
binary search the change that
introduced a bug branch List,
create, or delete branches checkout
Checkout a branch or paths to the
working tree clone Clone a
repository into a new directory
commit Record changes to the
repository diff Show changes
between commits, commit and working
tree, etc fetch Download
objects and refs from another
repository grep Print lines
matching a pattern init
Create an empty git repository or
reinitialize an existing one log
Show commit logs merge Join
two or more development histories
together mv Move or rename
a file, a directory, or a symlink
pull Fetch from and merge with
another repository or a local branch
push Update remote refs along
with associated objects rebase
Forward-port local commits to the
updated upstream head reset
Reset current HEAD to the specified
state rm Remove files from
the working tree and from the index
show Show various types of
objects status Show the working
tree status tag Create,
list, delete or verify a tag object
signed with GPG
See 'git help COMMAND' for more
information on a specific command.
as the value
-i starts an interactive shell, you don't want that
/c does probably not work
use -c command to run a command, you have /c --login
try c:\\cygwin\\bin\\git.exe, or the complete path to git.