vetruvet/laravel-self-updater

此包已废弃,不再维护。未建议替代包。

Laravel 的 Git webhook 自更新工具

v1.0.0-beta4 2014-09-09 16:50 UTC

This package is not auto-updated.

Last update: 2023-09-11 21:30:22 UTC


README

此工具为使用 Git 和支持 webhooks 的中央 Git 服务器(如 Github 或 Gitlab)的 Laravel 4.x 项目提供自动自我更新功能。

功能

更新器由 Git 服务器在发生推送时执行的 POST 请求触发。一旦触发,它会执行以下操作:

  1. 检查推送是否指向已配置的分支
  2. 触发 pre-update 事件监听器
  3. 检查树状态以确保在拉取之前干净
  4. 执行 git pull
  5. 获取当前提交哈希值(用于显示目的)
  6. 获取已拉取的提交列表。
  7. 重建优化后的类文件(《清除编译’、‘导出自动加载’、‘优化’)
  8. 运行所有迁移
  9. 触发 post-update 事件监听器
  10. 可选地通过电子邮件通知更新状态

要求

  • Laravel 4.x
  • 您的 Git 项目在一个具有 webhooks 的 Git 仓库中
  • 在 Web 服务器上安装了 git

安装

在您的 composer.json 中要求此包并运行 composer update(或直接运行 composer require vetruvet/laravel-self-updater:dev-master

"vetruvet/laravel-self-updater": "dev-master"

更新 composer 后,将 ServiceProvider 添加到 app/config/app.php 中的 providers 数组

'Vetruvet\LaravelSelfUpdater\SelfUpdaterServiceProvider',

配置

要自定义配置,请运行以下命令

$ php artisan config:publish vetruvet/laravel-self-updater

配置选项

<?php

return array(
    'routes' => array(
        'manual' => '/trigger_update', // url for GET manual trigger      DEFAULT: /trigger_update
        'auto'   => '/trigger_update', // url for POST automatic trigger  DEFAULT: /trigger_update

        'manual_filter' => null, // filter for manual trigger route (e.g. if you want to 'auth.admin' first)  DEFAULT: <none>
    ),

    'branch'             => 'master', // branch to listen for pushes for and to pull from  DEFAULT: master
    'site_name'          => null,     // site name - used in various display functions     DEFAULT: $_SERVER['SERVER_NAME']
    'commit_hash_length' => 7,        // length limit for commit hash (0 for full hash)    DEFAULT: 0

  //'email' => false, //false to disable email notifications
    'email' => array(
        // from info for notification email  DEFAULT: as specified in app/config/mail.php; or <site_name> Self Updater <update@$_SERVER['SERVER_NAME']>
        'from'     => array('address' => 'update@example.com', 'name' => 'Sample Self-Updater'),
        'to'       => 'update-notify@example.com', // target for notification email  REQUIRED
        'reply_to' => 'dev-team@example.com',      // reply-to for notification email  DEFAULT: <from>

        //subject can be a static string, or a callback (or either split out into success/failure subject lines)
      //'subject'  => 'Self-Updater was triggered',
      //'subject'  => function($site_name, $success, $commit_hash) { return $site_name . ' Self-Updater ' . ($success ? 'Success: ' . $commit_hash : 'ERROR'); },
        'subject'  => array(
            'success' => 'Self-Updater success', // can also be callback function like above
            'failure' => 'Self-Updater failure',
        ),
    ),
);

要自定义更新通知电子邮件,请运行以下命令

$ php artisan view:publish vetruvet/laravel-self-updater

现在,在您的 Git 服务器上设置 webhook,使其指向配置中指定的路由(默认为 /trigger_update),您就完成了!

事件监听器

Event::listen('self-updater.pre-update', function ($auto) {
    // $auto: boolean whether update was triggered automatically through webhook (POST) or manually (GET)
    // 
    // return false; // return false to cancel the update operation.
});

Event::listen('self-updater.post-update', function($success, $error, $auto, $git_commit_hash, $sent_email) {
    // $success: boolean success or failure
    // $error: error string if failure, empty string otherwise
    // $auto: boolean whether update was triggered automatically through webhook (POST) or manually (GET)
    // $git_commit_hash: new git commit hash if update was successful, empty string otherwise
    // $sent_email: boolean whether or not a notification email was sent
});

计划功能

  • 在拉取后更新 composer 包(需要提交 composer.lock)
  • 重新启动队列守护进程和其他后台任务(某种方式,尚无头绪)。

欢迎并提出功能建议,并将非常感激。