nlmenke/deploy-version

通过部署自动更新应用程序版本。

1.2.0 2018-08-26 02:55 UTC

This package is auto-updated.

Last update: 2024-09-18 18:33:39 UTC


README

通过部署自动更新应用程序版本。

Total Downloads Latest Stable Version Latest Unstable Version License

此包的工作方式与迁移类似,您需要为每个特性生成一个部署文件。然后,您可以一次性部署所有新特性。

安装

此包可以通过Composer安装

composer require nlmenke/deploy-version

在Laravel 5.5及以上版本中,包将自动注册服务提供者。在Laravel 5.4中,您必须手动安装服务提供者

// config/app.php
'providers' => [
    ...
    NLMenke\DeployVersion\DeployVersionServiceProvider::class,
    ...
];

在Laravel 5.5及以上版本中,包将自动注册外观。在Laravel 5.4中,您必须手动安装外观

// config/app.php
'aliases' => [
    ...
    NLMenke\DeployVersion\DeployVersionFacade::class
    ...
];

可选地,您可以发布包的配置文件

php artisan vendor:publish --provider="NLMenke\DeployVersion\DeployVersionServiceProvider"

以下配置文件将发布到config/deploy-version.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Starting Version
    |--------------------------------------------------------------------------
    |
    | This value determines the starting version of the application. If the
    | project is already version 3.0.0, set that here so we can continue
    | from there. This will only be used when doing the first deploy.
    |
    */

    'starting_version' => '0.0.0-dev',

    /*
    |--------------------------------------------------------------------------
    | Deployment Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the deployments that have already run for
    | your application. Using this information, we can determine which of
    | the deployments on disk haven't been run in the application yet.
    |
    */

    'table' => 'deployments',

    /*
    |--------------------------------------------------------------------------
    | Maintenance Mode
    |--------------------------------------------------------------------------
    |
    | This value determines whether your deployments should employ the artisan
    | down command (`php artisan down`). This value can either be a boolean
    | or a string. String values are used as the message for the command.
    |
    */

    'maintenance_mode' => 'We are currently down for maintenance and should be back shortly. We apologise for any inconvenience.',

    /*
    |--------------------------------------------------------------------------
    | Deployment Commands
    |--------------------------------------------------------------------------
    |
    | This array determines commands that should be performed during each
    | deployment cycle; after all deployment files have been executed.
    | Feature-specific commands should be added to that deployment.
    |
    */

    'commands' => [
        'git reset --hard HEAD',
        'git pull',
        'composer install',
        'yarn',
        'npm run ' . (config('env') === 'production' ? 'production' : 'development'),
    ],

];

生成部署

要创建部署,请使用make:deployment命令

php artisan make:deployment initial_deployment

新部署将被放置在项目基本目录下的新目录(deployments)中。每个部署都包含一个时间戳,这允许Laravel确定部署的顺序。

--major--minor--patch选项也可以用来指示发布类型。如果没有传递任何选项,则假定部署将是补丁。每个部署只能使用一种发布类型:主发布将覆盖次要和补丁选项,而次要发布将覆盖补丁。

--pre选项将确定部署是否是预发布(alpha、beta、rc等)

php artisan make:deployment initial_deployment --major --pre=alpha.1

如果部署应包含迁移,请添加--migration选项。如果在部署时多个部署包含迁移选项,则迁移命令将在部署周期结束时只运行一次。

部署结构

部署类将包含一些变量和一个可选的deploy函数。如果没有为make:deployment命令提供任何选项,将创建一个基本的部署类

<?php

use NLMenke\DeployVersion\Deployments\Deployment;

class SomePatch extends Deployment
{
    /**
     * Patch versions include backwards-compatible bug fixes.
     *
     * Patch version Z (x.y.Z | x > 0) MUST be incremented if only backwards compatible bug fixes
     * are introduced. A bug fix is defined as an internal change that fixes incorrect behavior.
     *
     * A true value will result in the patch version being increased while major and minor versions
     * will remain unchanged (e.g.: 1.2.3 -> 1.2.4). We'll assume all deployments are a patch
     * unless stated otherwise.
     *
     * @var bool
     */
    protected $patch = true;

    /**
     * Release notes for the deployment.
     *
     * @var array
     */
    protected $releaseNotes = [
        // changelog
    ];

    /**
     * Additional deployment functionality.
     *
     * @return void
     */
    public function deploy()
    {
        // do other deployment stuff
    }
}

注意

补丁变量将始终包含在新部署类中,如果删除则默认为true。如果您的部署将是仅预发布更新,则将值设置为false并包含$preRelease变量。

如果单个部署期间需要任何其他功能,例如运行一个种子或发送电子邮件通知用户有关更新更改,请将其添加到deploy方法中;应在所有部署结束时运行的任何命令都应添加到配置文件中的commands选项。

注意

迁移将在部署周期内仅运行一次,并在第一个待处理部署类的deploy方法之前运行,因此如果您的部署需要首先运行迁移,它们应按预期工作。

配置命令将在所有部署完成后运行。

基本上,发布说明变量可以按照您希望的任何方式构建,只要它仍然是核心数组。此变量在插入数据库之前将进行JSON编码。数据将以多维数组的形式返回 - 之后的任何格式化都需要由您的应用程序处理。

运行部署

要运行所有挂起的部署,请执行deploy Artisan命令

php artisan deploy

注意

由于这是一个Composer包,因此必须在第一次部署之前运行composer install

如果您使用的是maintenance_mode功能(它调用down Artisan命令),则可以包含--message选项来更改默认的维护信息

php artisan deploy --message="Patience! For the Jedi it is time to eat as well."

注意

为了显示此消息,您需要更新(或创建,如果尚未存在)resources/views/errors/503.blade.php 并在需要显示消息的位置包含 $exception->getMessage()

强制在生产环境中运行部署

部署是单向的 - 它没有回滚选项。为了保护您免在生产应用程序上运行部署,在执行命令之前,您将被提示确认。要强制命令在没有提示的情况下运行,请使用 --force-f)标志。

php artisan deploy --force

检索当前版本

要获取最新版本,您需要使用 DeployVersion 门面

DeployVersion::version();

// 2.1.0-dev

它们可以通过传递长度参数或手动调用方法(默认情况下,version 调用 release 方法)返回不同的长度

DeployVersion::release();

// 2.1.0-dev

DeployVersion::version('short');
DeployVersion::short();

// v2.1.0-alpha+8752f75

DeployVersion::version('long');
DeployVersion::long();

// Version 2.1.0-dev <span>(build 8752f75)</span>

版本说明

检索版本说明也将使用 DeployVersion 门面。该方法将返回一个数组,其中包含版本号作为键

DeployVersion::releaseNotes();

// [ '1.0.0 <span>(August 25, 2018)</span>' => [ /* notes for this release */ ], '1.0.0-beta <span>(August 23, 2018)</span>' => [ /* notes for this beta release */ ] ]

您还可以将 major 作为第一个参数传递,以仅返回最新主要版本的说明;minor 将返回最新次要版本的说明;single 将仅返回最新版本的说明。传递任何参数都将返回类似的数组。

发布日期

可以通过调用日期函数来访问最新版本的日期/时间。这将返回一个 Carbon 对象,因此您可以按自己的意愿进行操作(有关详细信息,请参阅 Carbon

DeployVersion::date()->format('Ymd');

// 20180821

待办事项

  • 测试套件