codenamephp/deploymentchecks.base

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

This package is auto-updated.

Last update: 2024-09-16 10:05:20 UTC


README

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

此软件包是部署检查的基础构建块。它提供了运行检查所需的接口和基本执行逻辑。实际的检查将由其他专业软件包提供。

什么是部署检查?

部署检查、发布后验证或发布后测试(无论您想叫什么,它们基本上是相同的概念)用于验证部署是否成功,应用程序是否按预期运行。

这些步骤通常作为CI/CD管道的一部分执行,是自动化部署/发布周期的重要步骤。

它们可以在服务器本身或构建/部署“服务器”上运行,例如GitHub Workflows,在最终推出之前或之后在生产资源上运行。根据结果,您可以停止推出,执行回滚……或者只是被告知可能存在问题。

安装

最简单的方法是通过composer。只需在您的cli中运行composer require codenamephp/deploymentchecks.base即可为您安装最新版本。

用法

非常基础

最简单的情况是一个包含自动加载器、构建一些检查、执行它们并在退出脚本时使用退出代码的PHP脚本,其余的留给运行检查的构建系统

<?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:///'),
    'Frontpage should be available',
    new StatusCode(200),
))->run();

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

此示例使用http包执行HTTP请求并对其进行测试。它创建一个包含一个测试的单个检查,运行它并检查结果。如果是带有退出代码的结果,则使用退出代码。如果不是,则检查结果是否成功,并使用默认退出代码。

由于大多数CI/CD系统都会运行脚本并检查退出代码,因此这应该已经足够使系统在检查失败时失败。

运行多个检查非常容易

<?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://'),
    'Frontpage should be available',
    new StatusCode(200),
  ),
  new RunTestsOnHttpResponse(
    new Request('GET', 'https:///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));

没有太多变化。唯一的不同之处在于,检查现在被包装在一个将按顺序运行它们的集合中。如果其中一个失败,结果将是不成功的。

同时运行检查也很容易

<?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://'),
    'Frontpage should be available',
    new StatusCode(200),
  ),
  new RunTestsOnHttpResponse(
    new Request('GET', 'https:///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));

再次,没有太多变化。唯一的不同之处在于,检查现在被包装在一个将并行运行它们的集合中。