-1

I am searching for a way to see the history of what echoed in the server. I am using GitHub webhooks and when an auto-deployment occurs a script with echo is executed. This is the code for example:

<?php
try
{
  $payload = json_decode($_REQUEST['payload']);
    echo 'payload is received; ';
}
catch(Exception $e)
{
    echo 'no payload; ';
  exit(0);
}

if ($payload->ref === 'refs/heads/development')
{   echo 'received updated in dev branch; bash/deploy_frontend2dev.sh ';
    exec('sh /var/www/html/bash/deploy_frontend2dev.sh', $output);
    var_dump($output);
}
else 
echo 'smth is called';

I know you can see the history of commands with $ history but when I do that it doesn't show the text that has been echoed, only the commands that have run. Is there anyway to see it? Also if you notice in the code there is a var_dump which is also printed to the screen.

By the way, the exec that happens in the PHP script does that:

cd /var/www/html/ezmob-frontend/;

git pull origin development;
echo "pulling file from dev branch";

rm -R ../dashboard;
echo "dashboard is updated";

unzip ezmob-frontend-built.zip -d ../;

chmod 777 -R dashboard;

This should also echo some text, but yet nothing is shown.

halfer
  • 19,824
  • 17
  • 99
  • 186
shay.k
  • 503
  • 1
  • 7
  • 15
  • 1
    if you're running from command line, you can write to a file like: `php file.php > output.txt`. This will write all output to `output.txt` for you to read (or e-mail yourself) later. You can also do this with the `exec()` part of your script. – ʰᵈˑ Mar 02 '15 at 15:08
  • 1
    I understand that these days writing in all lower-case is considered stylish, in some quarters at least. However, we like questions well-written and easy to read here, so if you could use the Shift key occasionally, it is appreciated - and saves an editor some work. – halfer Mar 02 '15 at 15:23
  • how exactly do i do it? what command should i write and where? – shay.k Mar 02 '15 at 15:24

1 Answers1

0

Setting up the GitHub webhook.

You've already had experience with this, as you've explained it in your question, but...

Set up another script (a listener) to listen to GitHub to push an update onto your script. This listener will then call your other script by exec() which will write to a text file all of the output.

Make GitHub webhook point to the script listener.php.

In listener.php, add the following:

<?php

//Run the file to run the auto-deployment and write all output to your log file.
$logfile = '/var/log/github-deployment/log_'. date('d-m-y_G:i:s') .'.log';
exec('php auto-deployment-is-a-go.php \''. http_build_query(json_decode($_POST['payload'], true)) .'\' > '. $logfile); 

(Make sure you have the directory /var/log/github-deployment/ created and writable - or change the path where the log files are created).

See this question regarding the exec(): How to pass parameters from command line to $_POST in php-script?

The worker

Now that the listener has passed a command to the worker (auto-deployment-is-a-go.php) to do its job, you can not worry about logging, as the exec() command in the listener will write output to the log file.

You will have to change your $payload value to become use parse_str, for example;

<?php
$payload = null;
parse_str($argv[1], $payload);

try {
  if( $payload == null ) {
      throw new Exception("no payload detected");
  } 
} catch(Exception $e) {
   die($e->getMessage());
}

// ...

Notifying you when finished

Generally, you don't want to push to live then scramble through logs. So, have the system e-mail you the output. So in your listener.php script, you would have (after you have called exec());

exec("echo 'Pushed to live. Log files attached' > /tmp/pushtolive.txt");
exec("/bin/mail -s \"Pushed to live!\" -a '. $logfile .' \"email@company.com\" < /tmp/pushtolive.txt");

See this answer for emailing from the command line: https://serverfault.com/a/194388/207143

Community
  • 1
  • 1
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
  • Thanks for the answer. But can't i just use "file_put_contents()" with the output? – shay.k Mar 03 '15 at 08:25
  • And also store the text that meant to be echoed in a variable and the add it to the "file_put_contents()". – shay.k Mar 03 '15 at 08:33
  • You can yeah. I prefer the bash approach though, but whatever best suits your environment and application – ʰᵈˑ Mar 03 '15 at 08:33