webhook exec git pull not working when called from the browser - php

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"

Related

Automation git pull doesn't work on server with cron and url

I want to pulling commits from git repo automatically. I have ready server where I can't use ssh and make configuration becouse I don't have permission but I remember login and password in git files.
For automation I want to use cron or place php file with script in public folder. I writed script:
<?php system('echo "Updating";'); ?>
<?php system('cd project && ls -al && pwd && git pull'); ?>
When I run it with php72-cli(it's version of php on server) script.php it's work and pull commits. When I run it in broswer by url myurl.com/script.php it showing text 'Updating' but didn't update. I made script executable by chmod + x.
I thing raeson of that is problem with wrong rights or access but I don't know how to find error and fix it. I can think wrong.

shell_exec('git push origin master') won't work

I want to push a folder with data to an empty repository in atlassian Git stash. The repository is created via the rest api. The remote origin is set. When I enter
$ git push origin master
in the shell it works all fine. When I use the PHP interactive shell and enter
exec('git push origin master');
it also works. But when the code is called in my PHP file nothing happens. Of course the code is the same. I am in the right working directory (I change it with chdir() before the shell_exec). No errors are reported (even when error_reporting is set to E_ALL) and when i set the output argument in the exec() I get an empty array.
Can anyone help?
When you execute code interactively, or commands via the shell, you're executing under your user account and your shell environment.
I would assume that the reason this isn't working is that you're executing the PHP script as another user (and possibly lacking your ssh/git environment config).
More information is needed to better answer your question, but you may want to start by verifying the user you're executing your PHP file as is also able to perform a git push origin master by doing something like:
sudo su -s /bin/bash -c "cd /my/git/repo/dir; git push origin master" MYPHPEXECUTEUSER
I found out that i have to add '2>&1' to the end of a shell command to get an error output when using shell_exec(). It returned "fatal: could not read Password for"url": device not configured". i just had to add the password to the url in a http basic auth fashion. Now it all works fine. Thanks for the answers :)

Unable to run bash commands in php script

I'm trying to use a script to automate some syncing stuff with the website's corresponding github.
The offending code is
<html>
<head><title>github_sync script</title></head>
<body><p>Github sync script working... Although you're probably a github webhook anyways</p><body>
<?php
`mkdir test_dir`;
`cd github_sync/`;
`git clone https://github.com/frczyxw/my-website.git`;
`cd my-website/`;
`rm sixdegrees/`;
`cp -R * ../../`;
?>
</html>
The script is placed in the public_html/beta/directory. I can log into and navigate through the ftp and launch the script by clicking on it fine (the body text displays), but upon rechecking the ftp directory, I find that public_html/beta/test_dir has not appeared, nor does /public_html/beta/github_sync/ have anything in it (I created it manually previous to running the script).
The server should be running php5.3, and I am hosting the website vie Bluehost
OK, so I've solved the problem-
The server was configured correctly- however it turns out that to run all of these commands in sequence and in the same context (i.e. cd into github_sync make a git clone in that folder) the commands need to be executed all at once, like
`cd github_sync/;git clone wsgzsgg; rm -rf...`
Thnaks to everyone who tried to help

Cannot run "git clone" from php on a Minimac

I have a problem executing a git clone on a Minimac 10.8.5.
If I execute the command from a shell, it works.
If I execute the same command from a php file (through a button press on a web page), I get
"error: ssl peer certificate or ssh remote key was not ok while accessing...".
Already executed: git config --global http.sslVerify = false
Already changed the User and Group of the file /etc/apache2/httpd.conf
Tried to execute it with a git clone --verbose
The only error I see is the above one, no more informations.
Where or how can I find a more detailed log ?
What could be the cause ?
This look that the remote requires an authentication either through a ssh key like heroku an github or with an password via https.
In both cases you will need this, you can configure your system first and then try to run the git via php.
I believe that you are doing some type of deploy script right? If so you may want consider use git pull on the after the first deploy.
Try this
env GIT_SSL_NO_VERIFY=true git pull origin master
Edit: solved in another way.
The only way I've found to create a shell with the correct user, is starting the process via ssh. So I do the following and all works as expected.
<?php
$cmd = "ssh user#localhost <cmd to execute>";
shell_exec($cmd);
?>

php exec() - git push origin master not working, but other git commands are working

I used below code :
echo exec("git add . "); //this is working
echo exec("git commit -am 'first commit' "); //also working
echo exec("git push origin master"); //NOT WORKING, also not showing any error .
I chowned folder permissions from user to www-data . So, some git commands are working but
GIT PUSH ORIGIN MASTER
is not working from php exec . What is the solution ?
Also, please tell me why PUSH in exec NOT showing any error or msg, how can i see those msgs .
Also,if possible, please provide me any good links for more advanced use of git commands from php exec .
Update :
I also tried this : I added post-commit hook by creating file .git/hooks/post-commit
I added this code to it :
git push origin master
But I didnt get any msg or error after commiting, it just commited but didnt do any push.
Thanks !
I assume the push command will try to push to a remote repository (i.e. not another folder on your system but a remote server behind SSH/HTTPS).
In this case you are most likely missing a HTTPS client certificate HTTPS or an SSH key. Your webserver (and therefore PHP) most likely runs as a different user and does not have access to the private key. Next to that if the private key needs a password it will not work because the exec command is not an interactive session.
Anyway I heavily recommend you to use some sort of bindings and not calling the binaries. Any direct call will start a new process and this is way more inefficient that calls into a library.
Have you checked which branch your are on?
'git status' will tell you.
I'd also recommend working on a branch and not directly on master.
git branch dev_test
git checkout dev_test
git add .
git commit -a -m "first comment"
git push origin dev_test
git checkout master
git merge dev_test
git push origin master

Categories