首页 > 解决方案 > Laravel 部署理念

问题描述

我正在为我的客户公司开发 Laravel 应用程序。那家公司有一个服务员。在开发过程中,我让他为应用程序设置一个服务器。但是他设置了一个没有任何自动部署程序的服务器,而是给了我一个 C-Pannel 来手动部署更改。应用程序变得如此复杂,现在手动部署成为一项非常繁琐的任务。我正在手动构建 js 和 css 并将它们上传到服务器。在与他激烈争论之后,我终于让他从事自动化部署的工作。他没有正确地进行部署,而是这样说。

 
<?php
//app/Console/Commands/GitPull.php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GitPull extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'git:pull';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Get updates from git server';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        exec('git pull origin master');
    }
}

<?php
app/Console/Kernel.php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
       Commands\GitPull::class, 
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('git:pull')
                  ->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

基本上他已经放了一个调度程序来每分钟执行一次 git pull 。这是放置部署的正确方法吗?这种方式有什么缺点?部署是否必须依赖于应用程序?是否可以在没有 laravel 框架的帮助下添加自动化部署?我对开发运维不是很熟悉。

另外,我的源代码在 bitbucket 中。我准备回答有关此的任何问题。提前致谢。

标签: phplaraveldevopscontinuous-deployment

解决方案


我当然不会使用 Laravel 命令来运行部署。自动化部署可以根据需要简单或复杂,但这里有一些我希望构建代理做的事情:

  • 收听对 master 的更改以进行生产部署
  • 运行测试(如果失败则提醒并停止部署)
  • 构建我的静态资产(js/css/etc)
  • 将代码复制到服务器(有几种方法可以做到这一点)
  • 运行迁移
  • 重新启动队列,以便它们使用最新代码运行

由于您使用的是 bitbucket,因此您可以查看管道来执行 ci/cd。


推荐阅读