|
8 | 8 |  |
9 | 9 |  |
10 | 10 |
|
| 11 | +This package is the base building block for deployment checks. It provides the interfaces and basic execution logic needed to run checks. |
| 12 | +The checks itself will be provided by other specialized packages. |
| 13 | + |
| 14 | +## What are deployment checks? |
| 15 | + |
| 16 | +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 |
| 17 | +to verify that a deployment was successful and the application is running is expected. |
| 18 | + |
| 19 | +These steps are usually performed as part of the CI/CD pipeline and are an important step when automating the deployment/release cycle. |
| 20 | + |
| 21 | +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 |
| 22 | +resources. |
| 23 | +Depending on the outcome you can stop the rollout, perform a rollback ... or just be notified that something may be wrong. |
| 24 | + |
11 | 25 | ## Installation |
12 | 26 |
|
13 | 27 | Easiest way is via composer. Just run `composer require codenamephp/deploymentchecks.base` in your cli which should |
14 | 28 | install the latest version for you. |
15 | 29 |
|
16 | | -## Usage |
| 30 | +## Usage |
| 31 | + |
| 32 | +### The very basics |
| 33 | + |
| 34 | +The simplest case ist just PHP script that includes the autoloader, builds some checks, executes them and ends the script with an exit |
| 35 | +code and leaves the rest to the build system that was running the checks: |
| 36 | + |
| 37 | +```php |
| 38 | +<?php declare(strict_types=1); |
| 39 | + |
| 40 | +use de\codenamephp\deploymentchecks\async\Collection\AsyncCheckCollection; |
| 41 | +use de\codenamephp\deploymentchecks\base\Check\Result\WithExitCodeInterface; |
| 42 | +use de\codenamephp\deploymentchecks\base\ExitCode\DefaultExitCodes; |
| 43 | +use de\codenamephp\deploymentchecks\http\Check\HttpCheck; |
| 44 | +use de\codenamephp\deploymentchecks\http\Check\Test\StatusCode; |
| 45 | +use GuzzleHttp\Psr7\Request; |
| 46 | + |
| 47 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 48 | + |
| 49 | +$result = (new AsyncCheckCollection(new \Spatie\Async\Pool(), |
| 50 | + new HttpCheck( |
| 51 | + new Request('GET', 'https://localhost'), |
| 52 | + 'Frontpage should be available', |
| 53 | + new StatusCode(200), |
| 54 | + ), |
| 55 | + new HttpCheck( |
| 56 | + new Request('GET', 'https://localhost/admin'), |
| 57 | + 'Admin login page should be available', |
| 58 | + new StatusCode(401), |
| 59 | + ) |
| 60 | +))->run(); |
| 61 | + |
| 62 | +exit($result instanceof WithExitCodeInterface ? $result->exitCode() : ($result->successful() ? DefaultExitCodes::SUCCESSFUL->value : DefaultExitCodes::ERROR->value)); |
| 63 | +``` |
| 64 | + |
| 65 | +This example uses the http package to perform http requests and run tests on them together with the async package to the requests are tested in parallel. |
| 66 | +At the end the result is checked. If it is a result with an exit code, the exit code is used. If not, the result is checked for success and |
| 67 | +the default exit codes are used. |
| 68 | + |
| 69 | +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. |
0 commit comments