codenamephp/deploymentchecks.base

1.0.0-beta.1 2023-04-23 11:30 UTC

This package is auto-updated.

Last update: 2024-04-16 09:10:48 UTC


README

Packagist Version Packagist PHP Version Support Lines of code GitHub code size in bytes CI Packagist Downloads GitHub

This package is the base building block for deployment checks. It provides the interfaces and basic execution logic needed to run checks. The checks itself will be provided by other specialized packages.

What are deployment checks?

Deployment checks or Post-Deploy Verification or Post-Release Tests (whatever you want to call them, they are more or less the same concept) are used to verify that a deployment was successful and the application is running is expected.

These steps are usually performed as part of the CI/CD pipeline and are an important step when automating the deployment/release cycle.

They can either be run on the server itself or on a build/deployment "server" like Github Workflows either before the final rollout or after on the production resources. Depending on the outcome you can stop the rollout, perform a rollback ... or just be notified that something may be wrong.

Installation

Easiest way is via composer. Just run composer require codenamephp/deploymentchecks.base in your cli which should install the latest version for you.

Usage

The very basics

The simplest case ist just PHP script that includes the autoloader, builds some checks, executes them and ends the script with an exit code and leaves the rest to the build system that was running the checks:

<?php declare(strict_types=1);

use de\codenamephp\deploymentchecks\base\Check\Result\WithExitCodeInterface;
use de\codenamephp\deploymentchecks\base\ExitCode\DefaultExitCodes;
use de\codenamephp\deploymentchecks\http\RunTestsOnHttpResponse;
use de\codenamephp\deploymentchecks\http\Test\StatusCode;

require_once __DIR__ . '/../vendor/autoload.php';

$result = (new RunTestsOnHttpResponse(
    new Request('GET', 'https://localhost/'),
    'Frontpage should be available',
    new StatusCode(200),
))->run();

exit($result instanceof WithExitCodeInterface ? $result->exitCode() : ($result->successful() ? DefaultExitCodes::SUCCESSFUL->value : DefaultExitCodes::ERROR->value));

This example uses the http package to perform http requests and run tests on them. It creates a single check with one test, runs it and checks the result. If it is a result with an exit code, the exit code is used. If not, the result is checked for success and the default exit codes are used.

Since most CI/CD systems will run the script and check the exit code, this should already be enough to get the system to fail if one of the checks fail.

It's very easy to run multiple checks:

<?php declare(strict_types=1);

use de\codenamephp\deploymentchecks\base\Check\Collection\SequentialCheckCollection;
use de\codenamephp\deploymentchecks\base\Check\Result\WithExitCodeInterface;
use de\codenamephp\deploymentchecks\base\ExitCode\DefaultExitCodes;
use de\codenamephp\deploymentchecks\http\RunTestsOnHttpResponse;
use de\codenamephp\deploymentchecks\http\Test\StatusCode;

require_once __DIR__ . '/../vendor/autoload.php';

$result = (new SequentialCheckCollection(
  new RunTestsOnHttpResponse(
    new Request('GET', 'https://localhost'),
    'Frontpage should be available',
    new StatusCode(200),
  ),
  new RunTestsOnHttpResponse(
    new Request('GET', 'https://localhost/admin'),
    'Admin login page should be available',
    new StatusCode(401),
  )
))->run();

exit($result instanceof WithExitCodeInterface ? $result->exitCode() : ($result->successful() ? DefaultExitCodes::SUCCESSFUL->value : DefaultExitCodes::ERROR->value));

Not much has changed. The only difference is that the checks are now wrapped in a collection that will run them sequentially. If one fails, the result will be unsuccessful.

It's also very easy to run checks in parallel. The next example uses the async package to run the checks in parallel:

<?php declare(strict_types=1);

use de\codenamephp\deploymentchecks\async\Collection\AsyncCheckCollection;
use de\codenamephp\deploymentchecks\base\Check\Result\WithExitCodeInterface;
use de\codenamephp\deploymentchecks\base\ExitCode\DefaultExitCodes;
use de\codenamephp\deploymentchecks\http\RunTestsOnHttpResponse;
use de\codenamephp\deploymentchecks\http\Test\StatusCode;
use GuzzleHttp\Psr7\Request;

require_once __DIR__ . '/../vendor/autoload.php';

$result = (new AsyncCheckCollection(new \Spatie\Async\Pool(),
  new RunTestsOnHttpResponse(
    new Request('GET', 'https://localhost'),
    'Frontpage should be available',
    new StatusCode(200),
  ),
  new RunTestsOnHttpResponse(
    new Request('GET', 'https://localhost/admin'),
    'Admin login page should be available',
    new StatusCode(401),
  )
))->run();

exit($result instanceof WithExitCodeInterface ? $result->exitCode() : ($result->successful() ? DefaultExitCodes::SUCCESSFUL->value : DefaultExitCodes::ERROR->value));

Again, not much has changed. The only difference is that the checks are now wrapped in a collection that will run them in parallel.