laravel使用php部署工具deployer实战

deployer 一键部署php代码,还可以保留历史版本.
官网:https://deployer.org
我安装的deployer版本 7.x

注意,7.x版本和6.x 部署命令不一致, 请注意看文档,
网上很多教程都是6.x的.

安装deployer composer require --dev deployer/deployer

引入官方的laravel的配置文件
文档: https://deployer.org/docs/7.x/recipe/laravel

laravel菜谱配置文件里面配置了一些目录,例如共享目录,日志目录,.env等文件路径

然后定义了一些任务.

定义了deploy任务, 里面是调用一些子任务.

运行原理

deployer 脚本会到远程服务器 执行 git clone 拉取代码
执行composer install
把.env 和上传文件的目录, 配置软连接到新目录.
执行php artisan migrate等

部署命令?

# 调用部署任务 deploy , 第二个参数是选择部署到哪个环境
vendor/bin/dep deploy production -vvv

# 也可以调用其他自定义的任务
vendor/bin/dep test myhost


# 或者执行ssh连接服务器命令
vendor/bin/dep ssh

ngixn配置

服务器的nginx配置到部署目录的 current 目录上.
新发布的版本会自动软链接到current目录.
这样部署完不用修改nginx

注意事项

php禁用函数: putenv 要取消禁用

fileinfo扩展 需要自行安装

在本地配置好免密登录配置文件 ~/用户名/.ssh/config 文件

报错:

command-line line 0: unsupported option “accept-new”.

不支持的选项accept-new

网上说的升级openssh 和 git 不好使

加上 set('git_ssh_command', 'ssh');

完整的deploy.php

<?php

namespace Deployer;

require 'recipe/laravel.php';

// Config
set('application', 'csdn多才多多的项目');
set('repository', 'git@xxx.com:csdn-duocaiduoduo/project_name.git');
set('git_tty', true);
add('shared_files', []);
add('shared_dirs', []);
add('writable_dirs', []);

set('git_ssh_command', 'ssh');

task('test', function () {
    writeln('type:' . get('labels')['type'] . ' env:' . get('labels')['env'] . ',hostname:' . get('hostname'));
});
// Hosts列表
//测试机
host('192.168.56.101')
    ->setLabels([
        'type' => 'web',
        'env' => 'test',
    ])->setRemoteUser('root')
    ->set('branch', 'master')
    ->setDeployPath('/www/wwwroot/project');

//线上机器
host('123.22.22.22')
    ->setLabels([
        'type' => 'web',
        'env' => 'prod',
    ])->setRemoteUser('root')
    ->set('branch', 'master')
    ->setDeployPath('/www/wwwroot/project');



//->setIdentityFile('~/.ssh/id_ed25519');连接远程主机的私钥,免密登录
//->setSshArguments(['-o UserKnownHostsFile=/dev/null']);

// Hooks

after('deploy:failed', 'deploy:unlock');


执行部署命令

# 部署到标签env=prod的机器
vendor/bin/dep deploy env=prod 

# 部署到标签env=test 的机器
vendor/bin/dep deploy env=test 

# 执行test任务,标签env=prod的机器
vendor/bin/dep test env=prod  

# 查看部署的版本列表
vendor/bin/dep releases env=test -vvv

# 回滚
vendor/bin/dep rollback env=prod 

# -vvv 表示部署时显示详细信

# ssh root@123.22.22.22 -i ~/.ssh/id_ed25519

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值