This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from dbpolito/fwd_tools
Deploy to fwd.tools
- Loading branch information
Showing
6 changed files
with
1,283 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Commands; | ||
|
||
use App\Tasks\Deploy as DeployTask; | ||
|
||
class Deploy extends Command | ||
{ | ||
/** | ||
* The signature of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'deploy'; | ||
|
||
/** | ||
* The description of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Deploy your app to fwd.tools.'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
return DeployTask::make($this)->run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace App\Tasks; | ||
|
||
use App\Builder\Builder; | ||
use App\Environment; | ||
use Illuminate\Http\Client\RequestException; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Support\Str; | ||
|
||
class Deploy extends Task | ||
{ | ||
const URL = 'https://fwd.tools'; | ||
|
||
protected $file; | ||
protected $deploy; | ||
|
||
public function run(...$args): int | ||
{ | ||
try { | ||
$commands = [ | ||
[$this, 'createReleaseFile'], | ||
[$this, 'sendReleaseFile'], | ||
[$this, 'building'], | ||
]; | ||
|
||
if ($exitCode = $this->runCallables($commands)) { | ||
return $exitCode; | ||
} | ||
|
||
$this->command->info('Access URL: ' . $this->deploy->url); | ||
|
||
return 0; | ||
} catch (RequestException $exception) { | ||
$this->command->error('Deploy failed'); | ||
|
||
if ($exception->response->status() === 422) { | ||
$this->command->error( | ||
collect($exception->response->object()->errors)->flatten()->implode(PHP_EOL) | ||
); | ||
} | ||
|
||
return 1; | ||
} catch (\Exception $exception) { | ||
$this->command->error('Deploy failed'); | ||
$this->command->error($exception->getMessage()); | ||
|
||
return 1; | ||
} finally { | ||
$environment = resolve(Environment::class); | ||
File::delete($environment->getContextFile($this->file)); | ||
} | ||
} | ||
|
||
public function createReleaseFile() : int | ||
{ | ||
return $this->runTask('Create Release File', function () { | ||
$this->file = sprintf('%s.tgz', Str::random(10)); | ||
|
||
$this->runCommandWithoutOutput( | ||
Builder::make('git', 'archive', '--format=tar.gz', 'HEAD', '-o', $this->file) | ||
); | ||
|
||
return 0; | ||
}); | ||
} | ||
|
||
public function sendReleaseFile() : int | ||
{ | ||
return $this->runTask('Send Release File', function () { | ||
$environment = resolve(Environment::class); | ||
$file = fopen($environment->getContextFile($this->file), 'r'); | ||
$data = ['slug' => env('FWD_TOOLS_SLUG')]; | ||
|
||
$this->deploy = $this->http() | ||
->attach('deploy', $file, 'deploy.tgz') | ||
->post(self::URL . '/api/deploy/create', $data) | ||
->throw() | ||
->object(); | ||
|
||
return 0; | ||
}); | ||
} | ||
|
||
public function building() : int | ||
{ | ||
return $this->runTask('Building', function () { | ||
return $this->runCallableWaitFor(function () { | ||
$this->deploy = $this->http() | ||
->get(self::URL . sprintf('/api/deploy/%s/status', $this->deploy->id)) | ||
->throw() | ||
->object(); | ||
|
||
if ($this->deploy->status === 'failed') { | ||
throw new \Exception('Failed'); | ||
} | ||
|
||
return $this->deploy->status === 'success' ? 0 : 1; | ||
}, 600); | ||
}); | ||
} | ||
|
||
protected function http() | ||
{ | ||
return Http::withToken(env('FWD_TOOLS_TOKEN')) | ||
->withHeaders(['Accept' => 'application/json']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.