Comment on page
Deploy from local development
We can trigger remote deployment over SSH
With the addition of one small package and an additional artisan command we can create a
php artisan build
command which can be run from the local development environment and cause a deployment on the production server over SSHInstall the package https://github.com/spatie/ssh as a development dependency (we will never need this in production).
composer require spatie/ssh --dev
app/Console/Commands/Build.php
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Spatie\Ssh\Ssh;
6
use Illuminate\Console\Command;
7
8
class Build extends Command
9
{
10
protected $signature = 'build';
11
12
protected $description = 'Run the build command on the remote server';
13
14
protected $server = 'gukm1051.siteground.biz';
15
protected $account = 'u1119-ahcvzbcli5de';
16
protected $port = 18765;
17
protected $path = '~/www/mark384.sg-host.com';
18
19
public function handle()
20
{
21
$this->comment('starting build on remote server');
22
$this->warn('---------------------------------------------------');
23
24
$process = Ssh::create($this->account, $this->server, $this->port)
25
->onOutput(function($type, $line){
26
$this->line($line);
27
})
28
->execute([
29
'cd ' . $this->path,
30
'./build.sh'
31
]);
32
33
$this->warn('---------------------------------------------------');
34
35
if($process->isSuccessful()) {
36
$this->comment('Successfully Finished Deployment');
37
$this->newline();
38
return Command::SUCCESS;
39
} else {
40
$this->error('Deployment failed');
41
$this->newline();
42
return Command::FAILURE;
43
}
44
45
}
46
}
47
Add this file into your project then configure lines 14 to 16 for the hosts SSH connection and then add the folder for your remote website on line 17.
Now, after making changes to your project and pushing to your repository, you can locally run
php artisan build
and your site will be deployed with feedback from the remote server on your console.