vcs-patch-deploy 1.0.0
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/LICENSE +21 -0
- package/README.md +113 -0
- package/assets/php/apply-update.php +373 -0
- package/bin/patch-build.js +147 -0
- package/env.example +23 -0
- package/lib/backends/index.js +29 -0
- package/lib/backends/php.js +103 -0
- package/lib/build-patch.js +323 -0
- package/lib/config.js +159 -0
- package/lib/exclude.js +18 -0
- package/lib/git.js +157 -0
- package/lib/index.js +27 -0
- package/lib/marker.js +62 -0
- package/lib/svn.js +181 -0
- package/lib/zip.js +36 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 qingyuai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# vcs-patch-deploy
|
|
2
|
+
|
|
3
|
+
PHP 后端(ThinkPHP)增量部署包生成工具。对比 **SVN revision** 或 **Git commit** 差异,生成本地 zip,附带 `apply-update.php`,供堡垒机 / Web 终端上线。
|
|
4
|
+
|
|
5
|
+
> v1 仅内置 **PHP 后端**配置;Node/Java 等后端可在后续版本通过 `--backend` 扩展。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g vcs-patch-deploy
|
|
11
|
+
# 或项目内
|
|
12
|
+
npm install --save-dev vcs-patch-deploy
|
|
13
|
+
npx patch-build --help
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## 快速开始(ThinkPHP + SVN)
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
cd BackendSVN/trunk
|
|
20
|
+
|
|
21
|
+
# 首次:初始化 deploy 目录
|
|
22
|
+
patch-build --init --vcs=svn
|
|
23
|
+
|
|
24
|
+
# 创建基准(上次成功上线的 revision)
|
|
25
|
+
echo svn:430 > deploy/.last-deploy-marker
|
|
26
|
+
|
|
27
|
+
# 打包增量(430 -> 当前 HEAD)
|
|
28
|
+
patch-build --vcs=svn
|
|
29
|
+
|
|
30
|
+
# 产物
|
|
31
|
+
# deploy/out/backend-update-r{rev}.zip
|
|
32
|
+
# deploy/out/patch-r{rev}/
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Git 仓库
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
cd BackendSVN/trunk # 若 trunk 为 Git 仓库
|
|
39
|
+
|
|
40
|
+
echo git:abc123def... > deploy/.last-deploy-marker
|
|
41
|
+
patch-build --vcs=git
|
|
42
|
+
patch-build --vcs=git --from=v1.0.0 --to=HEAD
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 服务器应用
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
unzip -o backend-update-r467.zip -d patch-r467
|
|
49
|
+
cd patch-r467
|
|
50
|
+
|
|
51
|
+
php apply-update.php --check /usr/local/tool/THS/html/BackendSVN/Backend
|
|
52
|
+
php apply-update.php /usr/local/tool/THS/html/BackendServer/Backend
|
|
53
|
+
|
|
54
|
+
# 或环境变量
|
|
55
|
+
export PATCH_BACKEND_ROOT=/path/to/backend
|
|
56
|
+
php apply-update.php
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`apply-update.php` 行为:
|
|
60
|
+
|
|
61
|
+
- 按包内路径覆盖线上文件
|
|
62
|
+
- **不覆盖** `.env`
|
|
63
|
+
- **不自动删除** `MANIFEST-deleted.txt` 中的文件
|
|
64
|
+
- 成功后清理 `runtime/cache`,并尝试 `php think clear`
|
|
65
|
+
|
|
66
|
+
## CLI 参数
|
|
67
|
+
|
|
68
|
+
| 参数 | 说明 |
|
|
69
|
+
|------|------|
|
|
70
|
+
| `--backend=php` | 后端类型(默认 php) |
|
|
71
|
+
| `--vcs=svn\|git` | 强制 VCS 类型 |
|
|
72
|
+
| `--source=<dir>` | PHP 项目根 |
|
|
73
|
+
| `--deploy=<dir>` | deploy 目录 |
|
|
74
|
+
| `--from=<ref>` | 起始基准 |
|
|
75
|
+
| `--to=<ref>` | 结束目标 |
|
|
76
|
+
| `--no-zip` | 只生成 patch 目录 |
|
|
77
|
+
| `--mark-deployed` | 更新基准 marker |
|
|
78
|
+
| `--init` | 初始化 deploy 目录 |
|
|
79
|
+
|
|
80
|
+
## 不会打进包的内容
|
|
81
|
+
|
|
82
|
+
`.env`、`vendor/`、`runtime/`、`public/uploads/`、`deploy/out/` 等(见 `lib/backends/php.js`)。
|
|
83
|
+
|
|
84
|
+
若变更含 `composer.json` / `composer.lock`,上线后需在服务器执行 `composer install --no-dev`。
|
|
85
|
+
|
|
86
|
+
## 基准文件
|
|
87
|
+
|
|
88
|
+
| 文件 | 格式 |
|
|
89
|
+
|------|------|
|
|
90
|
+
| `deploy/.last-deploy-marker` | `svn:467` 或 `git:fullsha` |
|
|
91
|
+
| `deploy/.last-deploy-rev` | 兼容旧版 SVN(仅数字) |
|
|
92
|
+
|
|
93
|
+
**上线成功后再更新基准**,否则下次增量会漏文件。
|
|
94
|
+
|
|
95
|
+
## 编程式调用
|
|
96
|
+
|
|
97
|
+
```javascript
|
|
98
|
+
const { buildPatch, resolveConfig } = require('vcs-patch-deploy')
|
|
99
|
+
|
|
100
|
+
const config = resolveConfig({
|
|
101
|
+
backend: 'php',
|
|
102
|
+
vcs: 'svn',
|
|
103
|
+
sourceDir: 'D:/Work/web/newKJCG/BackendSVN/trunk',
|
|
104
|
+
fromRef: 430,
|
|
105
|
+
toRef: 467
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
buildPatch(config).then(console.log)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
MIT
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
declare(strict_types=1);
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 堡垒机增量部署:将 patch 包内文件覆盖到线上 Backend 根目录,并清理 ThinkPHP 缓存。
|
|
6
|
+
*
|
|
7
|
+
* 用法(Linux Web 终端,在解压后的 patch 目录内):
|
|
8
|
+
* php apply-update.php /www/kjcg-api
|
|
9
|
+
* 或设置环境变量:
|
|
10
|
+
* KJCG_BACKEND_ROOT=/www/kjcg-api php apply-update.php
|
|
11
|
+
*
|
|
12
|
+
* 安全:不会覆盖 .env;不会自动删除 MANIFEST-deleted.txt 中列出的文件。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** patch 包根目录(本脚本所在目录) */
|
|
16
|
+
const PATCH_ROOT = __DIR__;
|
|
17
|
+
|
|
18
|
+
/** 部署时跳过的文件名(相对路径 basename 匹配或完整相对路径) */
|
|
19
|
+
const SKIP_FILES = [
|
|
20
|
+
'apply-update.php',
|
|
21
|
+
'MANIFEST.txt',
|
|
22
|
+
'MANIFEST-deleted.txt',
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
/** 永不覆盖的相对路径(保护生产配置与运行时数据) */
|
|
26
|
+
const PROTECTED_RELATIVE = [
|
|
27
|
+
'.env',
|
|
28
|
+
'.env.local',
|
|
29
|
+
'.env.production',
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
/** 本地产物路径前缀(误打进 zip 时跳过,避免污染 Backend) */
|
|
33
|
+
const SKIP_PATH_PREFIXES = [
|
|
34
|
+
'deploy/out/',
|
|
35
|
+
'out/_verify/',
|
|
36
|
+
'out/patch-',
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 解析线上 Backend 根目录(含 app、config、public、vendor 的 trunk 根)。
|
|
41
|
+
*/
|
|
42
|
+
function resolveBackendRoot(): string
|
|
43
|
+
{
|
|
44
|
+
foreach (['PATCH_BACKEND_ROOT', 'KJCG_BACKEND_ROOT'] as $envKey) {
|
|
45
|
+
$fromEnv = getenv($envKey);
|
|
46
|
+
if (is_string($fromEnv) && $fromEnv !== '') {
|
|
47
|
+
return rtrim($fromEnv, "/\\");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
global $argv;
|
|
52
|
+
$flags = ['--check', '--dry-run'];
|
|
53
|
+
foreach ($argv as $i => $arg) {
|
|
54
|
+
if ($i === 0) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (in_array($arg, $flags, true)) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if ($arg !== '' && !str_starts_with($arg, '-')) {
|
|
61
|
+
return rtrim($arg, "/\\");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fwrite(STDERR, "用法: php apply-update.php [--check] <Backend根目录>\n");
|
|
66
|
+
fwrite(STDERR, "示例: php apply-update.php /usr/local/tool/THS/html/BackendSVN/Backend\n");
|
|
67
|
+
exit(1);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @return list<string> 相对路径,使用 /
|
|
72
|
+
*/
|
|
73
|
+
function listPatchFiles(): array
|
|
74
|
+
{
|
|
75
|
+
$files = [];
|
|
76
|
+
$iterator = new RecursiveIteratorIterator(
|
|
77
|
+
new RecursiveDirectoryIterator(PATCH_ROOT, FilesystemIterator::SKIP_DOTS)
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
foreach ($iterator as $fileInfo) {
|
|
81
|
+
if (!$fileInfo->isFile()) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
$abs = $fileInfo->getPathname();
|
|
86
|
+
$rel = substr($abs, strlen(PATCH_ROOT) + 1);
|
|
87
|
+
$relNorm = str_replace('\\', '/', $rel);
|
|
88
|
+
|
|
89
|
+
if (in_array(basename($relNorm), SKIP_FILES, true)) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (in_array($relNorm, SKIP_FILES, true)) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (in_array($relNorm, PROTECTED_RELATIVE, true)) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
$skipArtifact = false;
|
|
100
|
+
foreach (SKIP_PATH_PREFIXES as $prefix) {
|
|
101
|
+
if (str_starts_with($relNorm, $prefix) || str_starts_with($relNorm, 'out/')) {
|
|
102
|
+
$skipArtifact = true;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if ($skipArtifact) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
$files[] = $relNorm;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
sort($files);
|
|
114
|
+
|
|
115
|
+
return $files;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function detectPhpBinary(): string
|
|
119
|
+
{
|
|
120
|
+
if (defined('PHP_BINARY') && PHP_BINARY !== '') {
|
|
121
|
+
return PHP_BINARY;
|
|
122
|
+
}
|
|
123
|
+
$which = trim((string) shell_exec('command -v php 2>/dev/null'));
|
|
124
|
+
if ($which !== '') {
|
|
125
|
+
return $which;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return 'php';
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function formatOctalPerm(string $path): string
|
|
132
|
+
{
|
|
133
|
+
$mode = @fileperms($path);
|
|
134
|
+
if ($mode === false) {
|
|
135
|
+
return '?';
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return substr(sprintf('%o', $mode), -4);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 读取文件属主(Linux 下便于排查 Permission denied)。
|
|
143
|
+
*/
|
|
144
|
+
function formatFileOwner(string $path): string
|
|
145
|
+
{
|
|
146
|
+
if (!function_exists('posix_getpwuid')) {
|
|
147
|
+
return '';
|
|
148
|
+
}
|
|
149
|
+
$uid = @fileowner($path);
|
|
150
|
+
if ($uid === false) {
|
|
151
|
+
return '';
|
|
152
|
+
}
|
|
153
|
+
$info = posix_getpwuid($uid);
|
|
154
|
+
|
|
155
|
+
return is_array($info) ? (string) ($info['name'] ?? '') : '';
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 部署前检查目标是否可写;不可写时给出运维排查提示。
|
|
160
|
+
*/
|
|
161
|
+
function assertTargetWritable(string $dst, string $rel): void
|
|
162
|
+
{
|
|
163
|
+
$dir = dirname($dst);
|
|
164
|
+
if (!is_dir($dir)) {
|
|
165
|
+
if (!is_writable(dirname($dir)) && is_dir(dirname($dir))) {
|
|
166
|
+
$owner = formatFileOwner(dirname($dir));
|
|
167
|
+
$perm = formatOctalPerm(dirname($dir));
|
|
168
|
+
$who = function_exists('posix_getpwuid')
|
|
169
|
+
? (posix_getpwuid(posix_geteuid())['name'] ?? 'unknown')
|
|
170
|
+
: 'unknown';
|
|
171
|
+
throw new RuntimeException(
|
|
172
|
+
"目录不可写: {$rel}(父目录 {$dir} owner={$owner} perm={$perm},当前用户={$who})"
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (is_file($dst) && !is_writable($dst)) {
|
|
179
|
+
$owner = formatFileOwner($dst);
|
|
180
|
+
$perm = formatOctalPerm($dst);
|
|
181
|
+
$who = function_exists('posix_getpwuid')
|
|
182
|
+
? (posix_getpwuid(posix_geteuid())['name'] ?? 'unknown')
|
|
183
|
+
: 'unknown';
|
|
184
|
+
throw new RuntimeException(
|
|
185
|
+
"Permission denied(owner={$owner} perm={$perm},当前用户={$who})"
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (!is_file($dst) && !is_writable($dir)) {
|
|
190
|
+
$owner = formatFileOwner($dir);
|
|
191
|
+
$perm = formatOctalPerm($dir);
|
|
192
|
+
$who = function_exists('posix_getpwuid')
|
|
193
|
+
? (posix_getpwuid(posix_geteuid())['name'] ?? 'unknown')
|
|
194
|
+
: 'unknown';
|
|
195
|
+
throw new RuntimeException(
|
|
196
|
+
"目录不可写: {$dir}(owner={$owner} perm={$perm},当前用户={$who})"
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function ensureParentDir(string $path): void
|
|
202
|
+
{
|
|
203
|
+
$dir = dirname($path);
|
|
204
|
+
if ($dir !== '' && $dir !== '.' && !is_dir($dir)) {
|
|
205
|
+
if (!mkdir($dir, 0755, true) && !is_dir($dir)) {
|
|
206
|
+
throw new RuntimeException("无法创建目录: {$dir}");
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function clearRuntimeCache(string $backendRoot): void
|
|
212
|
+
{
|
|
213
|
+
$cacheDir = $backendRoot . '/runtime/cache';
|
|
214
|
+
if (!is_dir($cacheDir)) {
|
|
215
|
+
echo "跳过清缓存(目录不存在): runtime/cache\n";
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
$items = scandir($cacheDir);
|
|
220
|
+
if ($items === false) {
|
|
221
|
+
echo "警告: 无法读取 runtime/cache\n";
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
$removed = 0;
|
|
226
|
+
foreach ($items as $name) {
|
|
227
|
+
if ($name === '.' || $name === '..') {
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
$path = $cacheDir . '/' . $name;
|
|
231
|
+
if (is_file($path)) {
|
|
232
|
+
unlink($path);
|
|
233
|
+
$removed++;
|
|
234
|
+
} elseif (is_dir($path)) {
|
|
235
|
+
// 递归删除子目录(ThinkPHP 缓存偶见子文件夹)
|
|
236
|
+
$it = new RecursiveIteratorIterator(
|
|
237
|
+
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
|
|
238
|
+
RecursiveIteratorIterator::CHILD_FIRST
|
|
239
|
+
);
|
|
240
|
+
foreach ($it as $fi) {
|
|
241
|
+
$fi->isDir() ? rmdir($fi->getPathname()) : unlink($fi->getPathname());
|
|
242
|
+
}
|
|
243
|
+
rmdir($path);
|
|
244
|
+
$removed++;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
echo "已清理 runtime/cache({$removed} 项)\n";
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function tryThinkClear(string $backendRoot): void
|
|
252
|
+
{
|
|
253
|
+
$think = $backendRoot . '/think';
|
|
254
|
+
if (!is_file($think)) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
$cmd = 'php ' . escapeshellarg($think) . ' clear 2>&1';
|
|
259
|
+
$output = [];
|
|
260
|
+
$code = 0;
|
|
261
|
+
exec($cmd, $output, $code);
|
|
262
|
+
if ($code === 0) {
|
|
263
|
+
echo "已执行 php think clear\n";
|
|
264
|
+
} else {
|
|
265
|
+
echo "提示: php think clear 未成功(可忽略,已手动清 runtime/cache)\n";
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// --- main ---
|
|
270
|
+
|
|
271
|
+
$backendRoot = resolveBackendRoot();
|
|
272
|
+
|
|
273
|
+
if (!is_dir($backendRoot)) {
|
|
274
|
+
fwrite(STDERR, "Backend 根目录不存在: {$backendRoot}\n");
|
|
275
|
+
exit(1);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (!is_dir($backendRoot . '/app')) {
|
|
279
|
+
fwrite(STDERR, "目标不像 Backend 根目录(缺少 app/): {$backendRoot}\n");
|
|
280
|
+
exit(1);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
$dryRun = in_array('--check', $argv ?? [], true) || in_array('--dry-run', $argv ?? [], true);
|
|
284
|
+
|
|
285
|
+
$runUser = function_exists('posix_getpwuid')
|
|
286
|
+
? (posix_getpwuid(posix_geteuid())['name'] ?? 'unknown')
|
|
287
|
+
: 'unknown';
|
|
288
|
+
|
|
289
|
+
echo "PATCH_ROOT: " . PATCH_ROOT . "\n";
|
|
290
|
+
echo "BACKEND_ROOT: {$backendRoot}\n";
|
|
291
|
+
echo "RUN_AS: {$runUser}\n";
|
|
292
|
+
if ($dryRun) {
|
|
293
|
+
echo "MODE: check-only(不写入文件)\n";
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
$files = listPatchFiles();
|
|
297
|
+
if ($files === []) {
|
|
298
|
+
echo "patch 包内无待部署文件。\n";
|
|
299
|
+
exit(0);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
$ok = 0;
|
|
303
|
+
$fail = 0;
|
|
304
|
+
$blocked = [];
|
|
305
|
+
|
|
306
|
+
foreach ($files as $rel) {
|
|
307
|
+
$src = PATCH_ROOT . '/' . str_replace('/', DIRECTORY_SEPARATOR, $rel);
|
|
308
|
+
$dst = $backendRoot . '/' . str_replace('/', DIRECTORY_SEPARATOR, $rel);
|
|
309
|
+
|
|
310
|
+
try {
|
|
311
|
+
assertTargetWritable($dst, $rel);
|
|
312
|
+
if ($dryRun) {
|
|
313
|
+
echo "OK {$rel}(可写)\n";
|
|
314
|
+
$ok++;
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
ensureParentDir($dst);
|
|
319
|
+
if (!copy($src, $dst)) {
|
|
320
|
+
throw new RuntimeException('copy 失败');
|
|
321
|
+
}
|
|
322
|
+
echo "OK {$rel}\n";
|
|
323
|
+
$ok++;
|
|
324
|
+
} catch (Throwable $e) {
|
|
325
|
+
echo "FAIL {$rel} — {$e->getMessage()}\n";
|
|
326
|
+
$fail++;
|
|
327
|
+
$blocked[] = $rel;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if ($fail > 0) {
|
|
332
|
+
$phpBin = detectPhpBinary();
|
|
333
|
+
echo "\n--- 权限排查 ---\n";
|
|
334
|
+
echo "当前终端用户「{$runUser}」无法覆盖 app/ 下 PHP 文件(runtime/cache 可写不代表源码可写)。\n";
|
|
335
|
+
echo "源码属主与 PHP-FPM 不一致时,THS 常见:文件 UID 48 无 passwd 条目,而 php-fpm 以 nyncb 运行。\n";
|
|
336
|
+
echo "建议: sudo chown -R {$runUser}:{$runUser} {$backendRoot}/app 后,再以 {$runUser} 执行本脚本;或 sudo cp 覆盖。\n";
|
|
337
|
+
echo "请在 Backend 根目录执行:\n";
|
|
338
|
+
echo " whoami\n";
|
|
339
|
+
echo " ls -ln app/service/admin/AdminAuthService.php\n";
|
|
340
|
+
echo " ps aux | grep php-fpm | head -3\n";
|
|
341
|
+
echo " command -v php\n";
|
|
342
|
+
echo "常见处理方式(择一,需运维配合):\n";
|
|
343
|
+
echo " 1) sudo chown -R {$runUser}:{$runUser} {$backendRoot}/app && {$phpBin} apply-update.php {$backendRoot}\n";
|
|
344
|
+
echo " 2) sudo cp -f app/service/... {$backendRoot}/app/service/...(在 patch 目录内)\n";
|
|
345
|
+
echo " 3) 请运维在 THS 控制台代为覆盖 patch 内文件\n";
|
|
346
|
+
echo "注意: 本 THS 环境通常无 www/nginx/apache 用户,勿用 sudo -u nginx。\n";
|
|
347
|
+
if ($blocked !== []) {
|
|
348
|
+
echo "\n未写入文件数: " . count($blocked) . "\n";
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if ($dryRun) {
|
|
353
|
+
echo "\n检查完成: 可写 {$ok},不可写 {$fail}\n";
|
|
354
|
+
exit($fail > 0 ? 1 : 0);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if ($fail > 0) {
|
|
358
|
+
echo "\n存在写入失败,已跳过清缓存(避免未更新代码却刷新缓存)。\n";
|
|
359
|
+
echo "完成: 成功 {$ok},失败 {$fail}\n";
|
|
360
|
+
exit(1);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
clearRuntimeCache($backendRoot);
|
|
364
|
+
tryThinkClear($backendRoot);
|
|
365
|
+
|
|
366
|
+
$deletedList = PATCH_ROOT . '/MANIFEST-deleted.txt';
|
|
367
|
+
if (is_file($deletedList)) {
|
|
368
|
+
echo "\n注意: 存在 MANIFEST-deleted.txt,未自动删除线上文件,请人工确认:\n";
|
|
369
|
+
echo file_get_contents($deletedList);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
echo "\n完成: 成功 {$ok},失败 {$fail}\n";
|
|
373
|
+
exit($fail > 0 ? 1 : 0);
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* PHP 后端增量部署包 CLI — SVN / Git 差异打包,附带 apply-update.php
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const path = require('path')
|
|
8
|
+
const chalk = require('chalk')
|
|
9
|
+
const { buildPatch, resolveConfig, initDeployDir, detectVcs } = require('../lib/index')
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
require('dotenv').config({ path: path.resolve(process.cwd(), '.env') })
|
|
13
|
+
} catch {
|
|
14
|
+
// optional
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function showHelp () {
|
|
18
|
+
console.log(`
|
|
19
|
+
${chalk.cyan.bold('vcs-patch-deploy')} — PHP 后端增量部署 zip 打包工具
|
|
20
|
+
|
|
21
|
+
${chalk.yellow('用法:')}
|
|
22
|
+
patch-build [选项]
|
|
23
|
+
|
|
24
|
+
${chalk.yellow('选项:')}
|
|
25
|
+
--backend=php 后端类型(v1 仅支持 php,默认 php)
|
|
26
|
+
--vcs=svn|git 版本控制系统(默认自动检测)
|
|
27
|
+
--source=<目录> PHP 项目根(SVN 常为 trunk,Git 为仓库根)
|
|
28
|
+
--deploy=<目录> deploy 配置目录(默认 <source>/deploy)
|
|
29
|
+
--from=<ref> 起始基准(SVN revision / Git commit,不含)
|
|
30
|
+
--to=<ref> 结束目标(默认 HEAD / 当前 revision)
|
|
31
|
+
--no-zip 仅生成 patch 目录
|
|
32
|
+
--mark-deployed 更新 deploy/.last-deploy-marker
|
|
33
|
+
--init 初始化 deploy 目录结构
|
|
34
|
+
--skip-validation 跳过 PHP 项目结构校验
|
|
35
|
+
--zip-prefix=<前缀> zip 前缀(默认 backend-update)
|
|
36
|
+
-h, --help 显示帮助
|
|
37
|
+
|
|
38
|
+
${chalk.yellow('环境变量:')}
|
|
39
|
+
PATCH_BACKEND=php 后端类型
|
|
40
|
+
PATCH_VCS svn | git
|
|
41
|
+
PATCH_SOURCE_DIR 源码根
|
|
42
|
+
PATCH_DEPLOY_DIR deploy 目录
|
|
43
|
+
PATCH_ZIP_PREFIX zip 前缀
|
|
44
|
+
|
|
45
|
+
${chalk.yellow('ThinkPHP + SVN(BackendSVN/trunk):')}
|
|
46
|
+
cd BackendSVN/trunk
|
|
47
|
+
patch-build --vcs=svn
|
|
48
|
+
|
|
49
|
+
${chalk.yellow('ThinkPHP + Git:')}
|
|
50
|
+
cd BackendSVN/trunk
|
|
51
|
+
patch-build --vcs=git --from=<commit> --to=HEAD
|
|
52
|
+
|
|
53
|
+
${chalk.yellow('服务器应用:')}
|
|
54
|
+
php apply-update.php --check /path/to/backend
|
|
55
|
+
php apply-update.php /path/to/backend
|
|
56
|
+
# 或: PATCH_BACKEND_ROOT=/path/to/backend php apply-update.php
|
|
57
|
+
`)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function parseArgs () {
|
|
61
|
+
const args = process.argv.slice(2)
|
|
62
|
+
const opts = { showHelp: false }
|
|
63
|
+
|
|
64
|
+
for (const arg of args) {
|
|
65
|
+
if (arg === '--help' || arg === '-h') opts.showHelp = true
|
|
66
|
+
else if (arg === '--no-zip') opts.noZip = true
|
|
67
|
+
else if (arg === '--mark-deployed') opts.markDeployed = true
|
|
68
|
+
else if (arg === '--init') opts.init = true
|
|
69
|
+
else if (arg === '--skip-validation') opts.skipValidation = true
|
|
70
|
+
else if (arg.startsWith('--backend=')) opts.backend = arg.split('=')[1]
|
|
71
|
+
else if (arg.startsWith('--vcs=')) opts.vcs = arg.split('=')[1]
|
|
72
|
+
else if (arg.startsWith('--source=')) opts.sourceDir = arg.split('=')[1]
|
|
73
|
+
else if (arg.startsWith('--deploy=')) opts.deployDir = arg.split('=')[1]
|
|
74
|
+
else if (arg.startsWith('--from=')) opts.fromRef = arg.split('=')[1]
|
|
75
|
+
else if (arg.startsWith('--to=')) opts.toRef = arg.split('=')[1]
|
|
76
|
+
else if (arg.startsWith('--zip-prefix=')) opts.zipPrefix = arg.split('=')[1]
|
|
77
|
+
else console.warn(chalk.yellow(`未知参数: ${arg}`))
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return opts
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function main () {
|
|
84
|
+
const opts = parseArgs()
|
|
85
|
+
if (opts.showHelp) {
|
|
86
|
+
showHelp()
|
|
87
|
+
process.exit(0)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const config = resolveConfig({
|
|
91
|
+
backend: opts.backend,
|
|
92
|
+
vcs: opts.vcs,
|
|
93
|
+
sourceDir: opts.sourceDir,
|
|
94
|
+
deployDir: opts.deployDir,
|
|
95
|
+
fromRef: opts.fromRef ?? null,
|
|
96
|
+
toRef: opts.toRef ?? null,
|
|
97
|
+
noZip: opts.noZip,
|
|
98
|
+
markDeployed: opts.markDeployed,
|
|
99
|
+
skipValidation: opts.skipValidation,
|
|
100
|
+
zipPrefix: opts.zipPrefix,
|
|
101
|
+
pkgRoot: path.join(__dirname, '..')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
if (opts.init) {
|
|
105
|
+
initDeployDir(config.deployDir, config.vcs)
|
|
106
|
+
console.log(chalk.green(`已初始化 deploy 目录: ${config.deployDir}`))
|
|
107
|
+
if (!opts.fromRef && !opts.toRef && !opts.markDeployed) {
|
|
108
|
+
process.exit(0)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
console.log(chalk.cyan(`后端: ${config.backend} VCS: ${config.vcs}`))
|
|
113
|
+
console.log(chalk.gray(`源码: ${config.sourceDir}`))
|
|
114
|
+
console.log(chalk.gray(`deploy: ${config.deployDir}`))
|
|
115
|
+
|
|
116
|
+
const result = await buildPatch(config)
|
|
117
|
+
|
|
118
|
+
if (result.skipped) {
|
|
119
|
+
console.log(chalk.yellow(result.message))
|
|
120
|
+
} else {
|
|
121
|
+
console.log(chalk.green(result.message))
|
|
122
|
+
console.log(chalk.green(` 变更文件: ${result.fileCount} 删除: ${result.deletedCount}`))
|
|
123
|
+
if (result.patchDir) console.log(chalk.green(` patch 目录: ${result.patchDir}`))
|
|
124
|
+
if (result.zipPath) console.log(chalk.green(` zip: ${result.zipPath} (${result.zipSize} bytes)`))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (result.warnings?.length) {
|
|
128
|
+
console.log(chalk.yellow('\n警告:'))
|
|
129
|
+
result.warnings.forEach(w => console.log(chalk.yellow(` - ${w}`)))
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (result.markDeployedHint) {
|
|
133
|
+
console.log(chalk.cyan('\n上线成功后更新基准:'))
|
|
134
|
+
console.log(chalk.cyan(` ${result.markDeployedHint}`))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (!result.skipped && !opts.markDeployed) {
|
|
138
|
+
console.log(chalk.cyan('\n下一步: 上传 zip → 解压 → php apply-update.php <Backend根目录>'))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
process.exit(result.success ? 0 : 1)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
main().catch(error => {
|
|
145
|
+
console.error(chalk.red(`错误: ${error.message}`))
|
|
146
|
+
process.exit(1)
|
|
147
|
+
})
|
package/env.example
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# PHP 后端增量部署(vcs-patch-deploy)
|
|
2
|
+
|
|
3
|
+
# ---------- 后端与 VCS ----------
|
|
4
|
+
# 后端类型(v1 仅 php)
|
|
5
|
+
PATCH_BACKEND=php
|
|
6
|
+
# svn | git;留空则自动检测
|
|
7
|
+
# PATCH_VCS=svn
|
|
8
|
+
|
|
9
|
+
# PHP 项目根(ThinkPHP 含 app/ config/ 的 trunk 或 Git 仓库根)
|
|
10
|
+
# PATCH_SOURCE_DIR=D:/Work/web/newKJCG/BackendSVN/trunk
|
|
11
|
+
|
|
12
|
+
# deploy 配置目录(含 .last-deploy-marker、out/)
|
|
13
|
+
# PATCH_DEPLOY_DIR=D:/Work/web/newKJCG/BackendSVN/trunk/deploy
|
|
14
|
+
|
|
15
|
+
# zip 文件名前缀
|
|
16
|
+
# PATCH_ZIP_PREFIX=backend-update
|
|
17
|
+
|
|
18
|
+
# 跳过 PHP 项目结构校验(非标准目录时使用)
|
|
19
|
+
# PATCH_SKIP_VALIDATION=false
|
|
20
|
+
|
|
21
|
+
# ---------- 工具路径(可选)----------
|
|
22
|
+
# SVN_EXE=C:/Program Files/TortoiseSVN/bin/svn.exe
|
|
23
|
+
# GIT_EXE=C:/Program Files/Git/bin/git.exe
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 后端类型注册表 — v1 仅支持 PHP
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const php = require('./php')
|
|
6
|
+
|
|
7
|
+
/** @type {Record<string, typeof php>} */
|
|
8
|
+
const BACKENDS = {
|
|
9
|
+
php
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} [backendId]
|
|
14
|
+
* @returns {typeof php}
|
|
15
|
+
*/
|
|
16
|
+
function getBackend (backendId = 'php') {
|
|
17
|
+
const id = (backendId || 'php').toLowerCase()
|
|
18
|
+
const backend = BACKENDS[id]
|
|
19
|
+
if (!backend) {
|
|
20
|
+
const supported = Object.keys(BACKENDS).join(', ')
|
|
21
|
+
throw new Error(`不支持的后端类型「${backendId}」,当前支持: ${supported}`)
|
|
22
|
+
}
|
|
23
|
+
return backend
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = {
|
|
27
|
+
BACKENDS,
|
|
28
|
+
getBackend
|
|
29
|
+
}
|