vcs-patch-deploy 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/README.md +63 -5
- package/assets/php/apply-update.php +354 -13
- package/bin/patch-build.js +21 -1
- package/bin/patch-extract.js +127 -0
- package/env.example +15 -0
- package/lib/backends/php.js +16 -5
- package/lib/build-patch.js +135 -51
- package/lib/cli-args.js +63 -0
- package/lib/config.js +8 -1
- package/lib/extract-patch.js +191 -0
- package/lib/file-filter.js +147 -0
- package/lib/git.js +40 -16
- package/lib/index.js +11 -0
- package/lib/manifest.js +47 -0
- package/lib/version-registry.js +137 -0
- package/lib/zip.js +4 -1
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.1.1
|
|
4
|
+
|
|
5
|
+
### 修复
|
|
6
|
+
|
|
7
|
+
- `apply-update.php`:兼容 ThinkPHP 5(`application/`)及 patch 路径推断,不再强制要求 `app/`
|
|
8
|
+
- `validatePhpBackend`:识别 `application/` 旧版目录结构
|
|
9
|
+
|
|
10
|
+
## Unreleased
|
|
11
|
+
|
|
12
|
+
## 1.1.0
|
|
13
|
+
|
|
14
|
+
### 新增
|
|
15
|
+
|
|
16
|
+
- **版本号部署**:`patch-build --version=1.2.3` 写入 `VERSION.txt` 与 `deploy/versions.json`
|
|
17
|
+
- **单独文件提取**:`patch-extract --version=1.2.3 --only=<文件>` 从完整包提取子集
|
|
18
|
+
- **按文件打包/部署**:`--only` / `--file-list`(打包端)、`--files` / `PATCH_DEPLOY_FILES`(服务器端)
|
|
19
|
+
- 新增 CLI:`patch-extract`
|
|
20
|
+
- 单元测试(`npm test`)与 GitHub Actions CI
|
|
21
|
+
|
|
22
|
+
### 改进
|
|
23
|
+
|
|
24
|
+
- `apply-update.php`:部署前自动备份;优先读 MANIFEST 减少目录扫描
|
|
25
|
+
- `versions.json`:原子写入、损坏时告警、子集版本去重
|
|
26
|
+
- `build-patch`:staging 目录构建,失败不破坏旧 patch;SVN revision NaN 校验
|
|
27
|
+
- `git.js`:Git 未安装时 `detectVcs` 不再崩溃;支持类型变更 diff
|
|
28
|
+
- zip 默认压缩级别 6(可通过 `PATCH_ZIP_LEVEL` 调整)
|
|
29
|
+
|
|
3
30
|
## 1.0.1
|
|
4
31
|
|
|
5
32
|
- 文档与示例改用通用占位路径,移除项目专有目录名
|
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
PHP 后端(ThinkPHP)增量部署包生成工具。对比 **SVN revision** 或 **Git commit** 差异,生成本地 zip,附带 `apply-update.php`,供堡垒机 / Web 终端上线。
|
|
4
4
|
|
|
5
|
-
- npm
|
|
6
|
-
- GitHub
|
|
5
|
+
- npm:<https://www.npmjs.com/package/vcs-patch-deploy>
|
|
6
|
+
- GitHub:<https://github.com/QINGYUAI/vcs-patch-deploy>
|
|
7
7
|
|
|
8
8
|
> v1 仅内置 **PHP 后端**配置;Node/Java 等后端可在后续版本通过 `--backend` 扩展。
|
|
9
9
|
|
|
@@ -18,7 +18,7 @@ npx patch-build --help
|
|
|
18
18
|
|
|
19
19
|
## 快速开始(ThinkPHP + SVN)
|
|
20
20
|
|
|
21
|
-
在 **PHP 项目根目录**(含 `app/`、`config/` 或 `think`)执行:
|
|
21
|
+
在 **PHP 项目根目录**(含 `app/`、`application/`、`config/` 或 `think`)执行:
|
|
22
22
|
|
|
23
23
|
```bash
|
|
24
24
|
cd /path/to/your-php-api
|
|
@@ -49,6 +49,37 @@ patch-build --vcs=git
|
|
|
49
49
|
patch-build --vcs=git --from=v1.0.0 --to=HEAD
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
## 版本号与单独文件部署
|
|
53
|
+
|
|
54
|
+
为每次上线打上**安装版本号**,并支持从完整包中**提取部分文件**单独部署:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
# 1. 打完整版本包(写入 VERSION.txt、deploy/versions.json)
|
|
58
|
+
patch-build --vcs=svn --version=1.2.3
|
|
59
|
+
|
|
60
|
+
# 2. 打包时只包含指定文件(从本次增量中筛选)
|
|
61
|
+
patch-build --vcs=svn --version=1.2.3 --only=app/service/Foo.php,app/controller/Bar.php
|
|
62
|
+
patch-build --vcs=svn --version=1.2.3 --file-list=hotfix-files.txt
|
|
63
|
+
|
|
64
|
+
# 3. 从已打好的 v1.2.3 完整包中提取子集(无需重新 diff)
|
|
65
|
+
patch-extract --version=1.2.3 --only=app/service/Foo.php
|
|
66
|
+
patch-extract --list-versions
|
|
67
|
+
|
|
68
|
+
# 产物示例
|
|
69
|
+
# deploy/out/patch-v1.2.3/
|
|
70
|
+
# deploy/out/backend-update-v1.2.3.zip
|
|
71
|
+
# deploy/versions.json
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
服务器上也可在完整 patch 包内**只部署部分文件**:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
php apply-update.php --files=app/service/Foo.php /path/to/production-api
|
|
78
|
+
php apply-update.php --file-list=deploy-files.txt /path/to/production-api
|
|
79
|
+
export PATCH_DEPLOY_FILES=app/service/Foo.php,app/controller/Bar.php
|
|
80
|
+
php apply-update.php /path/to/production-api
|
|
81
|
+
```
|
|
82
|
+
|
|
52
83
|
## 服务器应用
|
|
53
84
|
|
|
54
85
|
```bash
|
|
@@ -66,14 +97,26 @@ php apply-update.php
|
|
|
66
97
|
`apply-update.php` 行为:
|
|
67
98
|
|
|
68
99
|
- 按包内路径覆盖线上文件
|
|
100
|
+
- **覆盖前备份** 线上已有文件到 `Backend/.deploy-backups/<时间戳>/`(保持相对路径)
|
|
69
101
|
- **不覆盖** `.env`
|
|
70
102
|
- **不自动删除** `MANIFEST-deleted.txt` 中的文件
|
|
71
103
|
- 成功后清理 `runtime/cache`,并尝试 `php think clear`
|
|
72
104
|
|
|
105
|
+
备份相关:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
# 默认开启备份;跳过备份
|
|
109
|
+
php apply-update.php --no-backup /path/to/production-api
|
|
110
|
+
|
|
111
|
+
# 自定义备份根目录(仍会在其下创建 <时间戳>/ 子目录)
|
|
112
|
+
export PATCH_BACKUP_DIR=/data/backups/my-api
|
|
113
|
+
php apply-update.php /path/to/production-api
|
|
114
|
+
```
|
|
115
|
+
|
|
73
116
|
## CLI 参数
|
|
74
117
|
|
|
75
118
|
| 参数 | 说明 |
|
|
76
|
-
|
|
119
|
+
| ------ | ------ |
|
|
77
120
|
| `--backend=php` | 后端类型(默认 php) |
|
|
78
121
|
| `--vcs=svn\|git` | 强制 VCS 类型 |
|
|
79
122
|
| `--source=<dir>` | PHP 项目根 |
|
|
@@ -83,6 +126,20 @@ php apply-update.php
|
|
|
83
126
|
| `--no-zip` | 只生成 patch 目录 |
|
|
84
127
|
| `--mark-deployed` | 更新基准 marker |
|
|
85
128
|
| `--init` | 初始化 deploy 目录 |
|
|
129
|
+
| `--version=<版本>` | 安装版本号(写入 VERSION.txt、versions.json) |
|
|
130
|
+
| `--only=<文件>` | 仅打包指定文件(逗号分隔) |
|
|
131
|
+
| `--file-list=<文件>` | 文件列表(每行一个相对路径) |
|
|
132
|
+
|
|
133
|
+
### patch-extract
|
|
134
|
+
|
|
135
|
+
| 参数 | 说明 |
|
|
136
|
+
| ------ | ------ |
|
|
137
|
+
| `--version=<版本>` | 从已注册版本提取 |
|
|
138
|
+
| `--from-patch=<目录>` | 直接指定源 patch 目录 |
|
|
139
|
+
| `--only=<文件>` | 要提取的文件 |
|
|
140
|
+
| `--file-list=<文件>` | 文件列表 |
|
|
141
|
+
| `--list-versions` | 列出 deploy/versions.json 中的版本 |
|
|
142
|
+
| `--register` | 将子集包注册到 versions.json |
|
|
86
143
|
|
|
87
144
|
## 不会打进包的内容
|
|
88
145
|
|
|
@@ -93,9 +150,10 @@ php apply-update.php
|
|
|
93
150
|
## 基准文件
|
|
94
151
|
|
|
95
152
|
| 文件 | 格式 |
|
|
96
|
-
|
|
153
|
+
| ------ | ------ |
|
|
97
154
|
| `deploy/.last-deploy-marker` | `svn:467` 或 `git:fullsha` |
|
|
98
155
|
| `deploy/.last-deploy-rev` | 兼容旧版 SVN 工具(仅数字) |
|
|
156
|
+
| `deploy/versions.json` | 版本号与 patch 目录映射 |
|
|
99
157
|
|
|
100
158
|
**上线成功后再更新基准**,否则下次增量会漏文件。
|
|
101
159
|
|
|
@@ -10,16 +10,21 @@ declare(strict_types=1);
|
|
|
10
10
|
* PATCH_BACKEND_ROOT=/path/to/production-api php apply-update.php
|
|
11
11
|
*
|
|
12
12
|
* 安全:不会覆盖 .env;不会自动删除 MANIFEST-deleted.txt 中列出的文件。
|
|
13
|
+
* 备份:覆盖前将线上已有文件复制到备份目录(默认 Backend/.deploy-backups/<时间戳>/)。
|
|
13
14
|
*/
|
|
14
15
|
|
|
15
16
|
/** patch 包根目录(本脚本所在目录) */
|
|
16
17
|
const PATCH_ROOT = __DIR__;
|
|
17
18
|
|
|
19
|
+
/** 默认备份目录名(位于 Backend 根下,可通过 PATCH_BACKUP_DIR 覆盖父路径) */
|
|
20
|
+
const BACKUP_DIR_BASENAME = '.deploy-backups';
|
|
21
|
+
|
|
18
22
|
/** 部署时跳过的文件名(相对路径 basename 匹配或完整相对路径) */
|
|
19
23
|
const SKIP_FILES = [
|
|
20
24
|
'apply-update.php',
|
|
21
25
|
'MANIFEST.txt',
|
|
22
26
|
'MANIFEST-deleted.txt',
|
|
27
|
+
'VERSION.txt',
|
|
23
28
|
];
|
|
24
29
|
|
|
25
30
|
/** 永不覆盖的相对路径(保护生产配置与运行时数据) */
|
|
@@ -49,7 +54,7 @@ function resolveBackendRoot(): string
|
|
|
49
54
|
}
|
|
50
55
|
|
|
51
56
|
global $argv;
|
|
52
|
-
$flags = ['--check', '--dry-run'];
|
|
57
|
+
$flags = ['--check', '--dry-run', '--no-backup'];
|
|
53
58
|
foreach ($argv as $i => $arg) {
|
|
54
59
|
if ($i === 0) {
|
|
55
60
|
continue;
|
|
@@ -57,21 +62,81 @@ function resolveBackendRoot(): string
|
|
|
57
62
|
if (in_array($arg, $flags, true)) {
|
|
58
63
|
continue;
|
|
59
64
|
}
|
|
65
|
+
if (str_starts_with($arg, '--files=') || str_starts_with($arg, '--file-list=')) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
60
68
|
if ($arg !== '' && !str_starts_with($arg, '-')) {
|
|
61
69
|
return rtrim($arg, "/\\");
|
|
62
70
|
}
|
|
63
71
|
}
|
|
64
72
|
|
|
65
|
-
fwrite(STDERR, "用法: php apply-update.php [--check] <Backend根目录>\n");
|
|
73
|
+
fwrite(STDERR, "用法: php apply-update.php [--check] [--files=<路径>] [--file-list=<列表>] <Backend根目录>\n");
|
|
66
74
|
fwrite(STDERR, "示例: php apply-update.php /path/to/production-api\n");
|
|
75
|
+
fwrite(STDERR, " php apply-update.php --files=app/service/Foo.php /path/to/production-api\n");
|
|
67
76
|
exit(1);
|
|
68
77
|
}
|
|
69
78
|
|
|
79
|
+
/**
|
|
80
|
+
* 优先从 MANIFEST.txt 读取文件列表,避免大 patch 全目录扫描
|
|
81
|
+
*
|
|
82
|
+
* @return list<string>|null
|
|
83
|
+
*/
|
|
84
|
+
function readManifestFileList(): ?array
|
|
85
|
+
{
|
|
86
|
+
$manifestPath = PATCH_ROOT . '/MANIFEST.txt';
|
|
87
|
+
if (!is_file($manifestPath)) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
$lines = file($manifestPath, FILE_IGNORE_NEW_LINES);
|
|
92
|
+
if ($lines === false) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** @var list<string> $files */
|
|
97
|
+
$files = [];
|
|
98
|
+
$inFiles = false;
|
|
99
|
+
|
|
100
|
+
foreach ($lines as $line) {
|
|
101
|
+
$trimmed = trim($line);
|
|
102
|
+
if ($trimmed === '# files') {
|
|
103
|
+
$inFiles = true;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if ($inFiles && str_starts_with($trimmed, '#')) {
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
if ($inFiles && $trimmed !== '') {
|
|
110
|
+
$files[] = str_replace('\\', '/', $trimmed);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return $files === [] ? null : $files;
|
|
115
|
+
}
|
|
116
|
+
|
|
70
117
|
/**
|
|
71
118
|
* @return list<string> 相对路径,使用 /
|
|
72
119
|
*/
|
|
73
120
|
function listPatchFiles(): array
|
|
74
121
|
{
|
|
122
|
+
$fromManifest = readManifestFileList();
|
|
123
|
+
if ($fromManifest !== null) {
|
|
124
|
+
/** @var list<string> $files */
|
|
125
|
+
$files = [];
|
|
126
|
+
foreach ($fromManifest as $rel) {
|
|
127
|
+
$abs = PATCH_ROOT . '/' . str_replace('/', DIRECTORY_SEPARATOR, $rel);
|
|
128
|
+
if (!is_file($abs)) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (in_array($rel, PROTECTED_RELATIVE, true)) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
$files[] = $rel;
|
|
135
|
+
}
|
|
136
|
+
sort($files);
|
|
137
|
+
return $files;
|
|
138
|
+
}
|
|
139
|
+
|
|
75
140
|
$files = [];
|
|
76
141
|
$iterator = new RecursiveIteratorIterator(
|
|
77
142
|
new RecursiveDirectoryIterator(PATCH_ROOT, FilesystemIterator::SKIP_DOTS)
|
|
@@ -115,6 +180,99 @@ function listPatchFiles(): array
|
|
|
115
180
|
return $files;
|
|
116
181
|
}
|
|
117
182
|
|
|
183
|
+
/**
|
|
184
|
+
* 解析 --files / --file-list / PATCH_DEPLOY_FILES,用于单独部署部分文件
|
|
185
|
+
*
|
|
186
|
+
* @return list<string>|null null 表示部署全部
|
|
187
|
+
*/
|
|
188
|
+
function resolveDeployFileFilter(): ?array
|
|
189
|
+
{
|
|
190
|
+
global $argv;
|
|
191
|
+
|
|
192
|
+
/** @var list<string> $selected */
|
|
193
|
+
$selected = [];
|
|
194
|
+
|
|
195
|
+
$fromEnv = getenv('PATCH_DEPLOY_FILES');
|
|
196
|
+
if (is_string($fromEnv) && $fromEnv !== '') {
|
|
197
|
+
foreach (explode(',', $fromEnv) as $item) {
|
|
198
|
+
$item = trim(str_replace('\\', '/', $item));
|
|
199
|
+
if ($item !== '') {
|
|
200
|
+
$selected[] = $item;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
foreach ($argv ?? [] as $arg) {
|
|
206
|
+
if (str_starts_with($arg, '--files=')) {
|
|
207
|
+
foreach (explode(',', substr($arg, 8)) as $item) {
|
|
208
|
+
$item = trim(str_replace('\\', '/', $item));
|
|
209
|
+
if ($item !== '') {
|
|
210
|
+
$selected[] = $item;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (str_starts_with($arg, '--file-list=')) {
|
|
215
|
+
$listPath = substr($arg, 12);
|
|
216
|
+
if (!is_file($listPath)) {
|
|
217
|
+
throw new RuntimeException("文件列表不存在: {$listPath}");
|
|
218
|
+
}
|
|
219
|
+
foreach (file($listPath, FILE_IGNORE_NEW_LINES) ?: [] as $line) {
|
|
220
|
+
$line = trim($line);
|
|
221
|
+
if ($line === '' || str_starts_with($line, '#')) {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
$selected[] = str_replace('\\', '/', $line);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if ($selected === []) {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return array_values(array_unique($selected));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 按筛选条件过滤待部署文件;支持目录前缀(以 / 结尾)
|
|
238
|
+
*
|
|
239
|
+
* @param list<string> $allFiles
|
|
240
|
+
* @param list<string> $selected
|
|
241
|
+
* @return array{matched: list<string>, missing: list<string>}
|
|
242
|
+
*/
|
|
243
|
+
function filterDeployFiles(array $allFiles, array $selected): array
|
|
244
|
+
{
|
|
245
|
+
/** @var array<string, true> $matchedMap */
|
|
246
|
+
$matchedMap = [];
|
|
247
|
+
/** @var list<string> $missing */
|
|
248
|
+
$missing = [];
|
|
249
|
+
|
|
250
|
+
foreach ($selected as $sel) {
|
|
251
|
+
if (str_ends_with($sel, '/')) {
|
|
252
|
+
$hits = array_values(array_filter($allFiles, static fn(string $f): bool => str_starts_with($f, $sel)));
|
|
253
|
+
if ($hits === []) {
|
|
254
|
+
$missing[] = $sel;
|
|
255
|
+
} else {
|
|
256
|
+
foreach ($hits as $hit) {
|
|
257
|
+
$matchedMap[$hit] = true;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (in_array($sel, $allFiles, true)) {
|
|
264
|
+
$matchedMap[$sel] = true;
|
|
265
|
+
} else {
|
|
266
|
+
$missing[] = $sel;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
$matched = array_keys($matchedMap);
|
|
271
|
+
sort($matched);
|
|
272
|
+
|
|
273
|
+
return ['matched' => $matched, 'missing' => $missing];
|
|
274
|
+
}
|
|
275
|
+
|
|
118
276
|
function detectPhpBinary(): string
|
|
119
277
|
{
|
|
120
278
|
if (defined('PHP_BINARY') && PHP_BINARY !== '') {
|
|
@@ -208,6 +366,53 @@ function ensureParentDir(string $path): void
|
|
|
208
366
|
}
|
|
209
367
|
}
|
|
210
368
|
|
|
369
|
+
/**
|
|
370
|
+
* 解析备份根目录:PATCH_BACKUP_DIR 优先,否则 Backend/.deploy-backups
|
|
371
|
+
*/
|
|
372
|
+
function resolveBackupRoot(string $backendRoot): string
|
|
373
|
+
{
|
|
374
|
+
$fromEnv = getenv('PATCH_BACKUP_DIR');
|
|
375
|
+
if (is_string($fromEnv) && $fromEnv !== '') {
|
|
376
|
+
return rtrim($fromEnv, "/\\");
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return $backendRoot . '/' . BACKUP_DIR_BASENAME;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* 创建本次部署的备份会话目录(按时间戳区分)
|
|
384
|
+
*/
|
|
385
|
+
function createBackupSessionDir(string $backupRoot): string
|
|
386
|
+
{
|
|
387
|
+
$timestamp = date('Y-m-d_His');
|
|
388
|
+
$sessionDir = $backupRoot . '/' . $timestamp;
|
|
389
|
+
if (!is_dir($sessionDir) && !mkdir($sessionDir, 0755, true) && !is_dir($sessionDir)) {
|
|
390
|
+
throw new RuntimeException("无法创建备份目录: {$sessionDir}");
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
return $sessionDir;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* 将被覆盖的线上文件复制到备份目录(保持相对路径结构)
|
|
398
|
+
*
|
|
399
|
+
* @return bool 是否执行了备份(目标文件不存在则 false)
|
|
400
|
+
*/
|
|
401
|
+
function backupExistingFile(string $dst, string $rel, string $backupSessionDir): bool
|
|
402
|
+
{
|
|
403
|
+
if (!is_file($dst)) {
|
|
404
|
+
return false;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
$backupPath = $backupSessionDir . '/' . str_replace('/', DIRECTORY_SEPARATOR, $rel);
|
|
408
|
+
ensureParentDir($backupPath);
|
|
409
|
+
if (!copy($dst, $backupPath)) {
|
|
410
|
+
throw new RuntimeException("备份失败: {$rel}");
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return true;
|
|
414
|
+
}
|
|
415
|
+
|
|
211
416
|
function clearRuntimeCache(string $backendRoot): void
|
|
212
417
|
{
|
|
213
418
|
$cacheDir = $backendRoot . '/runtime/cache';
|
|
@@ -255,7 +460,8 @@ function tryThinkClear(string $backendRoot): void
|
|
|
255
460
|
return;
|
|
256
461
|
}
|
|
257
462
|
|
|
258
|
-
$
|
|
463
|
+
$phpBin = detectPhpBinary();
|
|
464
|
+
$cmd = escapeshellarg($phpBin) . ' ' . escapeshellarg($think) . ' clear 2>&1';
|
|
259
465
|
$output = [];
|
|
260
466
|
$code = 0;
|
|
261
467
|
exec($cmd, $output, $code);
|
|
@@ -266,6 +472,77 @@ function tryThinkClear(string $backendRoot): void
|
|
|
266
472
|
}
|
|
267
473
|
}
|
|
268
474
|
|
|
475
|
+
/**
|
|
476
|
+
* 识别 Backend 根目录布局
|
|
477
|
+
* - ThinkPHP 6+:app/
|
|
478
|
+
* - ThinkPHP 5:application/
|
|
479
|
+
* - 兜底:config/、think、composer.json,或 patch 首级目录与线上对齐
|
|
480
|
+
*
|
|
481
|
+
* @param list<string> $patchFiles
|
|
482
|
+
* @return array{valid: bool, layout: string, markers: list<string>}
|
|
483
|
+
*/
|
|
484
|
+
function validateBackendRoot(string $backendRoot, array $patchFiles = []): array
|
|
485
|
+
{
|
|
486
|
+
/** @var list<string> $markers */
|
|
487
|
+
$markers = [];
|
|
488
|
+
|
|
489
|
+
if (is_dir($backendRoot . '/app')) {
|
|
490
|
+
$markers[] = 'app/';
|
|
491
|
+
}
|
|
492
|
+
if (is_dir($backendRoot . '/application')) {
|
|
493
|
+
$markers[] = 'application/';
|
|
494
|
+
}
|
|
495
|
+
if (is_dir($backendRoot . '/config')) {
|
|
496
|
+
$markers[] = 'config/';
|
|
497
|
+
}
|
|
498
|
+
if (is_file($backendRoot . '/think')) {
|
|
499
|
+
$markers[] = 'think';
|
|
500
|
+
}
|
|
501
|
+
if (is_file($backendRoot . '/composer.json')) {
|
|
502
|
+
$markers[] = 'composer.json';
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (is_dir($backendRoot . '/app')) {
|
|
506
|
+
return ['valid' => true, 'layout' => 'thinkphp6', 'markers' => $markers];
|
|
507
|
+
}
|
|
508
|
+
if (is_dir($backendRoot . '/application')) {
|
|
509
|
+
return ['valid' => true, 'layout' => 'thinkphp5', 'markers' => $markers];
|
|
510
|
+
}
|
|
511
|
+
if (is_file($backendRoot . '/think') || is_dir($backendRoot . '/config')) {
|
|
512
|
+
return ['valid' => true, 'layout' => 'thinkphp-legacy', 'markers' => $markers];
|
|
513
|
+
}
|
|
514
|
+
if (is_file($backendRoot . '/composer.json')) {
|
|
515
|
+
return ['valid' => true, 'layout' => 'php-composer', 'markers' => $markers];
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// 根据 patch 内路径首段推断(兼容非标准旧版目录)
|
|
519
|
+
/** @var array<string, true> $topLevels */
|
|
520
|
+
$topLevels = [];
|
|
521
|
+
foreach ($patchFiles as $rel) {
|
|
522
|
+
$parts = explode('/', $rel, 2);
|
|
523
|
+
if ($parts[0] !== '') {
|
|
524
|
+
$topLevels[$parts[0]] = true;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
/** @var list<string> $matchedTops */
|
|
528
|
+
$matchedTops = [];
|
|
529
|
+
foreach (array_keys($topLevels) as $top) {
|
|
530
|
+
$candidate = $backendRoot . '/' . str_replace('/', DIRECTORY_SEPARATOR, $top);
|
|
531
|
+
if (is_dir($candidate) || is_file($candidate)) {
|
|
532
|
+
$matchedTops[] = $top . '/';
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
if ($matchedTops !== []) {
|
|
536
|
+
return [
|
|
537
|
+
'valid' => true,
|
|
538
|
+
'layout' => 'patch-inferred',
|
|
539
|
+
'markers' => array_values(array_unique([...$markers, ...$matchedTops])),
|
|
540
|
+
];
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
return ['valid' => false, 'layout' => 'unknown', 'markers' => $markers];
|
|
544
|
+
}
|
|
545
|
+
|
|
269
546
|
// --- main ---
|
|
270
547
|
|
|
271
548
|
$backendRoot = resolveBackendRoot();
|
|
@@ -275,12 +552,28 @@ if (!is_dir($backendRoot)) {
|
|
|
275
552
|
exit(1);
|
|
276
553
|
}
|
|
277
554
|
|
|
278
|
-
|
|
279
|
-
|
|
555
|
+
$files = listPatchFiles();
|
|
556
|
+
if ($files === []) {
|
|
557
|
+
echo "patch 包内无待部署文件。\n";
|
|
558
|
+
exit(0);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
$rootCheck = validateBackendRoot($backendRoot, $files);
|
|
562
|
+
if (!$rootCheck['valid']) {
|
|
563
|
+
fwrite(STDERR, "目标不像 Backend 根目录(缺少 app/、application/ 或与 patch 对应的首级目录): {$backendRoot}\n");
|
|
564
|
+
if ($rootCheck['markers'] !== []) {
|
|
565
|
+
fwrite(STDERR, '已检测到: ' . implode(', ', $rootCheck['markers']) . "\n");
|
|
566
|
+
}
|
|
567
|
+
$sample = $files[0] ?? '';
|
|
568
|
+
if ($sample !== '') {
|
|
569
|
+
fwrite(STDERR, "patch 示例路径: {$sample},请确认 SITE_ROOT 指向该路径的父级项目根\n");
|
|
570
|
+
}
|
|
280
571
|
exit(1);
|
|
281
572
|
}
|
|
282
573
|
|
|
283
574
|
$dryRun = in_array('--check', $argv ?? [], true) || in_array('--dry-run', $argv ?? [], true);
|
|
575
|
+
$noBackup = in_array('--no-backup', $argv ?? [], true);
|
|
576
|
+
$backupEnabled = !$dryRun && !$noBackup;
|
|
284
577
|
|
|
285
578
|
$runUser = function_exists('posix_getpwuid')
|
|
286
579
|
? (posix_getpwuid(posix_geteuid())['name'] ?? 'unknown')
|
|
@@ -288,20 +581,49 @@ $runUser = function_exists('posix_getpwuid')
|
|
|
288
581
|
|
|
289
582
|
echo "PATCH_ROOT: " . PATCH_ROOT . "\n";
|
|
290
583
|
echo "BACKEND_ROOT: {$backendRoot}\n";
|
|
584
|
+
echo "LAYOUT: {$rootCheck['layout']}";
|
|
585
|
+
if ($rootCheck['markers'] !== []) {
|
|
586
|
+
echo ' (' . implode(', ', $rootCheck['markers']) . ')';
|
|
587
|
+
}
|
|
588
|
+
echo "\n";
|
|
291
589
|
echo "RUN_AS: {$runUser}\n";
|
|
292
590
|
if ($dryRun) {
|
|
293
591
|
echo "MODE: check-only(不写入文件)\n";
|
|
294
592
|
}
|
|
593
|
+
if ($noBackup) {
|
|
594
|
+
echo "BACKUP: disabled(--no-backup)\n";
|
|
595
|
+
} elseif ($backupEnabled) {
|
|
596
|
+
echo "BACKUP: enabled -> " . resolveBackupRoot($backendRoot) . "/<timestamp>/\n";
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
try {
|
|
600
|
+
$fileFilter = resolveDeployFileFilter();
|
|
601
|
+
} catch (Throwable $e) {
|
|
602
|
+
fwrite(STDERR, $e->getMessage() . "\n");
|
|
603
|
+
exit(1);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
if ($fileFilter !== null) {
|
|
607
|
+
$filterResult = filterDeployFiles($files, $fileFilter);
|
|
608
|
+
if ($filterResult['missing'] !== []) {
|
|
609
|
+
fwrite(STDERR, "以下文件不在 patch 包内:\n - " . implode("\n - ", $filterResult['missing']) . "\n");
|
|
610
|
+
exit(1);
|
|
611
|
+
}
|
|
612
|
+
$files = $filterResult['matched'];
|
|
613
|
+
echo "DEPLOY_FILTER: " . count($files) . " 个文件(单独部署模式)\n";
|
|
614
|
+
}
|
|
295
615
|
|
|
296
|
-
$files = listPatchFiles();
|
|
297
616
|
if ($files === []) {
|
|
298
|
-
echo "
|
|
617
|
+
echo "筛选后无待部署文件。\n";
|
|
299
618
|
exit(0);
|
|
300
619
|
}
|
|
301
620
|
|
|
302
621
|
$ok = 0;
|
|
303
622
|
$fail = 0;
|
|
304
623
|
$blocked = [];
|
|
624
|
+
$backedUp = 0;
|
|
625
|
+
/** @var string|null 本次部署备份会话目录 */
|
|
626
|
+
$backupSessionDir = null;
|
|
305
627
|
|
|
306
628
|
foreach ($files as $rel) {
|
|
307
629
|
$src = PATCH_ROOT . '/' . str_replace('/', DIRECTORY_SEPARATOR, $rel);
|
|
@@ -310,11 +632,23 @@ foreach ($files as $rel) {
|
|
|
310
632
|
try {
|
|
311
633
|
assertTargetWritable($dst, $rel);
|
|
312
634
|
if ($dryRun) {
|
|
313
|
-
|
|
635
|
+
$existsHint = is_file($dst) ? ',线上已有文件' : '';
|
|
636
|
+
echo "OK {$rel}(可写{$existsHint})\n";
|
|
314
637
|
$ok++;
|
|
315
638
|
continue;
|
|
316
639
|
}
|
|
317
640
|
|
|
641
|
+
// 覆盖前先备份线上旧文件
|
|
642
|
+
if ($backupEnabled && is_file($dst)) {
|
|
643
|
+
if ($backupSessionDir === null) {
|
|
644
|
+
$backupSessionDir = createBackupSessionDir(resolveBackupRoot($backendRoot));
|
|
645
|
+
echo "BACKUP_DIR: {$backupSessionDir}\n";
|
|
646
|
+
}
|
|
647
|
+
if (backupExistingFile($dst, $rel, $backupSessionDir)) {
|
|
648
|
+
$backedUp++;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
318
652
|
ensureParentDir($dst);
|
|
319
653
|
if (!copy($src, $dst)) {
|
|
320
654
|
throw new RuntimeException('copy 失败');
|
|
@@ -330,18 +664,19 @@ foreach ($files as $rel) {
|
|
|
330
664
|
|
|
331
665
|
if ($fail > 0) {
|
|
332
666
|
$phpBin = detectPhpBinary();
|
|
667
|
+
$sampleRel = $blocked[0] ?? ($files[0] ?? 'path/to/file.php');
|
|
333
668
|
echo "\n--- 权限排查 ---\n";
|
|
334
|
-
echo "当前终端用户「{$runUser}
|
|
669
|
+
echo "当前终端用户「{$runUser}」无法覆盖源码文件(runtime/cache 可写不代表源码可写)。\n";
|
|
335
670
|
echo "源码属主与 PHP-FPM 不一致时较常见:Web 终端用户与 php-fpm 运行用户不同。\n";
|
|
336
|
-
echo "建议: sudo chown -R {$runUser}:{$runUser} {$backendRoot}
|
|
671
|
+
echo "建议: sudo chown -R {$runUser}:{$runUser} {$backendRoot} 后,再以 {$runUser} 执行本脚本;或 sudo cp 覆盖。\n";
|
|
337
672
|
echo "请在 Backend 根目录执行:\n";
|
|
338
673
|
echo " whoami\n";
|
|
339
|
-
echo " ls -ln
|
|
674
|
+
echo " ls -ln {$sampleRel}\n";
|
|
340
675
|
echo " ps aux | grep php-fpm | head -3\n";
|
|
341
676
|
echo " command -v php\n";
|
|
342
677
|
echo "常见处理方式(择一,需运维配合):\n";
|
|
343
|
-
echo " 1) sudo chown -R {$runUser}:{$runUser} {$backendRoot}
|
|
344
|
-
echo " 2) sudo cp -f
|
|
678
|
+
echo " 1) sudo chown -R {$runUser}:{$runUser} {$backendRoot} && {$phpBin} apply-update.php {$backendRoot}\n";
|
|
679
|
+
echo " 2) sudo cp -f {$sampleRel} {$backendRoot}/{$sampleRel}(在 patch 目录内)\n";
|
|
345
680
|
echo " 3) 请运维在面板/堡垒机代为覆盖 patch 内文件\n";
|
|
346
681
|
echo "注意: 勿假设固定系统用户(如 nginx/www),以 whoami 与 ls -ln 实际属主为准。\n";
|
|
347
682
|
if ($blocked !== []) {
|
|
@@ -363,6 +698,12 @@ if ($fail > 0) {
|
|
|
363
698
|
clearRuntimeCache($backendRoot);
|
|
364
699
|
tryThinkClear($backendRoot);
|
|
365
700
|
|
|
701
|
+
if ($backupSessionDir !== null) {
|
|
702
|
+
$sampleRel = $files[0] ?? 'path/to/file.php';
|
|
703
|
+
echo "\n已备份 {$backedUp} 个旧文件至: {$backupSessionDir}\n";
|
|
704
|
+
echo "回滚示例: cp -f {$backupSessionDir}/{$sampleRel} {$backendRoot}/{$sampleRel}\n";
|
|
705
|
+
}
|
|
706
|
+
|
|
366
707
|
$deletedList = PATCH_ROOT . '/MANIFEST-deleted.txt';
|
|
367
708
|
if (is_file($deletedList)) {
|
|
368
709
|
echo "\n注意: 存在 MANIFEST-deleted.txt,未自动删除线上文件,请人工确认:\n";
|