Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Fix Code Style

on: [push]

jobs:
lint:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php: [8.4]

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: json, dom, curl, libxml, mbstring
coverage: none

- name: Install Pint
run: composer global require laravel/pint

- name: Run Pint
run: pint

- name: Commit linted files
uses: stefanzweifel/git-auto-commit-action@v5
37 changes: 37 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: PHPStan

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
phpstan:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
coverage: none
tools: composer

- name: Get Composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Setup cache
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run:
composer install --prefer-dist --no-suggest --no-progress

- name: Run PHPStan
run: ./vendor/bin/phpstan analyse
12 changes: 5 additions & 7 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [7.4, 8.0]
laravel: [8.*, 7.*]
php: [8.3, 8.4]
laravel: [11.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
- laravel: 11.*
testbench: 9.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

Expand All @@ -28,7 +26,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv
coverage: none

- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ The purpose of this package is to introduce local zero-downtime deployments into

## Requirements

* Laravel 7 | 8
* PHP ^7.4 | ^8.0
* Laravel >= 11.x+
* PHP >= 8.3

## Installation

Expand Down
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
}
],
"require": {
"php": "^7.4|^8.0"
"php": "^8.3"
},
"require-dev": {
"laravel/framework": "^8.12",
"mockery/mockery": "^1.4",
"phpunit/phpunit": "^9.3.3",
"orchestra/testbench": "^6.9"
"laravel/framework": "^11.0",
"mockery/mockery": "^1.6.10",
"phpunit/phpunit": "^10.5.35|^11.3.6",
"orchestra/testbench": "^9.0",
"laravel/pint": "^1.20",
"phpstan/phpstan": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:

paths:
- src

# The level 9 is the highest level
level: 5

universalObjectCratesClasses:
- Illuminate\Http\Resources\Json\JsonResource
46 changes: 21 additions & 25 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Integration">
<directory suffix="Test.php">./tests/Integration</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_DATABASE" value="sqlite"/>

<env name="ATM_DEPLOYMENT_LINK" value="deployment-link"/>
<env name="ATM_BUILD" value="build"/>
<env name="ATM_DEPLOYMENTS" value="deployments"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_DATABASE" value="sqlite"/>
<env name="ATM_DEPLOYMENT_LINK" value="deployment-link"/>
<env name="ATM_BUILD" value="build"/>
<env name="ATM_DEPLOYMENTS" value="deployments"/>
</php>
<source>
<include>
<directory suffix=".php">./app</directory>
</include>
</source>
</phpunit>
6 changes: 3 additions & 3 deletions src/AtomicDeploymentsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

class AtomicDeploymentsServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}

public function register()
public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/../config/atomic-deployments.php', 'atomic-deployments');
$this->registerPublishables();
Expand All @@ -24,7 +24,7 @@ public function register()
$this->app->bind(DeploymentInterface::class, config('atomic-deployments.deployment-class'));

$this->app->bind(AtomicDeploymentService::class, function ($app, $params) {
if (empty($params) || (count($params) && !is_a($params[0], DeploymentInterface::class))) {
if (empty($params) || (count($params) && ! is_a($params[0], DeploymentInterface::class))) {
array_unshift($params, $app->make(DeploymentInterface::class));
}

Expand Down
100 changes: 59 additions & 41 deletions src/Commands/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,82 @@
use JTMcC\AtomicDeployments\Services\AtomicDeploymentService;
use JTMcC\AtomicDeployments\Services\Deployment;
use JTMcC\AtomicDeployments\Services\Output;
use Throwable;

class DeployCommand extends BaseCommand
{
protected $signature = 'atomic-deployments:deploy
{--hash= : Specify a previous deployments commit hash/deploy-dir to deploy }
{--directory= : Define your deploy folder name. Defaults to current HEAD hash }
{--dry-run : Test and log deployment steps }';
protected $signature = 'atomic-deployments:deploy
{--hash= : Specify a previous deployments commit hash/deploy-dir to deploy }
{--directory= : Define your deploy folder name. Defaults to current HEAD hash }
{--dry-run : Test and log deployment steps }';

protected $description = 'Deploy a clone of your latest build and attach symlink';

public function handle()
public function handle(): void
{
Output::alert('Running Atomic Deployment');

$migrate = config('atomic-deployments.migrate', []);
$dryRun = $this->option('dry-run');

if ($hash = $this->option('hash')) {
Output::info("Updating symlink to previous build: {$hash}");

$deploymentModel = AtomicDeployment::successful()->where('commit_hash', $hash)->first();

if (!$deploymentModel || !$deploymentModel->hasDeployment) {
Output::warn("Build not found for hash: {$hash}");
} else {
$atomicDeployment = AtomicDeploymentService::create(
new Deployment($deploymentModel),
$migrate,
$dryRun
);

try {
$atomicDeployment->getDeployment()->link();
$atomicDeployment->confirmSymbolicLink();
DeploymentSuccessful::dispatch($atomicDeployment, $deploymentModel);
} catch (\Throwable $e) {
$atomicDeployment->fail();
Output::throwable($e);
}
}
$this->deployPreviousBuild($hash, $migrate, $dryRun);
} else {
$atomicDeployment = AtomicDeploymentService::create($migrate, $dryRun);

Output::info('Running Deployment...');

try {
if ($deployDir = trim($this->option('directory'))) {
Output::info("Deployment directory option set - Deployment will use directory: {$deployDir} ");
$atomicDeployment->getDeployment()->setDirectory($deployDir);
}
$atomicDeployment->deploy(fn () => $atomicDeployment->cleanBuilds(config('atomic-deployments.build-limit')));
} catch (\Throwable $e) {
$atomicDeployment->fail();
Output::throwable($e);
}
$this->deployCurrentBuild($migrate, $dryRun);
}

Output::info('Finished');
ConsoleOutput::line('');
}

private function deployPreviousBuild(string $hash, array $migrate, bool $dryRun): void
{
Output::info("Updating symlink to previous build: {$hash}");

/** @var null|AtomicDeployment $deploymentModel */
$deploymentModel = AtomicDeployment::successful()->where('commit_hash', $hash)->first();

if (! $deploymentModel?->has_deployment) {
Output::warn("Build not found for hash: {$hash}");

return;
}

$atomicDeployment = AtomicDeploymentService::create(
new Deployment($deploymentModel),
$migrate,
$dryRun
);

try {
$atomicDeployment->getDeployment()->link();
$atomicDeployment->confirmSymbolicLink();
DeploymentSuccessful::dispatch($atomicDeployment, $deploymentModel);
} catch (Throwable $e) {
$atomicDeployment->fail();
Output::throwable($e);
}
}

private function deployCurrentBuild(array $migrate, bool $dryRun): void
{
$atomicDeployment = AtomicDeploymentService::create($migrate, $dryRun);

Output::info('Running Deployment...');

try {
if ($deployDir = trim($this->option('directory'))) {
Output::info("Deployment directory option set - Deployment will use directory: {$deployDir}");
$atomicDeployment->getDeployment()->setDirectory($deployDir);
}

$atomicDeployment
->deploy(
fn () => $atomicDeployment->cleanBuilds(config('atomic-deployments.build-limit'))
);
} catch (Throwable $e) {
$atomicDeployment->fail();
Output::throwable($e);
}
}
}
37 changes: 20 additions & 17 deletions src/Commands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,44 @@

use JTMcC\AtomicDeployments\Helpers\ConsoleOutput;
use JTMcC\AtomicDeployments\Models\AtomicDeployment;
use JTMcC\AtomicDeployments\Models\Enums\DeploymentStatus;

class ListCommand extends BaseCommand
{
protected $signature = 'atomic-deployments:list';

protected $description = 'List currently available deployments';

public function handle()
public function handle(): void
{
ConsoleOutput::line('');
ConsoleOutput::alert('Available Deployments');

$deployments = AtomicDeployment::select(
'id',
'commit_hash',
'deployment_path',
'deployment_link',
'deployment_status',
'created_at',
)->get()->map(function ($deployment) {
$deployment->append('isCurrentlyDeployed');
$deployment->deployment_status = DeploymentStatus::getNameFromValue($deployment->deployment_status);

return $deployment;
});

if (!$deployments->count()) {
$deployments = AtomicDeployment::query()
->select([
'id',
'commit_hash',
'deployment_path',
'deployment_link',
'deployment_status',
'created_at',
])
->get()
// @phpstan-ignore-next-line
->map(function (AtomicDeployment $deployment) {
$deployment->append('is_currently_deployed');

return $deployment;
});

if (! $deployments->count()) {
ConsoleOutput::info('No deployments found');

return;
}

$titles = ['ID', 'Commit Hash', 'Path', 'SymLink', 'Status', 'Created', 'Live'];

// @phpstan-ignore-next-line
ConsoleOutput::table($titles, $deployments);
ConsoleOutput::line('');
}
Expand Down
Loading