0

The setup: Windows Server 2019 webserver with XAMPP, hosting a webapp written in PHP with a MySQL DB. The devs push changes to our company's Git repo.

The issue: For reasons unknown to me, when new code is pushed, it needs to be manually Git Pulled on the webserver, then the new/updated PHP files are moved manually to the respective folders of the webapp. I decided to put into practice the little programming knowledge I've got and automate this process.

The first step was to create a batch file that does Git Pull, logs output in a text file, then using PowerShell, moves the latest files to the destination folders. This work beautifully. However, it requires logging into the server to run the batch file.

The next step is to start the batch file using a page in the webapp, let's say, www.mywebapp.com/git_pull_and_move.php. I've created a Start button on this page clicking on which should execute the batch file. The batch file goes as far as creating the text file to log the output of Git Pull, but the text file is empty. Any clues why this could be happening? Please ask if you need more info.

Snippet of the PHP code:

if(isset($_POST["Start"])){ 

// system("cmd /c C:\\full_path\\to_the_batch_file.bat"); // Doesn't work; Commented out at the moment
exec('C:\\full_path\\to_the_batch_file.bat'); // Doesn't work either

This is what the batch file looks like:

@echo off

cmd /c cd C:\full_path\to_the_local_git_repo

cmd /c git remote remove origin 

cmd /c git remote add origin _removed_for_privacy_reasons_ :D

cmd /c git -C C:\full_path\to_the_local_git_repo pull https://github.com/my_company master >> C:\full_path\to_log_git_pull_output.txt

Sorry for writing a novella!

Enigma
  • 3
  • 1
  • 1
    Your batch file is not doing what you want it to do. It is running a `cd` command in a cmd.exe instance, then closing that instance. Then it is trying to run a single git command in another cmd.exe instance, then closing that instance. Then it is trying to run another git command in another cmd.exe instance, then closing that instance…etc. You should start by removing ```cmd /c ``` from the beginning of every line in which it exists. – Compo Jul 12 '22 at 12:36
  • I'm afraid that did not fix the issue. Thanks for pointing me in a new direction, though. I removed `cmd /c`, then to make sure the same session persists till the end, added this to the beginning of the batch file, `set aVariable=Good day!`, and this to the end, `echo %aVariable%`, and the variable prints without any errors. I've also tried starting every git command with `call git _git_command_` following the suggestion in this post: [link]https://stackoverflow.com/a/5401466/19533197. – Enigma Jul 14 '22 at 03:39

0 Answers0