Protone Media logo

Introducing Laravel Task Runner: Write scripts like Blade Components and run them anywhere

After all the Splade announcements and developments of the past months, today marks the release of a new Laravel package! The latest and greatest version 10 of the Laravel framework comes with a brand new Process API - a wrapper around the famous Symfony component, which simplifies the execution of external processes:

$directoryContents = Process::run('ls -la')->output();

While the Laravel ecosystem has a package called Envoy that makes it easy to use the Blade template engine to define and run tasks, Laravel Task Runner takes a different approach. This new package allows you to write tasks as if they were Blade Components, providing a dedicated class and template view. It is built on top of the new Process API, enabling you to run tasks directly from your code without installing a separate binary like Envoy. Plus, it supports running tasks locally and on remote servers through an SSH connection.

Let's take a look at a simplified deploy script. With the new package, you can create a task with the Artisan CLI:

php artisan make:task DeployApp

This command generates a DeployApp.php class in the app/Tasks directory and a deploy-app.blade.php template file in the resources/views/tasks directory. Just like Blade components, public properties and methods are accessible in the template. For instance, you can make the repository branch and migration options variable:

use ProtoneMedia\LaravelTaskRunner\Task;

class DeployApp extends Task
{
    public function __construct(public string $branch) { }

    public function databaseConnection()
    {
        return 'mysql';
    }
}

And then use them in the template:

cd /var/www/html
git pull origin {{ $branch }}
php artisan migrate --database={{ $databaseConnection() }}

Tasks can easily be dispatched:

DeployApp::dispatch();

If you want to pass constructor parameters, you may use the static make() method:

DeployApp::make('dev-new-feature')->dispatch();

In addition to running tasks locally, you can also dispatch them on a remote server and run them in the background. You can configure one or more remote connections in the config file.

DeployApp::inBackgronud()->onConnection('web')->dispatch();

Just like Laravel’s Process implementation, you can easily fake tasks and make assertions in your test suite:

TaskRunner::fake();

$this->post('/api/composer/global-update');

TaskRunner::assertDispatched(ComposerGlobalUpdate::class);

Here's another example of faking task output:

TaskRunner::fake([
    ComposerGlobalUpdate::class => 'Updating dependencies'
]);

$this->post('/api/composer/global-update');

TaskRunner::assertDispatched(ComposerGlobalUpdate::class, function (PendingTask $task) {
    return $task->shouldRunInBackground() 
        && $task->shouldRunOnConnection('production');
});

The package is fully open-source and available at Github, where you'll also find the full documentation.

Related posts

Want to stay up-to-date on Laravel news and packages?

Pascal Baljet on Twitter

Follow me on Twitter: @pascalbaljet

Pascal Baljet on Twitter

Subscribe to my YouTube channel

© 2013-2024 Protone Media B.V. Hosted with Eddy Server Management.