0

I know this might sound odd or might probably be a duplicated question but have searched and could not find any possible solutions.

I want to auto-deploy my code from bitbucket to my server. I created an ssh key and linked them, created a bare git folder that the code goes to and then a production folder which carries the live site. I added a webhook to bitbucket that should execute anytime I run a push to my repository. My challenge now is, the script is executed but I don't see any changes, but If I manually write the command in the console, everything works fine. Please help. I'm running a Linode server with a debian 9 OS. This is the code from my deployment script. Please help me. I have on this for 4 days now.

<?php
$repo_dir = '/var/www/git/_ku1bo__c_plus_z_.git';
$web_root_dir = '/var/www/html/kobuplus.com/public_html';
$git_path = '/usr/bin/git';
exec("cd $repo_dir" && "$git_path fetch");
exec("cd $repo_dir" && "GIT_WORK_TREE=$web_root_dir $git_path checkout -f");
file_put_contents("deploy.log", date("m/d/Y h:i:s a") . "deployed latest branch" . "\n", FILE_APPEND);
?>
Atoms
  • 11
  • 8
  • Does your webserver has permissions? – Vince Verhoeven Nov 20 '17 at 21:21
  • 2
    You say `shell_exec` in your question, and use `exec` in your example. Did you know there is a [difference](https://stackoverflow.com/questions/20072696/what-is-different-between-exec-shell-exec-system-and-passthru-functions) and it mainly effects the output handling – Dan Nov 20 '17 at 21:22
  • exec, shell_exec, system. all methods of calling commands, with different return values and parameters. – Nic3500 Nov 20 '17 at 21:37
  • 1
    And you have a weird syntax here. `exec("command1" && "command2");`. This means that php will do `"command1" && "command2"`, so TRUE && TRUE, which === TRUE. So it will do `exec(TRUE)` ?!? Makes no sense. If you want to use && like in a bash shell, do `exec("command1 && command2");` Exec takes 1 string, which is executed on the system. – Nic3500 Nov 20 '17 at 21:39
  • Yes, my webserver have permissions. I have used both `exec` and `shell_exec` but still not working. – Atoms Nov 20 '17 at 22:33
  • I have adjusted my code to: `` But all to no avail. O don't know what to do anymore. Please help – Atoms Nov 20 '17 at 22:53

2 Answers2

0

Your joining your string incorrectly, try this:

<?php
$repo_dir = '/var/www/git/_ku1bo__c_plus_z_.git';
$web_root_dir = '/var/www/html/kobuplus.com/public_html';
$git_path = '/usr/bin/git';
exec("cd " . $repo_dir . $git_path . " fetch");
exec("cd $repo_dir GIT_WORK_TREE=$web_root_dir $git_path checkout -f");
file_put_contents("deploy.log", date("m/d/Y h:i:s a") . "deployed latest branch" . "\n", FILE_APPEND);
?>
Niellles
  • 868
  • 10
  • 27
  • That did not work. The fetch word on line 5 is not in quotes. It will flag error on running the code that way. – Atoms Nov 20 '17 at 23:44
  • @Atoms Fixed that. But probably still won't work, you're getting commands like: `cd /var/www/git/_ku1bo__c_plus_z_.git/var/www/html/kobuplus.com/public_html` now. What did you originally mean with the `&&`? And which commands are you trying to run in plain text. – Niellles Nov 21 '17 at 11:05
  • It's a script that I added to bitbucket webhook to automate deployment when I run a push command. So I believe running the command that way using the 'exec()' command will make it run at the terminal. I think my code is correct, i'm actually thinking the issue is coming from somewhere else. Probably permissions. I'm will look into that and try again. – Atoms Nov 21 '17 at 11:11
0

-> Run the file using this command to see how it behaves (using the SSH command line interface):

php absolute_path_of_the_file 

-> If you get this error: Permission denied (publickey), then make sure you have added the SSH key to the repository settings.

Yannis
  • 1,682
  • 7
  • 27
  • 45