webforward / gitdeploy
使用 composer 和 npm 支持的易于使用的自动 Git 部署工具,适用于 PHP 项目
dev-master
2020-12-02 19:48 UTC
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2024-09-29 05:29:19 UTC
README
使用 composer 和 npm 支持的易于使用的自动 Git 部署工具,适用于 PHP 项目。
要求
- PHP 7.0 或更高版本,并且已启用
proc_open
和shell_exec
函数。 - 服务器上运行脚本(服务器机器)需要安装
git
和rsync
。- 可选,备份功能(
backup_dir
选项)需要tar
。 - 可选,使用 composer 功能(
use_composer
选项)需要composer
。 - 可选,使用 npm 功能(
use_npm
选项)需要npm
。
- 可选,备份功能(
- 运行 PHP 的系统用户(例如
www-data
)需要拥有对 服务器机器 上temp_dir
和target_dir
位置的必要访问权限。 - 如果您要部署的 Git 仓库是私有的,运行 PHP 的系统用户还需要安装正确的 SSH 密钥以访问远程仓库。请参阅 GitHub 或 BitBucket 的示例。
使用 - 在您的项目中使用 composer
- 首先运行命令
composer require webforward/gitdeploy:dev-master
。 - 在您的项目中使用以下代码并配置选项
use GitDeploy\GitDeploy; // PSR-4 - This line must be at the top of a script, not inside a function $gd = new GitDeploy(); $gd->remote_repository = 'https://github.com/williammalone/Simple-HTML5-Drawing-App.git'; $gd->target_dir = '/home/user/public_html'; // Your configurable options here, see deploy.php $gd->deploy();
- 设置一个检查/方法,使得上述代码不能被任何人公开访问,代码应放置在 admin php 会话之后,或运行 deploy() 函数时需要输入密钥或密码。请检查
deploy.php
以获取实现此功能的简单方法。 - 确保这些文件也存在于您的 git 仓库中,否则(根据您的配置选项),它们可能在下一次部署时被删除。
- 通过在浏览器中访问脚本 URL 来测试部署。
- 一旦满意,请按照设置钩子的步骤操作。请参阅 GitHub、BitBucket 或 Generic Git 的示例。
使用 - 独立
- 将此存储库的副本下载到您的网站目录中,例如 /deploy。
- 编辑
deploy.php
或创建自己的 php 文件,并使用以下代码以及配置选项
use GitDeploy\GitDeploy; // This line must be at the top of a script, not inside a function require 'path/to/src/GitDeploy.php'; $gd = new GitDeploy(); $gd->remote_repository = 'https://github.com/williammalone/Simple-HTML5-Drawing-App.git'; $gd->target_dir = '/home/user/public_html'; // Your configurable options here, see deploy.php $gd->deploy();
- 设置一个检查/方法,使得上述代码不能被任何人公开访问,您可以使用
deploy.php
中已编写的示例。出于安全原因,您的脚本应要求输入密钥或密码才能运行 deploy() 函数。 - 确保这些文件也存在于您的 git 仓库中,否则(根据您的配置选项),它们可能在下一次部署时被删除。
- 通过在浏览器中访问脚本 URL 来测试部署。
- 一旦满意,请按照设置钩子的步骤操作。请参阅 GitHub、BitBucket 或 Generic Git 的示例。
GitHub
- (此步骤仅适用于私有仓库) 访问
https://github.com/USERNAME/REPOSITORY/settings/keys
并添加您的服务器 SSH 密钥。 - 访问
https://github.com/USERNAME/REPOSITORY/settings/hooks
。 - 在 Webhooks 面板中点击 添加 webhook。
- 输入您的部署脚本 URL,例如
https://domain.com/deploy.php?secret=changeme
。 - 可选 选择应触发部署的事件。
- 确保选中了 Active 复选框。
- 点击 添加 webhook。
Bitbucket
- (此步骤仅适用于私有仓库) 访问
https://bitbucket.org/USERNAME/REPOSITORY/admin/deploy-keys
并添加您的服务器 SSH 密钥。 - 访问
https://bitbucket.org/USERNAME/REPOSITORY/admin/services
。 - 添加 POST 服务。
- 输入您的部署脚本 URL,例如
https://domain.com/deploy.php?secret=changeme
。 - 点击 保存。
Generic Git
- 配置 SSH 密钥。
- 添加一个可执行的
.git/hooks/post_receive
脚本来调用脚本,例如。
#!/bin/sh wget -q -O /dev/null https://domain.com/deploy.php?sat=changeme
完成!
下次您将代码推送到启用了钩子的仓库时,它将触发 deploy.php
脚本,该脚本将拉取更改并更新 服务器机器 上的代码。
更多信息,请阅读 deploy.php
和 src/GitDeploy.php
的源代码。
如果您觉得这个脚本很有用,请考虑向 PayPal richard@webfwd.co.uk
捐款。