前提条件
- 本地可以使用从服务器上拉取并提交项目
- 服务器可以使用ssh通过密钥文件拉取项目
- 配置文件所配置的用户在服务器有写的权限
项目地址
https://github.com/lorisleiva/laravel-deployer
- 安装
composer require lorisleiva/laravel-deployer
- 生成部署文件
php artisan deploy:init
- 配置
# 配置文件在config/deploy.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default deployment strategy 默认的部署策略
|--------------------------------------------------------------------------
|
| This option defines which deployment strategy to use by default on all
| of your hosts. Laravel Deployer provides some strategies out-of-box
| for you to choose from explained in detail in the documentation.
|
| Supported: 'basic', 'firstdeploy', 'local', 'pull'.
|
*/
'default' => 'basic',
/*
|--------------------------------------------------------------------------
| Custom deployment strategies
|--------------------------------------------------------------------------
|
| Here, you can easily set up new custom strategies as a list of tasks.
| Any key of this array are supported in the `default` option above.
| Any key matching Laravel Deployer's strategies overrides them.
|
*/
'strategies' => [
//
],
/*
|--------------------------------------------------------------------------
| Hooks
|--------------------------------------------------------------------------
|
| Hooks let you customize your deployments conveniently by pushing tasks
| into strategic places of your deployment flow. Each of the official
| strategies invoke hooks in different ways to implement their logic.
|
*/
// hooks自动部署(仅需要在push代码后自动触发部署事件时使用)
'hooks' => [
// Right before we start deploying.
'start' => [
//
],
// Code and composer vendors are ready but nothing is built.
'build' => [
//
],
// Deployment is done but not live yet (before symlink)
'ready' => [
'artisan:storage:link',
'artisan:view:clear',
'artisan:cache:clear',
'artisan:config:cache',
'artisan:migrate',
'artisan:horizon:install',
],
// Deployment is done and live
'done' => [
//
],
// Deployment succeeded.
'success' => [
//
],
// Deployment failed.
'fail' => [
//
],
],
/*
|--------------------------------------------------------------------------
| Deployment options 部署选项
|--------------------------------------------------------------------------
|
| Options follow a simple key/value structure and are used within tasks
| to make them more configurable and reusable. You can use options to
| configure existing tasks or to use within your own custom tasks.
|
*/
'options' => [
// 项目名称
'application' => env('APP_NAME', 'Laravel'),
// 项目git仓库
'repository' => 'git@git.zhantai.com:mingzhi_wuliu.git',
// 部署完要重启的php-fpm
'php_fpm_service' => 'php7.3-fpm',
],
/*
|--------------------------------------------------------------------------
| Hosts
|--------------------------------------------------------------------------
|
| Here, you can define any domain or subdomain you want to deploy to.
| You can provide them with roles and stages to filter them during
| deployment. Read more about how to configure them in the docs.
|
*/
// 主机
'hosts' => [
// 项目域名或外网ip
'wuliu.mingzhihuatong.com' => [
// 部署项目的路径
'deploy_path' => '/data/site/www/wuliu.mingzhihuatong.com',
// 部署项目所使用的服务器用户名称
'user' => 'dev',
// 部署项目阶段
'stage' => 'production',
// 要拉去的git分支
'branch' => 'master',
],
],
/*
|--------------------------------------------------------------------------
| Localhost
|--------------------------------------------------------------------------
|
| This localhost option give you the ability to deploy directly on your
| local machine, without needing any SSH connection. You can use the
| same configurations used by hosts to configure your localhost.
|
*/
'localhost' => [
//
],
/*
|--------------------------------------------------------------------------
| Include additional Deployer recipes
|--------------------------------------------------------------------------
|
| Here, you can add any third party recipes to provide additional tasks,
| options and strategies. Therefore, it also allows you to create and
| include your own recipes to define more complex deployment flows.
|
*/
// 自己的额外选项配置 项目根目录新建自定义目录,再新建自定义配置文件,如:
'include' => [
'my_deploy/my_recipe.php',
],
/*
|--------------------------------------------------------------------------
| Use a custom Deployer file
|--------------------------------------------------------------------------
|
| If you know what you are doing and want to take complete control over
| Deployer's file, you can provide its path here. Note that, without
| this configuration file, the root's deployer file will be used.
|
*/
'custom_deployer_file' => false,
];
自定义额外配置
# my_deploy/my_recipe.php
<?php
// 命名空间不能变,必须是这个
namespace Deployer;
// 发布队列管理工具前端资源(任务名必须同时在config/deploy.php中的ready数组中新增才可以)
task('artisan:horizon:install', function () {
run("cd {{release_path}} && {{bin/php}} artisan horizon:install");
});
注意:自定义配置文件的命名空间必须是 Deployer,不能是其他的
部署命令
php artisan deploy 域名/服务器外网IP