rocketeers / rocketeer

Send your projects up in the clouds
http://rocketeer.autopergamene.eu/
MIT License
2.66k stars 217 forks source link

runForCurrentRelease return code #731

Open powturns opened 7 years ago

powturns commented 7 years ago

When using a custom event to run a binary, it would be nice to be able to retrieve the return code to check if the command ran successfully. In the below example, I want to run gulp, but if the binary fails to run, the deployment continues, leaving the site in a broken state.

<?php

use Rocketeer\Facades\Rocketeer;

Rocketeer::listenTo('dependencies.after', function($task) {
    /** @var \Rocketeer\Tasks\Closure $task  */

    $r = $task
        ->binary('node_modules/gulp/bin/gulp.js')
        ->runForCurrentRelease('build --production');

    if (!$r) {
        return $this->halt('Error running command');
    }
});

Anyone know any workarounds for this? Thanks!

xk4r00t commented 7 years ago

Hi @51systems, I started to use rocketeers few days ago and I was faced to the same issue. I made a dirty workaround to check my command returned code :

$lastCmdRegex = '/last_cmd_returned_value=:(\d+):/';
$r = $task->runForCurrentRelease(["node_modules/.bin/gulp --production && echo \"last_cmd_returned_value=:$?:\""]);
$match = [];
if(is_string($r)) {
    preg_match($lastCmdRegex, $r, $match);
}
if ( !$task->command->option('pretend') && (!isset($match[1]) || (isset($match[1]) && (int) $match[1] != 0)) ) {
    return $task->halt('Build assets failed, cancelling deployment (trace: ' . json_encode(['returned_value_match' => $match, 'returned_value' => $r]) . ')');
}

I hope this will help you,

Regards