bluedot-bd / phploy
PHPloy - 支持子模块、多服务器和回滚的增量 Git (S)FTP 部署工具。
Requires
- php: ^8.0
- league/climate: ^3.0
- league/flysystem: ^3.0
- league/flysystem-ftp: ^3.0
- league/flysystem-sftp-v3: ^3.0
Requires (Dev)
- phpunit/phpunit: ^8.0
- teqneers/php-stream-wrapper-for-git: >=1.0.1 <2
This package is auto-updated.
Last update: 2024-09-08 11:52:32 UTC
README
版本 4.10.1 Flysystem 更新
PHPloy 是一个增量 Git FTP 和 SFTP 部署工具。它通过跟踪远程服务器(群)的状态,只部署自上次部署以来已提交的文件。PHPloy 支持 submodules、sub-submodules、部署到多个服务器和回滚。PHPloy 需要 PHP 7.3+ 和 Git 1.8+。
工作原理
PHPloy 在您的服务器上存储一个名为 .revision
的文件。该文件包含您已部署到该服务器的提交的哈希值。当您运行 phploy 时,它会下载该文件并比较其中的提交引用与您要部署的提交,以确定要上传哪些文件。PHPloy 还为您的存储库中的每个子模块存储一个 .revision
文件。
安装
通过 Composer
如果您在计算机上安装了 composer,您可以全局拉取 PHPloy,如下所示
composer global require "desm-it/PHPloy" or composer global require "bluedot-bd/PHPloy"
请确保将 $HOME/.composer/vendor/bin
目录(或您操作系统的等效目录)放置在您的 $PATH
中,以便系统可以找到 PHPloy 可执行文件。
通过 Phar 存档
您可以将 PHPloy Phar 全局安装到 /usr/local/bin
目录或本地安装到您的项目目录中。将 phploy.phar
重命名为 phploy
以便于使用。
- 全局: 将
phploy
移动到/usr/local/bin
。通过运行sudo chmod +x phploy
使其可执行。 - 本地: 将
phploy
移动到您的项目目录。
使用方法
在本地使用 PHPloy 时,请在命令前加上 php
- 在终端中运行
phploy --init
以创建phploy.ini
文件或手动创建一个。 - 在终端中运行
phploy
以进行部署。
Windows 用户:在 Windows 上全局安装 PHPloy
phploy.ini
phploy.ini
文件包含您的项目配置。它应位于项目的根目录中。phploy.ini
从不会上传到服务器。请查看以下示例以了解所有可用的选项
; This is a sample deploy.ini file. You can specify as many ; servers as you need and use normal or quickmode configuration. ; ; NOTE: If a value in the .ini file contains any non-alphanumeric ; characters it needs to be enclosed in double-quotes ("). [staging] scheme = sftp user = example ; When connecting via SFTP, you can opt for password-based authentication: pass = password ; Or private key-based authentication: privkey = 'path/to/or/contents/of/privatekey' host = staging-example.com path = /path/to/installation port = 22 ; You can specify a branch to deploy from branch = develop ; File permission set on the uploaded files/directories permissions = 0700 ; File permissions set on newly created directories directoryPerm = 0775 ; Deploy only this directory as base directory base = 'directory-name/' ; Files that should be ignored and not uploaded to your server, but still tracked in your repository exclude[] = 'src/*.scss' exclude[] = '*.ini' ; Files that are ignored by Git, but you want to send the the server include[] = 'js/scripts.min.js' include[] = 'directory-name/' ; conditional include - if source file has changed, include file include[] = 'css/style.min.css:src/style.css' ; Directories that should be copied after deploy, from->to copy[] = 'public->www' ; Directories that should be purged before deploy purge-before[] = "dist/" ; Directories that should be purged after deploy purge[] = "cache/" ; Pre- and Post-deploy hooks ; Use "DQOUTE" inside your double-quoted strings to insert a literal double quote ; Use 'QUOTE' inside your qouted strings to insert a literal quote ; For example pre-deploy[] = 'echo "that'QUOTE's nice"' to get a literal "that's". ; That workaround is based on https://php.ac.cn/manual/de/function.parse-ini-file.php#70847 pre-deploy[] = "wget http://staging-example.com/pre-deploy/test.php --spider --quiet" post-deploy[] = "wget http://staging-example.com/post-deploy/test.php --spider --quiet" ; Works only via SSH2 connection pre-deploy-remote[] = "touch .maintenance" post-deploy-remote[] = "mv cache cache2" post-deploy-remote[] = "rm .maintenance" ; You can specify a timeout for the underlying connection which might be useful for long running remote ; operations (cache clear, dependency update, etc.) timeout = 60 [production] quickmode = ftp://example:password@production-example.com:21/path/to/installation passive = true ssl = false ; You can specify a branch to deploy from branch = master ; File permission set on the uploaded files/directories permissions = 0774 ; File permissions set on newly created directories directoryPerm = 0755 ; Files that should be ignored and not uploaded to your server, but still tracked in your repository exclude[] = 'libs/*' exclude[] = 'config/*' exclude[] = 'src/*.scss' ; Files that are ignored by Git, but you want to send the the server include[] = 'js/scripts.min.js' include[] = 'js/style.min.css' include[] = 'directory-name/' purge-before[] = "dist/" purge[] = "cache/" pre-deploy[] = "wget http://staging-example.com/pre-deploy/test.php --spider --quiet" post-deploy[] = "wget http://staging-example.com/post-deploy/test.php --spider --quiet"
如果您的密码在 phploy.ini
文件或 PHPLOY_PASS
环境变量中缺失,PHPloy 将会交互式地询问您的密码。还有一个选项可以将用户名和密码存储在一个名为 .phploy
的文件中。
[staging]
user="theUser"
pass="thePassword"
[production]
user="theUser"
pass="thePassword"
此功能特别有用,如果您想通过 Git 分享您的 phploy.ini,但又想将密码隐藏起来。
您还可以使用环境变量进行部署,而不需要在文件中存储您的凭据。如果这些变量不在 phploy.ini
文件中,将使用这些变量
PHPLOY_HOST
PHPLOY_PORT
PHPLOY_PASS
PHPLOY_PATH
PHPLOY_USER
PHPLOY_PRIVKEY
这些变量可以像这样使用;
$ PHPLOY_PORT="21" PHPLOY_HOST="myftphost.com" PHPLOY_USER="ftp" PHPLOY_PASS="ftp-password" PHPLOY_PATH="/home/user/public_html/example.com" phploy -s servername
或者像这样导出,脚本将自动使用它们
$ export PHPLOY_PORT="21"
$ export PHPLOY_HOST="myftphost.com"
$ export PHPLOY_USER="ftp"
$ export PHPLOY_PASS="ftp-password"
$ export PHPLOY_PATH="/home/user/public_html/example.com"
$ export PHPLOY_PRIVKEY="path/to/or/contents/of/privatekey"
$ phploy -s servername
多服务器
PHPloy 允许您在部署文件中配置多个服务器,并轻松地将部署到其中任何一个。
默认情况下,PHPloy 将部署到所有指定的服务器。如果您的服务器配置中存在名为 'default' 的条目,则 PHPloy 将默认使用该服务器配置。要指定单个服务器,请运行
phploy -s servername
或者
phploy --server servername
servername
代表你在 phploy.ini
配置文件中给服务器指定的名称。
如果你配置了一个 'default' 服务器,你可以通过运行以下命令将代码部署到所有配置的服务器上:
phploy --all
共享配置(自定义默认值)
如果你指定了名为 *
的服务器配置,本节中配置的所有选项都将与其他服务器共享。这基本上允许你注入自定义默认值。
; The special '*' configuration is shared between all other configurations (think include) [*] exclude[] = 'src/*' include[] = "dist/app.css" ; As a result both shard1 and shard2 will have the same exclude[] and include[] "default" values [shard1] quickmode = ftp://example:password@shard1-example.com:21/path/to/installation [shard2] quickmode = ftp://example:password@shard2-example.com:21/path/to/installation
回滚
警告:目前 --rollback 选项无法正确更新你的子模块。
PHPloy 允许你在需要时回滚到早期版本。回滚操作非常简单。
要回滚到上一个提交,只需运行:
phploy --rollback
要回滚到你想要的任何提交,运行:
phploy --rollback commit-hash-goes-here
当你运行回滚时,你的工作副本中的文件将临时恢复到你正在部署的回滚版本。当部署完成后,一切将恢复原状。
请注意,没有 --rollback
的简写版本。
列出已更改的文件
PHPloy 允许你在实际推送之前查看将要上传/删除的文件。只需运行:
phploy -l
或者:
phploy --list
更新或“同步”远程版本
如果你想要更新服务器上的 .revision
文件以匹配当前的本地版本,请运行:
phploy --sync
如果你想要将其设置为之前的提交版本,只需指定版本如下:
phploy --sync your-revision-hash-here
在首次部署时创建部署目录
如果部署目录不存在,你可以指示 PHPloy 为你创建它
phploy --force
手动全新上传
如果你想要进行全新上传,即使你已经部署过,也可以使用 --fresh
参数,如下所示:
phploy --fresh
子模块
子模块受支持,但默认关闭,因为你不期望它们经常更改,而且你只是偶尔更新它们。要运行带有子模块扫描的部署,请将 --submodules
参数添加到命令中
phploy --submodules
清除
在许多情况下,我们需要在部署后清除目录的内容。可以通过在 phploy.ini
中指定目录来实现,如下所示:
; relative to the deployment path
purge[] = "cache/"
要在部署前清除目录,请在 phploy.ini
中指定目录,如下所示:
; relative to the deployment path
purge-before[] = "dist/"
钩子
PHPloy 允许你在部署前后执行命令。例如,你可以使用 wget
调用我的服务器上的脚本以执行 composer update
。
; To execute before deployment
pre-deploy[] = "wget http://staging-example.com/pre-deploy/test.php --spider --quiet"
; To execute after deployment
post-deploy[] = "wget http://staging-example.com/post-deploy/test.php --spider --quiet"
日志记录
PHPloy 支持简单的活动日志记录。日志以以下格式保存在你的项目中的 phploy.log
文件中:
2016-03-28 08:12:37+02:00 --- INFO: [SHA: 59a387c26641f731df6f0d1098aaa86cd55f4382] Deployment to server: "default" from branch "master". 2 files uploaded; 0 files deleted.
要开启日志记录,请将以下内容添加到 phploy.ini
:
[production]
logger = on
贡献
贡献非常欢迎;PHPloy 因贡献者而伟大。请查看 问题。
致谢
版本历史
请查看 发布历史 以获取详细信息。
许可证
PHPloy 采用 MIT 许可证(MIT)许可。