alapaev/laravel-deployment

通过利用Git网络钩子帮助自动化将项目部署到服务器。

0.4.0 2018-05-18 09:06 UTC

This package is not auto-updated.

Last update: 2024-09-26 23:32:59 UTC


README

git-deploy-laravel 允许通过从您的仓库服务器的webhook请求自动部署,并使用本地Git二进制文件自动拉取项目代码。

这应该可以在Laravel 5.x上开箱即用,使用GitHub和GitLab服务器的webhooks。

这是一个帮助我们常见工作流程模式的内部工具,但请随意借用、修改和改进。

安装

步骤 1

将以下内容添加到您的 composer.json 文件中,然后按常规更新您的composer

{
    "require" : {
        "alapaev/git-deploy-laravel" : "dev-master"
    }
}

或者运行

composer require alapaev/git-deploy-laravel

步骤 2

将以下行添加到您的 config/app.php 中的 providers

Orphans\GitDeploy\GitDeployServiceProvider::class,

** Laravel 5.5 新功能 - 不需要以上更改即可自动检测服务提供者 **

步骤 3

/git-deploy 路由添加到 CSRF 异常中,以便您的仓库主机可以发送消息到您的项目。

在文件 app/Http/Middleware/VerifyCsrfToken.php 中添加

protected $except = [
    'git-deploy',
];

步骤 4(可选)

如果成功提交后需要执行额外的操作,您可以添加您自己的事件监听器。例如,您可以编写自己的更新脚本来运行迁移等。

1) 创建一个监听器,在Git部署完成后执行操作。打开 App/Listeners 目录(如果不存在则创建)。现在创建一个新文件,命名为 GitDeployedListener.php。粘贴以下代码

<?php

namespace App\Listeners;

use \Orphans\GitDeploy\Events\GitDeployed;
use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Queue\ShouldQueue;

class GitDeployedListener implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        // Here you can setup something
    }

    /**
     * Handle the event.
     *
     * @param  ReactionAdded  $event
     * @return void
     */
    public function handle(GitDeployed $gitDeployed)
    {
        // Do some magic with event data $gitDeployed contains the commits

    }
}

如您所见,这是一个普通的事件监听器。您可能会注意到监听器 implements ShouldQueue — 这非常有用,因为我们的应用程序必须快速响应。如果您想在事件监听器中执行一些长时间运行的操作,您需要配置一个队列。

2) 现在我们将此监听器添加到 /App/Providers/EventServiceProvider.php 中,就像添加任何其他事件监听器一样

// ...

protected $listen = [

        // ...

       \Orphans\GitDeploy\Events\GitDeployed::class => [
            \App\Listeners\GitDeployedListener::class
        ]
    ];

// ...

用法

http://your.website.url/git-deploy 添加您的GitHub/GitLab项目中的webhook,此包将负责其余操作。webhook应该在push事件上触发。

您的网站将自动接收来自仓库管理器的POST消息并执行Git pull。

配置

在大多数情况下,包将找到正确的Git仓库和Git可执行文件,但我们仍然建议发布我们的配置,因为它将让您启用额外的安全选项和电子邮件通知。

要添加自定义配置,请运行

php artisan vendor:publish --provider="Orphans\GitDeploy\GitDeployServiceProvider"

然后编辑 /config/gitdeploy.php 以满足您的需求。

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Email recipients
    |--------------------------------------------------------------------------
    |
    | The email address and name that notification emails will be sent to.
    | Leave the array empty to disable emails.
    |
    | [
    |     ['name' => 'Joe Bloggs', 'address' => 'email@example1.com'],
    |     ['name' => 'Jane Doe', 'address' => 'email@example2.com'],
    |     ...
    | ]
    |
    */

    'email_recipients' => [],

    /*
    |--------------------------------------------------------------------------
    | Email sender
    |--------------------------------------------------------------------------
    |
    | The email address and name that notification emails will be sent from.
    | This will default to the sender in config(mail.from) if left null.
    |
    */

    'email_sender' => ['address' => null, 'name' => null],

    /*
    |--------------------------------------------------------------------------
    | Repository path
    |--------------------------------------------------------------------------
    |
    | This the root path of the Git repository that will be pulled. If this
    | is left empty the script will try to determine the directory itself
    | but looking for the project's .env file it's nearby .git directory.
    |
    | No trailing slash
    |
    */

    'repo_path' => '',

    /*
    |--------------------------------------------------------------------------
    | Allowed sources
    |--------------------------------------------------------------------------
    |
    | A request will be ignored unless it comes from an IP listed in this
    | array. Leave the array empty to allow all sources.
    |
    | This is useful for a little extra security if you run your own Git
    | repo server.
    |
    | Relies on the REMOTE_ADDR of the connecting client matching a value
    | in the array below. So if using IPv6 on both the server and the
    | notifing git server, then make sure to add it to the array. If your git
    | server listens on IPv4 and IPv6 it would be safest to add both.
    |
    | e.g.
    | 
    | 'allowed_sources' => ['192.160.0.1', '::1'], 
    |
    */

    'allowed_sources' => [],

    /*
    |--------------------------------------------------------------------------
    | Remote name
    |--------------------------------------------------------------------------
    |
    | The name of the remote repository to pull the changes from
    |
    */
    
    'remote' => 'origin',

    /*
    |--------------------------------------------------------------------------
    | Git binary path
    |--------------------------------------------------------------------------
    |
    | The full path to the system git binary. e.g. /usr/bin/git
    |
    | Leave blank to let the system detect using the current PATH variable
    |
    */
    
    'git_path' => '',

    /*
    |--------------------------------------------------------------------------
    | Maintenance mode
    |--------------------------------------------------------------------------
    |
    | Allow the git hook to put the site into maintenance mode before doing
    | the pull from the remote server.
    |
    | After a successful pull the site will be switched back to normal
    | operations. This does leave a possibility of the site remaining in
    | maintenance mode should an error occur during the pull.
    |
    */

    'maintenance_mode' => true,

    /*
    |--------------------------------------------------------------------------
    | Fire Event
    |--------------------------------------------------------------------------
    |
    | Allow the git hook to fire a event "GitDeployed" so that everybody can listen to that event.
    | See readme how to create a nice listener on that.
    |
    */
    'fire_event' => true,

    /*
    |--------------------------------------------------------------------------
    | Secret signature
    |--------------------------------------------------------------------------
    |
    | Allow webhook requests to be signed with a secret signature.
    |
    | If 'secret' is set to true, Gitdeploy will deny requests where the
    | signature does not match. If set to false it will ignore any signature
    | headers it recieves.
    | 
    | For Gitlab servers, you probably want the settings below:
    | 
    |     'secret_type' => 'plain',
    |     'secret_header' => 'X-Gitlab-Token',
    |
    | For Github, use something like the below (untested):
    |
    |    'secret_type' => 'hmac',
    |    'secret_header' => 'X-Hub-Signature',
    */
   
    'secret' => false,

    /**
     * plain|hmac
     */
    'secret_type' => 'plain',

    /**
     * X-Gitlab-Token|X-Hub-Signature
     */
    'secret_header' => 'X-Gitlab-Token',

    /**
     * The key you specified in the pushing client
     */
    'secret_key' => '',

];

未来计划

  • 在代码冲突阻止pull时发送电子邮件报告
  • 在部署后执行 composer install
  • 在部署后使用 artisan queue:restart 重新启动laravel队列
  • 在成功拉取后运行自定义的artisan命令