Laravel 4.1's New Remote Component

Posted by Adam Engebretson on November 26, 2013

What's new in Laravel 4.1? Well, TONS of new and exciting features (as well as an up-and-coming surprise from Taylor Otwell). However, taking the cake is Laravel's new Remote Component. Let's take a look!

We all know that deployment is a yet-to-be-perfected task in everyone's workflow. I have posted about deploying to Heroku, which is a viable option for simpler projects. However, I've come across issues with that workflow. My new workflow (obviously, it will depend on the project) however, utilizes Laravel 4.1's new Remote Component.

I've made a simple Artisan Command:

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class DeployCommand extends Command {

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'deploy';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Deploy code on server.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function fire()
    {
        $me = $this;
        $remote = $this->argument('remote');
        $config = app()->config['remote.connections.'.$remote];

        $commands = [
            'cd '.$config['root'],
            'git checkout -f',
            'git pull -f',
            'php artisan cache:clear',
        ];

        if($this->option('migrate'))
            $commands[] = 'php artisan migrate';

        if($this->option('composer'))
            $commands[] = 'composer install --no-dev';

        SSH::into($remote)->run(

            $commands,

            function($line) use ($me) {
                $me->info($line);
            }
        );

        $this->info('All done!');
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return array(
            array('remote', InputArgument::OPTIONAL, 'Define which remote to connect to.', 'production'),
        );
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return array(
            array('composer', null, InputOption::VALUE_NONE, 'Execute composer install.', null),
            array('migrate', null, InputOption::VALUE_NONE, 'Execute php artisan migrate.', null),
        );
    }
}

And, of course, I add Artisan::add(new DeployCommand); to my /app/start/artisan.php file.

This does a couple things, however its main task is to execute the following tasks on my remote server:

  • cd /var/www
  • git checkout -f
  • git pull -f
  • php artisan cache:clear
  • php artisan migrate (sometimes)
  • composer install (sometimes)

These commands effectively deploy my application to my production server!

To learn more about the Remote Component, check out Jeffrey Way's Laracast on the topic, as well as his Commands 101 lesson. Also, after installing 4.1 (composer create-project laravel/laravel:dev-master), you can run php artisan changes to see a list of new features that come with 4.1!