ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

使用 Deployer 零停机部署 Statamic 项目:官方 Recipe 全解析与实战指南

使用 Deployer 零停机部署 Statamic 项目:官方 Recipe 全解析与实战指南 DevOpsCI/CDCLI开发工具运维【免费下载链接】deployerThe PHP deployment tool with support for popular frameworks out of the box项目地址https://gitcode.com/gh_mirrors/de/deployer点击查看免费下载Statamic 是一个基于 Laravel 构建的现代内容管理系统CMS其部署流程在常规 Laravel 应用的基础上还涉及 Stache 缓存、静态页面缓存、资源预设、搜索索引等一系列 Statamic 专属产物。Deployer 官方在 recipe/statamic.php 中提供了开箱即用的 Statamic 部署配方Recipe本文以该配方为骨架结合 docs/recipe/statamic.md 文档与仓库源码完整讲解如何配置、运行并理解 Statamic 的自动化部署流程读者读完后可以独立完成 Statamic 项目的服务器初始化、零停机发布与回滚并掌握所有 Statamic 专属任务的底层原理。一、Statamic Recipe 是什么在 Deployer 中一个 Recipe配方就是一段可复用的 PHP 配置代码它声明了部署所需的主机配置、任务链与参数默认值。Statamic Recipe 的引入方式与其他框架一致require recipe/statamic.php;该文件位于 recipe/statamic.php其开头的注释直接说明了设计意图As Statamic is a Laravel Package, we will extend the Laravel recipe and simply add Statamic specific commands.也就是说Statamic Recipe以 Laravel Recipe 为基础recipe/laravel.php继承 Laravel 全部部署任务与配置再叠加 Statamic 特有的命令。从源码结构看recipe/statamic.php它通过require_once __DIR__ . /laravel.php引入 Laravel 配方并通过add(recipes, [statamic])注册自身标识而 Laravel Recipe 又基于 Common Reciperecipe/common.php因此整个继承链是statamic → laravel → common → deploy/* 基础任务这种分层设计让 Statamic 项目可以免费获得 Laravel 生态的全部 artisan 封装任务以及 Deployer 最核心的三大能力Provisioning服务器初始化——为你的服务器自动完成环境准备Zero downtime deployment零停机部署——通过符号链接原子切换 release 目录部署过程中服务不中断Rollbacks回滚——出问题时一键回滚到上一个版本。此外Deployer 还具备易于上手简洁直观的语法、快速并行连接部署、安全基于 SSH 连接服务器、全面支持所有主流 PHP 框架等特点。若此前未接触过 Deployer建议先阅读 docs/getting-started.md。二、引入 Recipe 后的配置继承引入 Statamic Recipe 后它会继承 Laravel Recipe 设置的以下关键参数定义见 recipe/laravel.php这些参数直接决定了 Statamic 部署时如何处理共享目录、可写目录与 artisan 命令配置项默认值说明shared_dirs[storage]在 release 之间共享的目录每个 release 通过符号链接指向{{deploy_path}}/shared下的实体shared_files[.env]在 release 之间共享的文件writable_dirs[bootstrap/cache, storage]需要 Web 服务器可写的目录writable_recursivetrue可写目录是否递归设置权限log_filesstorage/logs/*.log供logs:app任务跟踪的日志通配路径bin/artisan{{release_or_current_path}}/artisanartisan 可执行文件路径laravel_version动态解析运行php artisan --version后通过正则提取的版本号public_pathpublic公开根目录Statamic Recipe 在此基础上仅追加了一项配置见 recipe/statamic.phpadd(recipes, [statamic]); add(writable_dirs, [ storage/statamic, ]);它把storage/statamicStatamic 存放 Stache、静态缓存等运行期数据的目录追加进writable_dirs确保 Web 服务器用户对其具有写入权限。注意add()与set()的区别set()是整体覆盖而add()是向已有数组追加元素因此 Laravel 的bootstrap/cache、storage等可写目录依然生效。statamic_version版本探测配置Recipe 还注册了一个statamic_version配置recipe/statamic.php用于在部署时探测 Statamic 版本$result run({{bin/php}} {{release_or_current_path}}/please --version); preg_match_all(/(\d\.?)/, $result, $matches); return $matches[0][0] ?? unknown;它通过{{bin/php}}来自 Common Recipe 的bin/php配置默认which(php)解析见 recipe/common.php执行 Statamic 自带的please --version命令然后用正则/(\d\.?)/提取版本号提取失败时兜底返回unknown。注意这里的路径占位符{{release_or_current_path}}在部署期间指向新 release在回滚/查询阶段则指向current保证始终操作的是当前生效的代码。三、Statamic 专属任务总览Recipe 将 Statamic 的 artisan 命令逐个封装为 Deployer 任务recipe/statamic.php每个任务本质上是对statamic:xxxartisan 命令的一层薄封装。这些任务按功能模块分组如下3.1 Addons插件任务对应 artisan 命令说明statamic:addons:discoverstatamic:addons:discover重建缓存的插件包清单manifest新增或移除插件后需要执行3.2 Assets资源任务对应 artisan 命令说明statamic:assets:generate-presetsstatamic:assets:generate-presets生成资源预设preset操作产物statamic:assets:metastatamic:assets:meta生成资源元数据文件3.3 Git版本控制任务对应 artisan 命令说明statamic:git:commitstatamic:git:commit将受跟踪的内容 git add 并 commit3.4 Glide图片处理任务对应 artisan 命令说明statamic:glide:clearstatamic:glide:clear清除 Glide 图片处理缓存3.5 Responsive Images响应式图片非核心任务对应 artisan 命令说明statamic:responsive:generatestatamic:responsive:generate生成响应式图片statamic:responsive:regeneratestatamic:responsive:regenerate重新生成响应式图片3.6 Search搜索任务对应 artisan 命令说明statamic:search:insertstatamic:search:insert向搜索索引插入条目statamic:search:updatestatamic:search:update更新搜索索引3.7 StacheStatamic 核心缓存任务对应 artisan 命令说明statamic:stache:clearstatamic:stache:clear清除 “Stache” 缓存statamic:stache:doctorstatamic:stache:doctor诊断 Stache 存在的问题statamic:stache:refreshstatamic:stache:refresh清除并重建 “Stache” 缓存statamic:stache:warmstatamic:stache:warm构建预热“Stache” 缓存3.8 Static静态页面缓存任务对应 artisan 命令说明statamic:static:clearstatamic:static:clear清除静态页面缓存statamic:static:warmstatamic:static:warm通过访问所有 URL 预热静态缓存3.9 Support支持信息任务对应 artisan 命令说明statamic:support:detailsstatamic:support:details输出便于提交支持请求的详细信息3.10 Updates版本升级任务对应 artisan 命令说明statamic:updates:runstatamic:updates:run从指定版本起运行更新脚本这些任务可直接通过dep单独调用例如在部署后手动重建搜索索引dep statamic:search:update production四、默认 deploy 任务链Statamic 版与 Laravel 版的差异Statamic Recipe 的核心在于用 Statamic 专属的 deploy 任务覆盖 Laravel 默认 deployrecipe/statamic.phpdesc(Deploys your project); task(deploy, [ deploy:prepare, deploy:vendors, artisan:storage:link, artisan:cache:clear, statamic:stache:clear, statamic:stache:warm, deploy:publish, ]);对比 Laravel 默认的 deploy 任务recipe/laravel.php包含artisan:optimize、artisan:migrate、artisan:reloadStatamic 版做了如下调整这是理解本 Recipe 的关键去掉artisan:migrate默认不执行数据库迁移因为 Statamic 的内容entries、collections、taxonomies 等以文件形式存放在仓库中无需迁移步骤去掉artisan:optimize不再统一缓存框架引导文件而是由开发者按需通过 Laravel Recipe 提供的artisan:config:cache、artisan:route:cache、artisan:view:cache等任务自行编排去掉artisan:reload不重载 Octane/RoadRunner 等常驻服务新增statamic:stache:clearstatamic:stache:warm先清空再重建 Stache 缓存确保新 release 的 Stache 与代码内容一致保留artisan:storage:link与artisan:cache:clear建立 storage 符号链接并刷新应用缓存。下面展开deploy任务的完整调用链源文档中给出的deploy组成结构4.1 deploy:prepare —— 准备新 releasedeploy:prepare是deploy任务的第一步由 Common Recipe 定义recipe/common.php是一个包含 8 个子任务的组任务子任务作用deploy:info显示部署信息项目、服务器、分支等deploy:setup在服务器上准备部署目录结构deploy:lock锁定部署防止并发部署冲突deploy:release准备 release 目录deploy:update_code从仓库拉取代码到 releasedeploy:env配置.env文件deploy:shared为共享文件与目录创建符号链接deploy:writable设置可写目录权限其中 deploy:shared 会遍历shared_dirs与shared_files此处即 Laravel 设置的[storage]与[.env]先在{{deploy_path}}/shared下建立实体再在 release 内用{{bin/symlink}}默认ln -nfs若支持--relative则追加见 recipe/common.php替换为符号链接从而让每个 release 共享.env与storage。而 deploy:writable 会根据writable_mode默认acl为writable_dirs含 Statamic 追加的storage/statamic设置权限可选的模式包括chown、chgrp、chmod、acl、sticky、skip配合writable_use_sudo、writable_recursive、writable_chmod_mode默认0755等参数可按需调整。4.2 deploy:vendors —— 安装依赖deploy:vendorsrecipe/deploy/vendors.php在 release 目录执行 Composer 安装run(cd {{release_or_current_path}} {{bin/composer}} {{composer_action}} {{composer_options}} 21);默认composer_action为installcomposer_options为--verbose --prefer-dist --no-progress --no-interaction --no-dev --optimize-autoloaderbin/composer是动态解析的优先使用.dep/composer.phar其次是系统composer都找不到时自动下载安装到部署目录。生产环境通常不需要--no-dev之外的变化如需调整可覆盖composer_options。4.3 缓存与链接处理artisan:storage:link来自 recipe/laravel.php执行php artisan storage:link创建应用配置的 storage 符号链接对应 Statamic 的公共资源访问。artisan:cache:clearrecipe/laravel.php执行php artisan cache:clear刷新应用缓存。说明这两个任务经由 Laravel Recipe 的artisan()辅助函数生成recipe/laravel.php。该辅助函数支持min/maxLaravel 版本范围过滤、skipIfNoEnv/failIfNoEnv针对.env缺失时的跳过或报错、showOutput展示命令输出等选项Statamic 任务同样复用了这套机制因此所有statamic:*任务在底层都会调用{{bin/php}} {{bin/artisan}} statamic:xxx。4.4 Stache 处理部署的关键差异点Statamic 用 Stache 缓存内容entries、taxonomies、globals 等默认部署链中的两步保证了缓存一致性statamic:stache:clear清除旧 Stache 缓存statamic:stache:warm基于新 release 的内容重建 Stache。如果项目启用了静态页面缓存static caching可额外把statamic:static:clear、statamic:static:warm追加进自己的deploy任务见后文“自定义”小节。4.5 deploy:publish —— 发布 releasedeploy:publish同样来自 Common Reciperecipe/common.php包含 4 个子任务子任务作用deploy:symlink将current符号链接指向新 release原子切换deploy:unlock解锁部署deploy:cleanup清理旧的 release保留keep_releases个默认 10见 recipe/common.phpdeploy:success打印成功信息deploy:symlinkrecipe/deploy/symlink.php是实现零停机的关键优先使用mv -T {{deploy_path}}/release {{current_path}}完成原子替换若系统不支持mv --no-target-directory则退化为两步切换ln -nfs后删除 release 链接。由于切换符号链接是瞬时操作整个部署期间服务始终可用若新版本出现问题还可通过 Deployer 的rollback任务回滚到上一版本。五、在真实项目中使用 Statamic Recipe5.1 最小可用配置在项目根目录创建deploy.php?php namespace Deployer; require recipe/statamic.php; // 主机配置 host(production) -setHostname(your-server.example.com) -setRemoteUser(deployer) -set(labels, [stage production]) -set(deploy_path, ~/statamic-site); // 仓库 set(repository, gitgithub.com:your-name/your-statamic-site.git); // 部署时保持 5 个历史版本 set(keep_releases, 5);然后执行dep deploy productionDeployer 会按 4.x 节所述的任务链自动完成整个部署。5.2 自定义部署链按项目需求增删任务默认任务链不包含迁移与框架优化如果你的 Statamic 站点确实需要例如使用了数据库驱动的内容或需要预热静态缓存可以覆盖deploy任务task(deploy, [ deploy:prepare, deploy:vendors, artisan:storage:link, artisan:cache:clear, statamic:stache:clear, statamic:stache:warm, statamic:static:clear, // 清除静态页面缓存 statamic:static:warm, // 预热静态页面缓存 artisan:config:cache, // 需要时启用配置缓存 artisan:route:cache, // 需要时启用路由缓存 deploy:publish, ]);同理artisan:migrate若存在数据库迁移、artisan:optimize、artisan:reload均可按需加入。5.3 常用运维任务dep deploy production # 部署 dep rollback production # 回滚到上一版本 dep logs:app production # 跟踪应用日志依赖 log_files 配置 dep statamic:stache:refresh production # 手动刷新 Stache dep statamic:search:update production # 手动更新搜索索引 dep statamic:support:details production # 输出支持信息5.4 与 Laravel 部署的对照环节Laravel 默认Statamic 默认原因数据库迁移artisan:migrate无Statamic 内容默认以文件存储框架优化artisan:optimize无由开发者按需编排 config/route/view 缓存常驻服务重载artisan:reload无默认不假设使用 Octane 等方案Stache 重建无statamic:stache:clearwarm保证文件内容与缓存一致可写目录bootstrap/cache、storage追加storage/statamicStache 等运行期数据落盘于此六、源码路径速查Statamic Recipe 实现recipe/statamic.php官方文档 docs/recipe/statamic.md基类 Laravel Reciperecipe/laravel.php文档 docs/recipe/laravel.md含全部 artisan 封装任务migrate、cache、Horizon、Octane、Nova、Reverb、Pulse 等公共部署任务与配置recipe/common.php文档 docs/recipe/common.md共享目录/文件处理recipe/deploy/shared.php可写目录与权限模式recipe/deploy/writable.php原子符号链接切换recipe/deploy/symlink.phpComposer 依赖安装recipe/deploy/vendors.php完整任务文档索引docs/recipe/README.md七、总结Statamic Recipe 是 Deployer 对 StatamicLaravel 系 CMS部署需求的完整回答它复用 Laravel Recipe 的 artisan 封装与 Common Recipe 的 release 目录模型追加storage/statamic可写目录与 17 个 Statamic 专属任务并用「clear warm Stache」替换默认的迁移与优化步骤从而保证内容即文件的 Statamic 站点在零停机前提下安全发布、可随时回滚。理解本文的任务链与配置继承关系后你既可以直接dep deploy开箱即用也可以在此基础上自由编排出完全贴合业务的自定义部署流程。赞分享DevOpsCI/CDCLI开发工具运维【免费下载链接】deployerThe PHP deployment tool with support for popular frameworks out of the box项目地址https://gitcode.com/gh_mirrors/de/deployer点击查看免费下载相关推荐使用 Deployer 零停机部署 Contao 项目完整 Recipe 配置与实战指南使用 Deployer 零停机部署 Contao 项目完整 Recipe 配置与实战指南 导读 本文聚焦于 Deployer 项目中的 Contao 专属部署DevOpsCI/CDCLI开发工具运维使用 Deployer 零停机部署 CodeIgniter 4recipe/codeigniter4 完整实战指南使用 Deployer 零停机部署 CodeIgniter 4recipe/codeigniter4 完整实战指南 本指南讲解如何在 Deployer 中引入DevOpsCI/CDCLI开发工具运维使用 Deployer 的 Laravel 配方实现零停机部署recipe/laravel.php 全解析使用 Deployer 的 Laravel 配方实现零停机部署recipe/laravel.php 全解析 本指南以 Deployer 开源项目中的 LaraDevOpsCI/CDCLI开发工具运维创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表