@wdyy/skills 0.1.13 → 0.1.15
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/.well-known/skills/index.json +2 -2
- package/.well-known/skills/wdyy-api-standard/reference/api-his-sql.md +38 -0
- package/.well-known/skills/wdyy-deployment-standard/SKILL.md +20 -4
- package/.well-known/skills/wdyy-deployment-standard/agents/openai.yaml +2 -2
- package/.well-known/skills/wdyy-deployment-standard/reference/docker-delivery-rules.md +25 -14
- package/.well-known/skills/wdyy-deployment-standard/scripts/generate-deployment-files.mjs +44 -8
- package/.well-known/skills/wdyy-deployment-standard/scripts/generate-deployment-files.test.mjs +148 -2
- package/.well-known/skills/wdyy-deployment-standard/scripts/validate-deployment-package.mjs +39 -4
- package/.well-known/skills/wdyy-deployment-standard/scripts/validate-deployment-package.test.mjs +287 -6
- package/.well-known/skills/wdyy-deployment-standard/templates/backend.Dockerfile.template +4 -5
- package/.well-known/skills/wdyy-deployment-standard/templates/deploy.sh.template +89 -9
- package/.well-known/skills/wdyy-deployment-standard/templates/docker-compose.yml +12 -1
- package/.well-known/skills/wdyy-deployment-standard/templates/env.example.template +5 -1
- package/.well-known/skills/wdyy-deployment-standard/templates/frontend-container.conf.template +9 -0
- package/.well-known/skills/wdyy-deployment-standard/templates/frontend.Dockerfile.template +3 -0
- package/README.md +2 -2
- package/lib/wdyy-cli.js +2 -2
- package/package.json +1 -1
|
@@ -7,13 +7,13 @@
|
|
|
7
7
|
},
|
|
8
8
|
{
|
|
9
9
|
"name": "wdyy-deployment-standard",
|
|
10
|
-
"description": "为无 CI/CD
|
|
10
|
+
"description": "为无 CI/CD 的前后端项目生成并验证跨宿主机统一 Docker 交付:在 Linux、macOS Docker Desktop 或 Windows Docker Desktop + WSL2/Linux 容器模式中,使用本地已有基础镜像构建单一 linux/amd64 或 linux/arm64 双镜像包,并在平台匹配的 Linux engine 上以固定端口、容器名、重启策略和 CST(UTC+8)时间整栈运行。Use when 规划、实现、审查或执行跨构建机平台的人工 Docker 生产交付时。",
|
|
11
11
|
"files": ["SKILL.md", "agents/openai.yaml", "reference/docker-delivery-rules.md", "scripts/generate-deployment-files.mjs", "scripts/generate-deployment-files.test.mjs", "scripts/validate-deployment-package.mjs", "scripts/validate-deployment-package.test.mjs", "templates/backend.Dockerfile.template", "templates/frontend.Dockerfile.template", "templates/frontend-container.conf.template", "templates/env.example.template", "templates/deploy.sh.template", "templates/docker-compose.yml", "templates/dockerignore.template"]
|
|
12
12
|
},
|
|
13
13
|
{
|
|
14
14
|
"name": "wdyy-api-standard",
|
|
15
15
|
"description": "根据“获取XX数据”“使用XX接口”等具体需求,定位已登记的接口 reference,并按已确认的接口契约将调用方式写入程序。Use when 需要查询或实现一个已登记接口的数据调用时。",
|
|
16
|
-
"files": ["SKILL.md", "agents/openai.yaml", "reference/api-demo.md", "reference/api-sms.md"]
|
|
16
|
+
"files": ["SKILL.md", "agents/openai.yaml", "reference/api-demo.md", "reference/api-his-sql.md", "reference/api-sms.md"]
|
|
17
17
|
},
|
|
18
18
|
{
|
|
19
19
|
"name": "wdyy-bug-record",
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# 接口名称:api-his-sql
|
|
2
|
+
|
|
3
|
+
# 接口用途:执行 SQL 查询并返回查询结果
|
|
4
|
+
|
|
5
|
+
# 接口URL:http://172.26.1.64:8999/api/database/query
|
|
6
|
+
|
|
7
|
+
`POST` 请求,`Content-Type` 为 `application/json`。
|
|
8
|
+
|
|
9
|
+
# 入参格式:JSON
|
|
10
|
+
|
|
11
|
+
```json
|
|
12
|
+
{
|
|
13
|
+
"sql": "SELECT * FROM SS_User WHERE ssusr_name = '示例用户'",
|
|
14
|
+
"params": []
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
- `sql`:要执行的 SQL 查询语句。
|
|
19
|
+
- `params`:SQL 查询参数数组;示例 SQL 未使用参数,因此传入空数组。
|
|
20
|
+
|
|
21
|
+
# 返回值:JSON
|
|
22
|
+
|
|
23
|
+
```json
|
|
24
|
+
{
|
|
25
|
+
"data": [
|
|
26
|
+
{
|
|
27
|
+
"SSUSR_RowId": 20292,
|
|
28
|
+
"SSUSR_Initials": "20684",
|
|
29
|
+
"SSUSR_Name": "马俊"
|
|
30
|
+
}
|
|
31
|
+
],
|
|
32
|
+
"success": true,
|
|
33
|
+
"count": 1,
|
|
34
|
+
"message": "查询执行成功"
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
# 备注:使用 `POST` 请求调用;将 SQL 查询语句放入 `sql` 字段,将对应的查询参数放入 `params` 数组。
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: wdyy-deployment-standard
|
|
3
|
-
description: 为无 CI/CD
|
|
3
|
+
description: 为无 CI/CD 的前后端项目生成并验证跨宿主机统一 Docker 交付:在 Linux、macOS Docker Desktop 或 Windows Docker Desktop + WSL2/Linux 容器模式中,使用本地已有基础镜像构建单一 linux/amd64 或 linux/arm64 双镜像包,并在平台匹配的 Linux engine 上以固定端口、容器名、重启策略和 CST(UTC+8)时间整栈运行。Use when 规划、实现、审查或执行跨构建机平台的人工 Docker 生产交付时。
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# 统一 Docker 交付规范
|
|
7
7
|
|
|
8
8
|
## Overview
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
把一个项目作为一个整体交付:在 Linux、macOS Docker Desktop,或 Windows Docker Desktop + WSL2/Linux 容器模式中执行 `./deploy.sh build`,按 `DOCKER_PLATFORM` 生成前端、后端两个 Linux 单架构镜像并分别导出,最终只产出一个压缩包;工程师人工复制并解压;Docker Server 平台匹配时无参数执行 `./deploy.sh`,一次性加载并部署整个前后端项目栈。
|
|
11
|
+
|
|
12
|
+
macOS/arm64 宿主机的 Docker Server 平台是 `linux/arm64`。Windows 必须在启用 Docker Desktop 集成的 WSL2 发行版和 Linux 文件系统中执行同一 Bash 脚本,并使用 Linux 容器 engine;不支持原生 PowerShell、Windows 容器模式或 `windows/*` 镜像。
|
|
11
13
|
|
|
12
14
|
## Workflow
|
|
13
15
|
|
|
@@ -16,16 +18,23 @@ description: 为无 CI/CD 的前后端项目生成并验证统一 Docker 交付
|
|
|
16
18
|
3. 运行 `node scripts/generate-deployment-files.mjs --target <目标项目根目录> --write` 生成受版本控制文件并扩充 `.env`、`.env.example` 的部署段。既有生成文件不一致时先展示差异并等待工程师确认,确认后才使用 `--force`。
|
|
17
19
|
4. 在目标项目根执行生成器 `--check`,再运行 `scripts/validate-deployment-package.mjs` 和行为测试。
|
|
18
20
|
5. 本地执行 `./deploy.sh build`;只在全部校验成功后原子替换 `deploy/<PROJECT_NAME>-docker.tar.gz`。
|
|
19
|
-
6.
|
|
21
|
+
6. 人工复制并解压压缩包,在 Linux 文件系统的解压目录无参数执行 `./deploy.sh`;Windows 场景必须从 WSL2 执行。
|
|
20
22
|
|
|
21
23
|
## Fixed Contract
|
|
22
24
|
|
|
23
25
|
- 构建 `<PROJECT_NAME>_frontend:latest` 与 `<PROJECT_NAME>_backend:latest` 两个独立镜像。
|
|
26
|
+
- 每个交付包只对应 `DOCKER_PLATFORM` 指定的 `linux/amd64` 或 `linux/arm64`;构建机自身平台可以不同,不得回退到构建机原生架构。
|
|
24
27
|
- 分别导出 `frontend-image.tar`、`backend-image.tar`,但只交付一个 `<PROJECT_NAME>-docker.tar.gz`。
|
|
25
28
|
- 构建前检查 `FRONTEND_BASE_IMAGE`、`BACKEND_BASE_IMAGE` 已存在于本地;缺失即失败,不执行 `docker pull`。
|
|
29
|
+
- 宿主机可以是 Linux、macOS 或 Windows,但镜像和 Docker Server 必须是 `linux/amd64` 或 `linux/arm64`;加载前读取 Server OS/架构,无法读取、Windows 容器模式或不等于 `DOCKER_PLATFORM` 时立即失败。
|
|
26
30
|
- Compose 只定义 frontend、backend;数据库通过 `.env` 直连远程实例,不生成数据库容器、数据卷或迁移路径。
|
|
27
31
|
- 服务器无参数命令整体部署;只额外允许 `stop` 和 `status`,不得独立操作单一服务。
|
|
28
32
|
- 前后端直接暴露 `.env` 指定的宿主机端口;两个容器共同挂载项目根 `./logs` 到 `/app/logs`。
|
|
33
|
+
- `FRONTEND_PORT` 同时控制 frontend 宿主机映射和容器 Nginx 监听;`BACKEND_PORT` 同时控制 backend 宿主机映射、Node 监听、健康检查和 frontend 内部代理,不增加重复容器端口变量。
|
|
34
|
+
- frontend 容器和镜像 repository 都命名为 `<PROJECT_NAME>_frontend`,backend 都命名为 `<PROJECT_NAME>_backend`;镜像额外使用 `:latest` 标签。同名容器不属于当前 Compose 项目和服务时必须在加载镜像前失败,不得删除或接管。
|
|
35
|
+
- frontend 与 backend 都使用 `restart: unless-stopped`:意外退出由 Docker 自动重启,`./deploy.sh stop` 人工停止后保持停止,直到再次明确整栈部署。
|
|
36
|
+
- frontend Nginx 使用 `.env` 的 `VITE_API_BASE_URL` 与 `BACKEND_PORT`,把同源 API 请求代理到 Compose `backend` 服务;`VITE_API_BASE_URL` 必须等于 `/${API_PREFIX}`。
|
|
37
|
+
- 根 `.env` 与 `.env.example` 必须严格使用 `TZ=Asia/Shanghai`。构建后、Docker save 前实际探测两个镜像;整栈健康后实际探测两个运行容器,均必须以 24 小时格式输出 `CST +0800`。
|
|
29
38
|
- `.env` 可以进入人工上传的压缩包,但必须被 `.dockerignore` 排除,不得进入镜像。
|
|
30
39
|
|
|
31
40
|
## Prohibited
|
|
@@ -33,6 +42,7 @@ description: 为无 CI/CD 的前后端项目生成并验证统一 Docker 交付
|
|
|
33
42
|
- 不得使用镜像仓库 pull、镜像 fallback、数据库容器、数据库迁移、宿主机 Nginx、蓝绿、滚动切流或自动回滚。
|
|
34
43
|
- 不得保留发布账本、版本目录、`release.env`、交互菜单或前后端独立替换命令。
|
|
35
44
|
- 不得上传文件、拉取代码、安装 Docker、固定服务器目录或使用 `source`、`eval` 执行 `.env`。
|
|
45
|
+
- 不得生成 PowerShell 备用脚本、Windows 镜像或自动切换 Docker Desktop engine;Windows 原生容器模式必须明确失败。
|
|
36
46
|
- 不得跳过清单、镜像标签、Compose 服务范围、远程数据库配置或健康状态验证。
|
|
37
47
|
|
|
38
48
|
## Verification
|
|
@@ -40,8 +50,14 @@ description: 为无 CI/CD 的前后端项目生成并验证统一 Docker 交付
|
|
|
40
50
|
- [ ] deploy 目录只有一个 `<PROJECT_NAME>-docker.tar.gz`。
|
|
41
51
|
- [ ] 压缩包解压后只含 `.env`、deploy.sh、docker-compose.yml、两个镜像 tar 和 manifest.sha256。
|
|
42
52
|
- [ ] 两个镜像归档各自只含预期的 `latest` 标签。
|
|
43
|
-
- [ ]
|
|
53
|
+
- [ ] 单一压缩包不含 macOS AppleDouble、资源叉或扩展属性元数据。
|
|
54
|
+
- [ ] 构建与服务器 Compose 均禁止 pull,服务器禁止 build;基础镜像、构建参数、产出镜像和 Compose 平台均与 `DOCKER_PLATFORM` 一致。
|
|
55
|
+
- [ ] Linux Engine、macOS/arm64 Docker Desktop 与 Windows Docker Desktop + WSL2 场景使用同一包契约;Windows 原生容器模式被拒绝。
|
|
56
|
+
- [ ] Docker Server 平台在加载镜像前通过校验,并严格等于 `DOCKER_PLATFORM`。
|
|
44
57
|
- [ ] Compose 只含 frontend、backend,并使用远程数据库环境变量和根 logs。
|
|
58
|
+
- [ ] Compose 的双向端口都来自 FRONTEND_PORT/BACKEND_PORT;两个固定容器名等于各自镜像 repository,并各使用 `unless-stopped`。
|
|
59
|
+
- [ ] 根 `.env` 严格使用 `TZ=Asia/Shanghai`;两个镜像和两个运行容器的真实时间探测均为 24 小时制 `CST +0800`。
|
|
60
|
+
- [ ] frontend 镜像不包含 `.env`,同源 API 代理能够把真实页面查询转发到 backend。
|
|
45
61
|
- [ ] 无参数部署、stop、status 的实际行为及失败传播通过测试。
|
|
46
62
|
|
|
47
63
|
任一步失败必须指出具体阶段并返回非零;不得静默跳过、伪造成功或删除上一份成功交付包。
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
interface:
|
|
2
2
|
display_name: "wdyy-deployment-standard"
|
|
3
|
-
short_description: "
|
|
4
|
-
default_prompt: "Use $wdyy-deployment-standard to build
|
|
3
|
+
short_description: "跨 Linux、Mac 与 Windows WSL2 生成并验证统一双镜像交付"
|
|
4
|
+
default_prompt: "Use $wdyy-deployment-standard to build and run a single-target linux/amd64 or linux/arm64 dual-image package on Linux engines, macOS Docker Desktop, or Windows Docker Desktop through WSL2, with deterministic ports, names, restart policy, and CST UTC+8 time."
|
|
@@ -31,10 +31,14 @@ DB_USER=app_user
|
|
|
31
31
|
DB_PASSWORD=change_me
|
|
32
32
|
DB_NAME=app
|
|
33
33
|
DB_SCHEMA=public
|
|
34
|
+
API_PREFIX=api
|
|
35
|
+
VITE_API_BASE_URL=/api
|
|
34
36
|
PROJECT_NAME=example_project
|
|
35
37
|
DOCKER_BIND_IP=0.0.0.0
|
|
38
|
+
DOCKER_PLATFORM=linux/amd64
|
|
36
39
|
FRONTEND_BASE_IMAGE=nginx:stable
|
|
37
|
-
BACKEND_BASE_IMAGE=node:24-
|
|
40
|
+
BACKEND_BASE_IMAGE=node:24-bookworm-slim
|
|
41
|
+
TZ=Asia/Shanghai
|
|
38
42
|
```
|
|
39
43
|
|
|
40
44
|
- `PROJECT_NAME` 只允许小写字母开头,后续使用小写字母、数字或下划线。
|
|
@@ -42,21 +46,25 @@ BACKEND_BASE_IMAGE=node:24-alpine3.24
|
|
|
42
46
|
- `.env` 权限必须为 `0600` 或只读 `0400`。
|
|
43
47
|
- 按数据解析 `.env`;不得 `source`、`eval` 或执行其中内容。
|
|
44
48
|
- `DB_URL` 指向远程数据库主机或地址;部署资产不得把它解释为 Docker 服务名并创建数据库容器。
|
|
49
|
+
- `DOCKER_PLATFORM` 必须为 `linux/amd64` 或 `linux/arm64`,表示当前单一交付包的镜像目标平台,并与运行时 Docker Server 平台一致;不得根据 Linux、macOS、Windows 宿主机或 CPU 自动改写,不得使用 `darwin/*` 或 `windows/*`。
|
|
50
|
+
- `TZ` 必须严格等于 `Asia/Shanghai`,使两个容器统一使用 24 小时制的 CST(UTC+8);其他合法 IANA 时区也不属于本交付合同。
|
|
51
|
+
- `API_PREFIX` 只允许一个安全路径段,`VITE_API_BASE_URL` 必须严格等于 `/${API_PREFIX}`;生成器在两个键都缺失时加入标准值,存在缺失或冲突时明确失败。
|
|
45
52
|
- `.env.example` 可保留空数据库凭据,但真实 `.env` 构建时必须完整。
|
|
46
53
|
|
|
47
54
|
## 3. 本地镜像与构建
|
|
48
55
|
|
|
49
|
-
|
|
56
|
+
基础镜像引用允许使用标签,但目标架构版本必须在构建机本地预先存在。构建命令可以在 Linux、macOS Docker Desktop,或 Windows Docker Desktop + WSL2/Linux 容器模式中执行,也可以与目标架构不同;Windows 必须从启用 Docker Desktop 集成的 WSL2 发行版和 Linux 文件系统执行,不支持原生 PowerShell 或 Windows 容器模式。相同标签被人工替换后可能对应不同内容,构建过程不得访问镜像仓库或回退到构建机原生架构。
|
|
50
57
|
|
|
51
58
|
`./deploy.sh build` 必须按顺序:
|
|
52
59
|
|
|
53
60
|
1. 验证根 `.env`、Dockerfile、Compose 和 `.dockerignore`。
|
|
54
|
-
2.
|
|
55
|
-
3.
|
|
56
|
-
4.
|
|
57
|
-
5.
|
|
58
|
-
6.
|
|
59
|
-
7.
|
|
61
|
+
2. 使用平台感知的 `docker image inspect --platform "$DOCKER_PLATFORM"` 确认两个基础镜像的目标架构版本存在。
|
|
62
|
+
3. 校验本地基础镜像平台与 `DOCKER_PLATFORM` 一致,使用 `docker build --pull=false --platform "$DOCKER_PLATFORM"` 构建 `<PROJECT_NAME>_frontend:latest` 与 `<PROJECT_NAME>_backend:latest`,并校验产出镜像平台。
|
|
63
|
+
4. 对两个产出镜像分别使用 `docker run --rm --pull=never --platform "$DOCKER_PLATFORM"` 探测 `date '+%H %Z %z'`,要求小时为 `00`–`23`、时区为 `CST +0800`。
|
|
64
|
+
5. 分别执行 Docker save,生成 `frontend-image.tar`、`backend-image.tar`。
|
|
65
|
+
6. 复制根 `.env`、deploy.sh、docker-compose.yml,生成覆盖全部包内文件的 `manifest.sha256`。
|
|
66
|
+
7. 验证镜像归档各自只含预期标签,验证完整包结构。
|
|
67
|
+
8. 使用 `COPYFILE_DISABLE=1` 和 `tar --no-xattrs` 排除 macOS AppleDouble、资源叉和扩展属性,生成 `deploy/<PROJECT_NAME>-docker.tar.gz`,成功后原子替换同名旧包。
|
|
60
68
|
|
|
61
69
|
构建失败必须保留上一份成功压缩包,并清理本次临时目录。不得维护发布账本、版本号、版本目录、`release.env` 或半成品目录。
|
|
62
70
|
|
|
@@ -73,13 +81,14 @@ backend-image.tar
|
|
|
73
81
|
manifest.sha256
|
|
74
82
|
```
|
|
75
83
|
|
|
76
|
-
不得包含源代码、Git
|
|
84
|
+
不得包含源代码、Git、依赖缓存、数据库目录、迁移脚本、秘密副本、符号链接、macOS AppleDouble/扩展属性或额外嵌套目录。`.env` 必须保持 owner-only 权限并纳入清单;`.dockerignore` 必须确保 `.env`、秘密、deploy、logs、Git、依赖缓存和虚拟环境不进入镜像构建上下文。
|
|
77
85
|
|
|
78
86
|
## 5. 服务器一键操作
|
|
79
87
|
|
|
80
|
-
|
|
88
|
+
工程师人工复制并解压单一压缩包。部署根由解压后的 deploy.sh 物理路径确定。Windows Docker Desktop 场景必须把包放在 WSL2 Linux 文件系统中;不能满足 `.env` owner-only 权限的 Windows 挂载目录必须失败。复制用的 `.tar.gz` 必须放在部署根外,或在解压后移出部署根;执行 `./deploy.sh` 时,部署根只允许保留压缩包契约中的 6 个文件和运行后生成的 `logs/` 目录。
|
|
81
89
|
|
|
82
|
-
- `./deploy.sh`:校验 `.env` 权限和内容、清单、两个镜像 tar
|
|
90
|
+
- `./deploy.sh`:校验 `.env` 权限和内容、清单、两个镜像 tar 及预期标签;通过 `docker version` 读取 Docker Server OS/架构并要求是与 `DOCKER_PLATFORM` 相等的 Linux engine;检查固定容器名不存在或属于当前 Compose 项目和对应服务;校验通过后才加载两个镜像,再使用一次 `docker compose up -d --force-recreate --no-build --pull never --wait frontend backend` 整体部署并等待健康,最后在两个运行容器中验证 24 小时制 `CST +0800`。
|
|
91
|
+
- 加载后必须再次校验两个镜像与 `DOCKER_PLATFORM` 一致;Compose 的两个服务也必须显式声明同一平台。
|
|
83
92
|
- `./deploy.sh stop`:同时停止 frontend、backend,保留容器、镜像、交付文件和日志。
|
|
84
93
|
- `./deploy.sh status`:输出当前 Compose 项目的服务状态,不改变 Docker 状态。
|
|
85
94
|
|
|
@@ -87,17 +96,19 @@ manifest.sha256
|
|
|
87
96
|
|
|
88
97
|
## 6. Compose、数据库和日志
|
|
89
98
|
|
|
90
|
-
- Compose 只允许 frontend、backend
|
|
99
|
+
- Compose 只允许 frontend、backend 两个服务;`container_name` 分别固定为 `${PROJECT_NAME}_frontend`、`${PROJECT_NAME}_backend`,与镜像 repository 相同但不含 `:latest`。
|
|
91
100
|
- 镜像固定为 `${PROJECT_NAME}_frontend:latest`、`${PROJECT_NAME}_backend:latest`,不得提供 fallback。
|
|
92
101
|
- frontend 映射 `${DOCKER_BIND_IP}:${FRONTEND_PORT}:${FRONTEND_PORT}`;backend 同理。
|
|
93
|
-
-
|
|
102
|
+
- 两个服务都固定使用 `restart: unless-stopped`;意外退出由 Docker 重启,`./deploy.sh stop` 后不得由脚本再次启动。
|
|
103
|
+
- frontend Nginx 把 `${VITE_API_BASE_URL}/...` 原样代理到 `http://backend:${BACKEND_PORT}`;不得硬编码服务器地址或后端端口。
|
|
104
|
+
- 两个服务均使用根 `.env`,显式接收同一 `TZ`,并挂载 `./logs:/app/logs`。
|
|
94
105
|
- backend 直接读取 DB_URL、DB_PORT、DB_USER、DB_PASSWORD、DB_NAME、DB_SCHEMA。
|
|
95
106
|
- Compose 不得定义数据库服务、数据库镜像、数据库 volume、数据库端口映射或迁移任务。
|
|
96
107
|
- 前端 Nginx 日志写入 `/app/logs/frontend-access.log`、`/app/logs/frontend-error.log`;后端遵循 wdyy-logging-standard;部署操作写入 `./logs/deploy.log`。
|
|
97
108
|
|
|
98
109
|
## 7. 失败边界
|
|
99
110
|
|
|
100
|
-
-
|
|
111
|
+
- 目标架构基础镜像缺失、Docker Server 平台无法读取或不匹配、Windows 容器模式、同名容器所有权冲突、TZ 不等于 `Asia/Shanghai`、镜像或运行容器时间不是 24 小时制 `CST +0800`、清单错误、镜像标签错误、远程数据库配置不完整或任一服务不健康时明确失败。
|
|
101
112
|
- 禁止 `docker pull`、服务器 `docker build`、镜像 fallback、自动回滚、自动安装和静默跳过。
|
|
102
113
|
- 脚本只操作当前 `PROJECT_NAME` 的 Compose 项目和 frontend、backend 服务,不清理其他项目资源。
|
|
103
114
|
- 旧部署资产由工程师根据实际状态单独迁移;新脚本不得猜测并删除。
|
|
@@ -24,7 +24,9 @@ const exampleDefaults = new Map([
|
|
|
24
24
|
['BACKEND_URL', '0.0.0.0'], ['BACKEND_PORT', '3000'],
|
|
25
25
|
['DB_URL', ''], ['DB_PORT', '5432'], ['DB_USER', ''], ['DB_PASSWORD', ''], ['DB_NAME', ''], ['DB_SCHEMA', ''],
|
|
26
26
|
]);
|
|
27
|
-
const
|
|
27
|
+
const apiKeys = ['API_PREFIX', 'VITE_API_BASE_URL'];
|
|
28
|
+
const legacyDeploymentKeys = ['PROJECT_NAME', 'DOCKER_BIND_IP', 'DOCKER_PLATFORM', 'FRONTEND_BASE_IMAGE', 'BACKEND_BASE_IMAGE'];
|
|
29
|
+
const deploymentKeys = [...legacyDeploymentKeys, 'TZ'];
|
|
28
30
|
const removedKeys = new Set([
|
|
29
31
|
'PROJECT_HTTP_PORT', 'FRONTEND_BLUE_PORT', 'FRONTEND_GREEN_PORT', 'BACKEND_BLUE_PORT', 'BACKEND_GREEN_PORT',
|
|
30
32
|
'SERVER_PROJECTS_ROOT', 'NGINX_SOURCE', 'LOG_DIR', 'HOST_LOG_DIR', 'FRONTEND_CONTAINER_PORT',
|
|
@@ -94,24 +96,54 @@ function parseEnvironment(text, relativePath, requireExampleDefaults, deployment
|
|
|
94
96
|
}
|
|
95
97
|
}
|
|
96
98
|
}
|
|
99
|
+
const apiEntries = sections.get('API');
|
|
100
|
+
const apiValues = new Map(apiEntries.map(({ key, value }) => [key, value]));
|
|
101
|
+
const presentApiKeys = apiKeys.filter((key) => apiValues.has(key));
|
|
102
|
+
if (presentApiKeys.length !== 0 && presentApiKeys.length !== apiKeys.length) {
|
|
103
|
+
throw new Error(`${relativePath} # API must contain both: ${apiKeys.join(', ')}`);
|
|
104
|
+
}
|
|
105
|
+
if (presentApiKeys.length === apiKeys.length) {
|
|
106
|
+
const prefix = apiValues.get('API_PREFIX');
|
|
107
|
+
const baseUrl = apiValues.get('VITE_API_BASE_URL');
|
|
108
|
+
if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(prefix)) throw new Error(`${relativePath} API_PREFIX must be one safe path segment`);
|
|
109
|
+
if (baseUrl !== `/${prefix}`) throw new Error(`${relativePath} VITE_API_BASE_URL must equal /${prefix}`);
|
|
110
|
+
}
|
|
97
111
|
const deploymentEntries = sections.get('部署');
|
|
98
112
|
const actualDeploymentKeys = deploymentEntries.map(({ key }) => key);
|
|
99
|
-
|
|
100
|
-
|
|
113
|
+
const deploymentKeySequence = actualDeploymentKeys.join('|');
|
|
114
|
+
const isEmptyDeployment = actualDeploymentKeys.length === 0;
|
|
115
|
+
const isLegacyDeployment = deploymentKeySequence === legacyDeploymentKeys.join('|');
|
|
116
|
+
const isCurrentDeployment = deploymentKeySequence === deploymentKeys.join('|');
|
|
117
|
+
if (!isEmptyDeployment && !isLegacyDeployment && !isCurrentDeployment) {
|
|
118
|
+
throw new Error(`${relativePath} # 部署 keys must be empty, the legacy sequence ${legacyDeploymentKeys.join(', ')}, or exactly: ${deploymentKeys.join(', ')}`);
|
|
101
119
|
}
|
|
102
120
|
if (requireExampleDefaults) {
|
|
103
121
|
for (const { key, value } of deploymentEntries) {
|
|
104
122
|
if (value !== deploymentDefaults.get(key)) throw new Error(`${relativePath} must keep the standard deployment default for ${key}`);
|
|
105
123
|
}
|
|
106
124
|
}
|
|
125
|
+
if (isCurrentDeployment) {
|
|
126
|
+
const timezone = deploymentEntries.find(({ key }) => key === 'TZ')?.value;
|
|
127
|
+
if (timezone !== 'Asia/Shanghai') throw new Error(`${relativePath} TZ must equal Asia/Shanghai for CST (UTC+8)`);
|
|
128
|
+
}
|
|
107
129
|
return {
|
|
108
|
-
|
|
130
|
+
apiExtended: presentApiKeys.length === apiKeys.length,
|
|
131
|
+
deploymentExtended: isCurrentDeployment,
|
|
132
|
+
presentDeploymentKeys: actualDeploymentKeys,
|
|
109
133
|
nonDeploymentKeys: [...seen].filter((key) => !deploymentKeys.includes(key)),
|
|
110
134
|
};
|
|
111
135
|
}
|
|
112
136
|
|
|
113
|
-
function
|
|
114
|
-
const additions =
|
|
137
|
+
function extendApiSection(text, apiDefaults) {
|
|
138
|
+
const additions = apiKeys.map((key) => `${key}=${apiDefaults.get(key)}`).join('\n');
|
|
139
|
+
return `${text.replaceAll('\r\n', '\n').trimEnd().replace(/^# 部署$/m, `${additions}\n\n# 部署`)}\n`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function extendDeploymentSection(text, deploymentDefaults, presentDeploymentKeys) {
|
|
143
|
+
const additions = deploymentKeys
|
|
144
|
+
.filter((key) => !presentDeploymentKeys.includes(key))
|
|
145
|
+
.map((key) => `${key}=${deploymentDefaults.get(key)}`)
|
|
146
|
+
.join('\n');
|
|
115
147
|
return `${text.trimEnd()}\n${additions}\n`;
|
|
116
148
|
}
|
|
117
149
|
|
|
@@ -145,7 +177,8 @@ if (!targetInfo.isDirectory()) throw new Error(`Target project root is not a dir
|
|
|
145
177
|
|
|
146
178
|
const deploymentTemplate = await readFile(join(templatesRoot, 'env.example.template'), 'utf8');
|
|
147
179
|
const deploymentDefaults = new Map([...deploymentTemplate.matchAll(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/gm)].map((match) => [match[1], match[2]]));
|
|
148
|
-
if (deploymentKeys.some((key) => !deploymentDefaults.has(key))) throw new Error('Deployment environment template is incomplete');
|
|
180
|
+
if ([...apiKeys, ...deploymentKeys].some((key) => !deploymentDefaults.has(key))) throw new Error('Deployment environment template is incomplete');
|
|
181
|
+
const apiDefaults = new Map(apiKeys.map((key) => [key, deploymentDefaults.get(key)]));
|
|
149
182
|
|
|
150
183
|
const environmentPlans = [];
|
|
151
184
|
for (const [relativePath, expectedMode, requireDefaults] of [['.env', 0o600, false], ['.env.example', 0o644, true]]) {
|
|
@@ -153,10 +186,13 @@ for (const [relativePath, expectedMode, requireDefaults] of [['.env', 0o600, fal
|
|
|
153
186
|
const existingBuffer = await readRegularFile(path, relativePath);
|
|
154
187
|
const existing = existingBuffer.toString('utf8');
|
|
155
188
|
const parsed = parseEnvironment(existing, relativePath, requireDefaults, deploymentDefaults);
|
|
189
|
+
let expected = existing;
|
|
190
|
+
if (!parsed.apiExtended) expected = extendApiSection(expected, apiDefaults);
|
|
191
|
+
if (!parsed.deploymentExtended) expected = extendDeploymentSection(expected, deploymentDefaults, parsed.presentDeploymentKeys);
|
|
156
192
|
environmentPlans.push({
|
|
157
193
|
path, relativePath, expectedMode,
|
|
158
194
|
existing: existingBuffer,
|
|
159
|
-
expected: Buffer.from(
|
|
195
|
+
expected: Buffer.from(expected),
|
|
160
196
|
nonDeploymentKeys: parsed.nonDeploymentKeys,
|
|
161
197
|
});
|
|
162
198
|
}
|
package/.well-known/skills/wdyy-deployment-standard/scripts/generate-deployment-files.test.mjs
CHANGED
|
@@ -71,10 +71,33 @@ test('生成统一 Docker 交付文件并通过确定性检查', async () => {
|
|
|
71
71
|
}
|
|
72
72
|
assert.equal((await stat(join(root, 'deploy.sh'))).mode & 0o777, 0o755);
|
|
73
73
|
assert.equal((await stat(join(root, '.env'))).mode & 0o777, 0o600);
|
|
74
|
-
|
|
75
|
-
assert.match(
|
|
74
|
+
const environment = await readFile(join(root, '.env'), 'utf8');
|
|
75
|
+
assert.match(environment, /API_URL=\nAPI_PREFIX=api\nVITE_API_BASE_URL=\/api\n\n# 部署/);
|
|
76
|
+
assert.match(environment, /FRONTEND_BASE_IMAGE=nginx:stable/);
|
|
77
|
+
assert.match(environment, /DOCKER_PLATFORM=linux\/amd64/);
|
|
78
|
+
assert.match(environment, /TZ=Asia\/Shanghai/);
|
|
79
|
+
assert.match(await readFile(join(root, '.env.example'), 'utf8'), /BACKEND_BASE_IMAGE=node:24-bookworm-slim/);
|
|
80
|
+
const compose = await readFile(join(root, 'scripts/deployment/docker-compose.yml'), 'utf8');
|
|
81
|
+
assert.equal((compose.match(/TZ: "\$\{TZ:\?TZ is required\}"/g) ?? []).length, 2);
|
|
82
|
+
assert.match(compose, /container_name: "\$\{PROJECT_NAME:\?PROJECT_NAME is required\}_frontend"/);
|
|
83
|
+
assert.match(compose, /container_name: "\$\{PROJECT_NAME:\?PROJECT_NAME is required\}_backend"/);
|
|
84
|
+
assert.equal((compose.match(/restart: unless-stopped/g) ?? []).length, 2);
|
|
85
|
+
assert.match(compose, /"\$\{DOCKER_BIND_IP\}:\$\{FRONTEND_PORT\}:\$\{FRONTEND_PORT\}"/);
|
|
86
|
+
assert.match(compose, /"\$\{DOCKER_BIND_IP\}:\$\{BACKEND_PORT\}:\$\{BACKEND_PORT\}"/);
|
|
87
|
+
const nginx = await readFile(join(root, 'scripts/deployment/frontend-container.conf.template'), 'utf8');
|
|
88
|
+
assert.match(nginx, /location \$\{VITE_API_BASE_URL\}\/ \{/);
|
|
89
|
+
assert.match(nginx, /proxy_pass http:\/\/backend:\$\{BACKEND_PORT\};/);
|
|
90
|
+
const frontendDockerfile = await readFile(join(root, 'src/frontend/Dockerfile'), 'utf8');
|
|
91
|
+
assert.match(frontendDockerfile, /ARG VITE_API_BASE_URL/);
|
|
92
|
+
assert.doesNotMatch(frontendDockerfile, /COPY\s+\.env/);
|
|
93
|
+
const backendDockerfile = await readFile(join(root, 'src/backend/Dockerfile'), 'utf8');
|
|
94
|
+
assert.match(backendDockerfile, /pnpm --filter \.\/src\/backend deploy --prod --legacy \/runtime/);
|
|
95
|
+
assert.match(backendDockerfile, /WORKDIR \/app\n/);
|
|
96
|
+
assert.match(backendDockerfile, /COPY --from=build \/runtime \.\/src\/backend\//);
|
|
97
|
+
assert.match(backendDockerfile, /CMD \["node", "src\/backend\/dist\/main\.js"\]/);
|
|
76
98
|
const check = await invoke(root, '--check');
|
|
77
99
|
assert.match(check.stdout, /verified unified Docker delivery files/);
|
|
100
|
+
assert.match(await readFile(join(root, 'deploy.sh'), 'utf8'), /env COPYFILE_DISABLE=1 tar --no-xattrs -czf "\$candidate"/);
|
|
78
101
|
});
|
|
79
102
|
|
|
80
103
|
test('扩充部署段时完整保留真实环境值', async () => {
|
|
@@ -86,6 +109,112 @@ test('扩充部署段时完整保留真实环境值', async () => {
|
|
|
86
109
|
assert.match(actual, /PROJECT_NAME=\nDOCKER_BIND_IP=\n/);
|
|
87
110
|
});
|
|
88
111
|
|
|
112
|
+
test('旧五键部署段只追加标准时区并保留既有值', async () => {
|
|
113
|
+
const legacyReal = realEnv.replace('# 部署\n', `# 部署
|
|
114
|
+
PROJECT_NAME=custom_project
|
|
115
|
+
DOCKER_BIND_IP=127.0.0.1
|
|
116
|
+
DOCKER_PLATFORM=linux/arm64
|
|
117
|
+
FRONTEND_BASE_IMAGE=custom-nginx:1
|
|
118
|
+
BACKEND_BASE_IMAGE=custom-node:1
|
|
119
|
+
`);
|
|
120
|
+
const legacyExample = exampleEnv.replace('# 部署\n', `# 部署
|
|
121
|
+
PROJECT_NAME=
|
|
122
|
+
DOCKER_BIND_IP=
|
|
123
|
+
DOCKER_PLATFORM=linux/amd64
|
|
124
|
+
FRONTEND_BASE_IMAGE=nginx:stable
|
|
125
|
+
BACKEND_BASE_IMAGE=node:24-bookworm-slim
|
|
126
|
+
`);
|
|
127
|
+
const root = await fixture(legacyReal, legacyExample);
|
|
128
|
+
await invoke(root, '--write');
|
|
129
|
+
const actual = await readFile(join(root, '.env'), 'utf8');
|
|
130
|
+
assert.match(actual, /PROJECT_NAME=custom_project/);
|
|
131
|
+
assert.match(actual, /DOCKER_PLATFORM=linux\/arm64/);
|
|
132
|
+
assert.match(actual, /BACKEND_BASE_IMAGE=custom-node:1\nTZ=Asia\/Shanghai\n$/);
|
|
133
|
+
assert.match(await readFile(join(root, '.env.example'), 'utf8'), /BACKEND_BASE_IMAGE=node:24-bookworm-slim\nTZ=Asia\/Shanghai\n$/);
|
|
134
|
+
await invoke(root, '--check');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('非标准时区默认值和无效部署键顺序被拒绝且不修改', async () => {
|
|
138
|
+
const root = await fixture();
|
|
139
|
+
await invoke(root, '--write');
|
|
140
|
+
const examplePath = join(root, '.env.example');
|
|
141
|
+
const invalidTimezoneExample = (await readFile(examplePath, 'utf8')).replace('TZ=Asia/Shanghai', 'TZ=UTC');
|
|
142
|
+
await writeFile(examplePath, invalidTimezoneExample, { mode: 0o644 });
|
|
143
|
+
const before = await readFile(examplePath);
|
|
144
|
+
await assert.rejects(invoke(root, '--write'), /standard deployment default for TZ/);
|
|
145
|
+
assert.deepEqual(await readFile(examplePath), before);
|
|
146
|
+
|
|
147
|
+
const invalidOrder = realEnv.replace('# 部署\n', `# 部署
|
|
148
|
+
TZ=Asia/Shanghai
|
|
149
|
+
PROJECT_NAME=example_project
|
|
150
|
+
DOCKER_BIND_IP=0.0.0.0
|
|
151
|
+
DOCKER_PLATFORM=linux/amd64
|
|
152
|
+
FRONTEND_BASE_IMAGE=nginx:stable
|
|
153
|
+
BACKEND_BASE_IMAGE=node:24-bookworm-slim
|
|
154
|
+
`);
|
|
155
|
+
await assert.rejects(invoke(await fixture(invalidOrder), '--write'), /# 部署 keys must be/);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('真实环境必须固定 Asia/Shanghai 且失败时不修改受控文件', async () => {
|
|
159
|
+
const root = await fixture();
|
|
160
|
+
await invoke(root, '--write');
|
|
161
|
+
const envPath = join(root, '.env');
|
|
162
|
+
const invalid = (await readFile(envPath, 'utf8')).replace('TZ=Asia/Shanghai', 'TZ=UTC');
|
|
163
|
+
await writeFile(envPath, invalid, { mode: 0o600 });
|
|
164
|
+
const beforeEnv = await readFile(envPath);
|
|
165
|
+
const beforeDeploy = await readFile(join(root, 'deploy.sh'));
|
|
166
|
+
await assert.rejects(invoke(root, '--write'), /TZ must equal Asia\/Shanghai for CST \(UTC\+8\)/);
|
|
167
|
+
assert.deepEqual(await readFile(envPath), beforeEnv);
|
|
168
|
+
assert.deepEqual(await readFile(join(root, 'deploy.sh')), beforeDeploy);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test('非默认应用端口保持单一来源且宿主机变量不改写镜像平台', async () => {
|
|
172
|
+
const configured = realEnv
|
|
173
|
+
.replace('FRONTEND_PORT=5173', 'FRONTEND_PORT=18080')
|
|
174
|
+
.replace('BACKEND_PORT=3000', 'BACKEND_PORT=19090')
|
|
175
|
+
.replace('# 部署\n', `# 部署
|
|
176
|
+
PROJECT_NAME=desktop_project
|
|
177
|
+
DOCKER_BIND_IP=127.0.0.1
|
|
178
|
+
DOCKER_PLATFORM=linux/arm64
|
|
179
|
+
FRONTEND_BASE_IMAGE=nginx:stable
|
|
180
|
+
BACKEND_BASE_IMAGE=node:24-bookworm-slim
|
|
181
|
+
TZ=Asia/Shanghai
|
|
182
|
+
`);
|
|
183
|
+
const root = await fixture(configured);
|
|
184
|
+
await invoke(root, '--write');
|
|
185
|
+
const actual = await readFile(join(root, '.env'), 'utf8');
|
|
186
|
+
assert.match(actual, /FRONTEND_PORT=18080/);
|
|
187
|
+
assert.match(actual, /BACKEND_PORT=19090/);
|
|
188
|
+
assert.match(actual, /DOCKER_PLATFORM=linux\/arm64/);
|
|
189
|
+
const compose = await readFile(join(root, 'scripts/deployment/docker-compose.yml'), 'utf8');
|
|
190
|
+
assert.match(compose, /"\$\{DOCKER_BIND_IP\}:\$\{FRONTEND_PORT\}:\$\{FRONTEND_PORT\}"/);
|
|
191
|
+
assert.match(compose, /"\$\{DOCKER_BIND_IP\}:\$\{BACKEND_PORT\}:\$\{BACKEND_PORT\}"/);
|
|
192
|
+
assert.doesNotMatch(compose, /18080|19090|FRONTEND_CONTAINER_PORT|BACKEND_CONTAINER_PORT/);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('保留有效的自定义 API 路径', async () => {
|
|
196
|
+
const configuredReal = realEnv.replace('API_URL=\n', 'API_URL=\nAPI_PREFIX=service\nVITE_API_BASE_URL=/service\n');
|
|
197
|
+
const configuredExample = exampleEnv.replace('API_URL=\n', 'API_URL=\nAPI_PREFIX=service\nVITE_API_BASE_URL=/service\n');
|
|
198
|
+
const root = await fixture(configuredReal, configuredExample);
|
|
199
|
+
await invoke(root, '--write');
|
|
200
|
+
assert.match(await readFile(join(root, '.env'), 'utf8'), /API_PREFIX=service\nVITE_API_BASE_URL=\/service/);
|
|
201
|
+
await invoke(root, '--check');
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('缺失、非法或冲突的 API 路径被明确拒绝', async () => {
|
|
205
|
+
const partialReal = realEnv.replace('API_URL=\n', 'API_URL=\nAPI_PREFIX=api\n');
|
|
206
|
+
const partialExample = exampleEnv.replace('API_URL=\n', 'API_URL=\nAPI_PREFIX=api\n');
|
|
207
|
+
await assert.rejects(invoke(await fixture(partialReal, partialExample), '--write'), /must contain both/);
|
|
208
|
+
|
|
209
|
+
const invalidReal = realEnv.replace('API_URL=\n', 'API_URL=\nAPI_PREFIX=api/path\nVITE_API_BASE_URL=/api\/path\n');
|
|
210
|
+
const invalidExample = exampleEnv.replace('API_URL=\n', 'API_URL=\nAPI_PREFIX=api/path\nVITE_API_BASE_URL=/api\/path\n');
|
|
211
|
+
await assert.rejects(invoke(await fixture(invalidReal, invalidExample), '--write'), /one safe path segment/);
|
|
212
|
+
|
|
213
|
+
const conflictReal = realEnv.replace('API_URL=\n', 'API_URL=\nAPI_PREFIX=api\nVITE_API_BASE_URL=/service\n');
|
|
214
|
+
const conflictExample = exampleEnv.replace('API_URL=\n', 'API_URL=\nAPI_PREFIX=api\nVITE_API_BASE_URL=/service\n');
|
|
215
|
+
await assert.rejects(invoke(await fixture(conflictReal, conflictExample), '--write'), /VITE_API_BASE_URL must equal \/api/);
|
|
216
|
+
});
|
|
217
|
+
|
|
89
218
|
test('非标准环境文件被拒绝且不修改', async () => {
|
|
90
219
|
const invalid = realEnv.replace('# 数据库\n', '');
|
|
91
220
|
const root = await fixture(invalid);
|
|
@@ -95,6 +224,23 @@ test('非标准环境文件被拒绝且不修改', async () => {
|
|
|
95
224
|
await assert.rejects(stat(join(root, 'deploy.sh')), { code: 'ENOENT' });
|
|
96
225
|
});
|
|
97
226
|
|
|
227
|
+
test('旧后端基础镜像默认值被拒绝且两个环境文件均不修改', async () => {
|
|
228
|
+
const root = await fixture();
|
|
229
|
+
await invoke(root, '--write');
|
|
230
|
+
const envPath = join(root, '.env');
|
|
231
|
+
const examplePath = join(root, '.env.example');
|
|
232
|
+
const configuredEnv = (await readFile(envPath, 'utf8')).replace('node:24-bookworm-slim', 'node:24-alpine3.24');
|
|
233
|
+
const outdatedExample = (await readFile(examplePath, 'utf8')).replace('node:24-bookworm-slim', 'node:24-alpine3.24');
|
|
234
|
+
await writeFile(envPath, configuredEnv, { mode: 0o600 });
|
|
235
|
+
await writeFile(examplePath, outdatedExample, { mode: 0o644 });
|
|
236
|
+
const beforeEnv = await readFile(envPath);
|
|
237
|
+
const beforeExample = await readFile(examplePath);
|
|
238
|
+
|
|
239
|
+
await assert.rejects(invoke(root, '--write'), /must keep the standard deployment default for BACKEND_BASE_IMAGE/);
|
|
240
|
+
assert.deepEqual(await readFile(envPath), beforeEnv);
|
|
241
|
+
assert.deepEqual(await readFile(examplePath), beforeExample);
|
|
242
|
+
});
|
|
243
|
+
|
|
98
244
|
test('环境变量集合不一致时拒绝修改', async () => {
|
|
99
245
|
const root = await fixture(realEnv, exampleEnv.replace('API_URL=', 'API_URL=\nAPI_TOKEN='));
|
|
100
246
|
await assert.rejects(invoke(root, '--write'), /variable names must remain consistent/);
|
|
@@ -55,7 +55,7 @@ const envText = await readFile(join(packageRoot, '.env'), 'utf8');
|
|
|
55
55
|
const env = parseDotenv(envText);
|
|
56
56
|
const sections = [...envText.matchAll(/^# (前端|后端|数据库|API|部署)$/gm)].map((match) => match[1]);
|
|
57
57
|
assert(sections.join('|') === '前端|后端|数据库|API|部署', 'Root .env must use the five standard sections in order');
|
|
58
|
-
for (const key of ['FRONTEND_URL', 'FRONTEND_PORT', 'BACKEND_URL', 'BACKEND_PORT', 'DB_URL', 'DB_PORT', 'DB_USER', 'DB_PASSWORD', 'DB_NAME', 'DB_SCHEMA', 'PROJECT_NAME', 'DOCKER_BIND_IP', 'FRONTEND_BASE_IMAGE', 'BACKEND_BASE_IMAGE']) {
|
|
58
|
+
for (const key of ['FRONTEND_URL', 'FRONTEND_PORT', 'BACKEND_URL', 'BACKEND_PORT', 'API_PREFIX', 'VITE_API_BASE_URL', 'DB_URL', 'DB_PORT', 'DB_USER', 'DB_PASSWORD', 'DB_NAME', 'DB_SCHEMA', 'PROJECT_NAME', 'DOCKER_BIND_IP', 'DOCKER_PLATFORM', 'FRONTEND_BASE_IMAGE', 'BACKEND_BASE_IMAGE', 'TZ']) {
|
|
59
59
|
assert(env.get(key), `Root .env is missing ${key}`);
|
|
60
60
|
}
|
|
61
61
|
assert(/^[a-z][a-z0-9_]*$/.test(env.get('PROJECT_NAME')), 'Invalid PROJECT_NAME');
|
|
@@ -64,6 +64,10 @@ for (const key of ['FRONTEND_PORT', 'BACKEND_PORT', 'DB_PORT']) {
|
|
|
64
64
|
assert(Number.isInteger(port) && port >= 1 && port <= 65535, `${key} must be a valid port`);
|
|
65
65
|
}
|
|
66
66
|
assert(env.get('FRONTEND_PORT') !== env.get('BACKEND_PORT'), 'Frontend and backend ports must differ');
|
|
67
|
+
assert(/^[A-Za-z][A-Za-z0-9_-]*$/.test(env.get('API_PREFIX')), 'API_PREFIX must be one safe path segment');
|
|
68
|
+
assert(env.get('VITE_API_BASE_URL') === `/${env.get('API_PREFIX')}`, 'VITE_API_BASE_URL must equal /API_PREFIX');
|
|
69
|
+
assert(/^(?:linux\/amd64|linux\/arm64)$/.test(env.get('DOCKER_PLATFORM')), 'Invalid DOCKER_PLATFORM');
|
|
70
|
+
assert(env.get('TZ') === 'Asia/Shanghai', 'Invalid TZ; expected Asia/Shanghai for CST (UTC+8)');
|
|
67
71
|
for (const key of env.keys()) {
|
|
68
72
|
assert(!['PROJECT_HTTP_PORT', 'FRONTEND_BLUE_PORT', 'FRONTEND_GREEN_PORT', 'BACKEND_BLUE_PORT', 'BACKEND_GREEN_PORT', 'SERVER_PROJECTS_ROOT', 'NGINX_SOURCE', 'LOG_DIR', 'HOST_LOG_DIR', 'FRONTEND_CONTAINER_PORT', 'BACKEND_CONTAINER_PORT', 'BACKEND_LISTEN_HOST', 'DATABASE_MIGRATION_MODE', 'MIGRATIONS_SOURCE', 'MIGRATION_RUNNER_SOURCE', 'RELEASE_VERSION'].includes(key) && !/_(?:HEALTH|VERSION)_URL$/.test(key), `Root .env contains removed configuration: ${key}`);
|
|
69
73
|
}
|
|
@@ -77,15 +81,35 @@ for (const [pattern, message] of [
|
|
|
77
81
|
[/"\$1" == status/, 'Missing status command'],
|
|
78
82
|
[/docker image inspect "\$FRONTEND_BASE_IMAGE"/, 'Frontend local base image must be checked'],
|
|
79
83
|
[/docker image inspect "\$BACKEND_BASE_IMAGE"/, 'Backend local base image must be checked'],
|
|
80
|
-
[/docker build --pull=false/g, 'Docker builds must disable pull'],
|
|
84
|
+
[/docker build --pull=false --platform "\$DOCKER_PLATFORM"/g, 'Docker builds must use the configured target platform and disable pull'],
|
|
81
85
|
[/docker save -o "\$package_root\/frontend-image\.tar"/, 'Frontend image must be exported separately'],
|
|
82
86
|
[/docker save -o "\$package_root\/backend-image\.tar"/, 'Backend image must be exported separately'],
|
|
87
|
+
[/env COPYFILE_DISABLE=1 tar --no-xattrs -czf "\$candidate"/, 'Delivery archive must exclude macOS metadata and extended attributes'],
|
|
88
|
+
[/docker version --format '\{\{\.Server\.Os\}\}\/\{\{\.Server\.Arch\}\}'/, 'Deploy must read the Docker Server platform'],
|
|
89
|
+
[/Windows container mode is unsupported; switch Docker Desktop to Linux containers/, 'Deploy must reject Windows container mode explicitly'],
|
|
90
|
+
[/assert_stack_container_ownership/, 'Deploy must validate fixed container name ownership'],
|
|
91
|
+
[/assert_image_cst_time "\$frontend_image"/, 'Frontend built image CST time must be checked'],
|
|
92
|
+
[/assert_image_cst_time "\$backend_image"/, 'Backend built image CST time must be checked'],
|
|
93
|
+
[/assert_container_cst_time "\$\{PROJECT_NAME\}_frontend"/, 'Running frontend CST time must be checked'],
|
|
94
|
+
[/assert_container_cst_time "\$\{PROJECT_NAME\}_backend"/, 'Running backend CST time must be checked'],
|
|
83
95
|
[/compose up -d --force-recreate --no-build --pull never --wait frontend backend/, 'Deploy must recreate and wait for both services without build or pull'],
|
|
84
96
|
[/compose stop frontend backend/, 'Stop must target both services'],
|
|
85
97
|
[/compose ps frontend backend/, 'Status must inspect both services'],
|
|
86
98
|
]) assert(pattern.test(deploy), message);
|
|
87
|
-
assert((deploy.match(/docker build --pull=false/g) ?? []).length === 2, 'Exactly two Docker builds must disable pull');
|
|
99
|
+
assert((deploy.match(/docker build --pull=false --platform "\$DOCKER_PLATFORM"/g) ?? []).length === 2, 'Exactly two Docker builds must use the configured target platform and disable pull');
|
|
100
|
+
const deployStackIndex = deploy.indexOf('deploy_stack() {');
|
|
101
|
+
const serverPlatformCheckIndex = deploy.indexOf(' assert_server_platform\n', deployStackIndex);
|
|
102
|
+
const ownershipCheckIndex = deploy.indexOf(' assert_stack_container_ownership\n', deployStackIndex);
|
|
103
|
+
const firstImageLoadIndex = deploy.indexOf(' docker load -i ', deployStackIndex);
|
|
104
|
+
const composeUpIndex = deploy.indexOf(' compose up -d --force-recreate --no-build --pull never --wait frontend backend', deployStackIndex);
|
|
105
|
+
const runningTimeCheckIndex = deploy.indexOf(' assert_container_cst_time "${PROJECT_NAME}_frontend"', deployStackIndex);
|
|
106
|
+
assert(deployStackIndex >= 0 && serverPlatformCheckIndex > deployStackIndex && ownershipCheckIndex > serverPlatformCheckIndex && firstImageLoadIndex > ownershipCheckIndex, 'Docker Server platform and container ownership must be checked before loading images');
|
|
107
|
+
assert(composeUpIndex > firstImageLoadIndex && runningTimeCheckIndex > composeUpIndex, 'Running container CST time must be checked after Compose health wait');
|
|
108
|
+
const frontendImageTimeIndex = deploy.indexOf(' assert_image_cst_time "$frontend_image"');
|
|
109
|
+
const firstImageSaveIndex = deploy.indexOf(' docker save -o "$package_root/frontend-image.tar"');
|
|
110
|
+
assert(frontendImageTimeIndex >= 0 && firstImageSaveIndex > frontendImageTimeIndex, 'Built image CST time must be checked before Docker save');
|
|
88
111
|
assert(!/\bdocker\s+pull\b/.test(deploy), 'docker pull is forbidden');
|
|
112
|
+
assert(!/\bdocker\s+(?:start|restart)\b/.test(deploy), 'Deployment script must not restart manually stopped containers outside explicit Compose deploy');
|
|
89
113
|
assert(!/\bsource\s+[^\n]*\.env|\beval\b/.test(deploy), 'Environment files must be parsed as data');
|
|
90
114
|
assert(!/server_menu|运行或替换|read_release_pointer|release_ledger|AGENTS\.md/.test(deploy), 'Legacy menu or release ledger logic is forbidden');
|
|
91
115
|
assert(!/\bpsql\b|\b(?:scp|rsync)\b|\bgit\s+(?:pull|clone)\b/.test(deploy), 'Database, upload or source-pull commands are forbidden');
|
|
@@ -95,11 +119,19 @@ const serviceNames = [...compose.matchAll(/^ ([a-z][a-z0-9_-]*):$/gm)].map((mat
|
|
|
95
119
|
assert(JSON.stringify(serviceNames) === JSON.stringify(['frontend', 'backend']), `Compose must define only frontend and backend; got ${serviceNames.join(', ')}`);
|
|
96
120
|
assert(compose.includes('image: "${PROJECT_NAME:?PROJECT_NAME is required}_frontend:latest"'), 'Compose frontend image is not fixed');
|
|
97
121
|
assert(compose.includes('image: "${PROJECT_NAME:?PROJECT_NAME is required}_backend:latest"'), 'Compose backend image is not fixed');
|
|
122
|
+
assert(compose.includes('container_name: "${PROJECT_NAME:?PROJECT_NAME is required}_frontend"'), 'Compose frontend container name must equal its image repository');
|
|
123
|
+
assert(compose.includes('container_name: "${PROJECT_NAME:?PROJECT_NAME is required}_backend"'), 'Compose backend container name must equal its image repository');
|
|
124
|
+
assert((compose.match(/platform: "\$\{DOCKER_PLATFORM:\?DOCKER_PLATFORM is required\}"/g) ?? []).length === 2, 'Both services must use DOCKER_PLATFORM');
|
|
98
125
|
assert((compose.match(/env_file: \.\/\.env/g) ?? []).length === 2, 'Both services must use root .env');
|
|
126
|
+
assert((compose.match(/TZ: "\$\{TZ:\?TZ is required\}"/g) ?? []).length === 2, 'Both services must explicitly use the same required TZ');
|
|
127
|
+
assert((compose.match(/restart: unless-stopped/g) ?? []).length === 2, 'Both services must use restart: unless-stopped');
|
|
99
128
|
assert((compose.match(/^\s*- \.\/logs:\/app\/logs\s*$/gm) ?? []).length === 2, 'Both services must mount root logs');
|
|
100
129
|
assert(compose.includes('"${DOCKER_BIND_IP}:${FRONTEND_PORT}:${FRONTEND_PORT}"'), 'Frontend port mapping is invalid');
|
|
101
130
|
assert(compose.includes('"${DOCKER_BIND_IP}:${BACKEND_PORT}:${BACKEND_PORT}"'), 'Backend port mapping is invalid');
|
|
102
|
-
assert(
|
|
131
|
+
assert(compose.includes('VITE_API_BASE_URL: "${VITE_API_BASE_URL:?VITE_API_BASE_URL is required}"'), 'Frontend API path is not passed to Nginx');
|
|
132
|
+
assert(compose.includes('BACKEND_PORT: "${BACKEND_PORT:?BACKEND_PORT is required}"'), 'Backend port is not passed to Nginx');
|
|
133
|
+
assert(compose.includes('NGINX_ENVSUBST_FILTER: "^(FRONTEND_URL|FRONTEND_PORT|VITE_API_BASE_URL|BACKEND_PORT)$"'), 'Nginx envsubst filter does not cover the runtime API proxy');
|
|
134
|
+
assert(!/\b(?:postgres|database|db):\s*$|postgres:\d|DATABASE_MIGRATION|MIGRATIONS_SOURCE|RELEASE_VERSION|blue|green/i.test(compose), 'Compose contains a database, migration, release or blue-green configuration');
|
|
103
135
|
|
|
104
136
|
const manifestText = await readFile(join(packageRoot, 'manifest.sha256'), 'utf8');
|
|
105
137
|
const manifestEntries = new Map();
|
|
@@ -122,6 +154,9 @@ async function assertImageArchive(name, expectedTag) {
|
|
|
122
154
|
const parsed = JSON.parse(stdout);
|
|
123
155
|
assert(Array.isArray(parsed) && parsed.length === 1, `${name} must contain exactly one image manifest`);
|
|
124
156
|
assert(JSON.stringify(parsed[0].RepoTags) === JSON.stringify([expectedTag]), `${name} must contain only ${expectedTag}`);
|
|
157
|
+
const { stdout: configText } = await run('tar', ['-xOf', archive, parsed[0].Config], { encoding: 'utf8', maxBuffer: 2 * 1024 * 1024 });
|
|
158
|
+
const config = JSON.parse(configText);
|
|
159
|
+
assert(`${config.os}/${config.architecture}` === env.get('DOCKER_PLATFORM'), `${name} platform must equal DOCKER_PLATFORM`);
|
|
125
160
|
}
|
|
126
161
|
await assertImageArchive('frontend-image.tar', `${env.get('PROJECT_NAME')}_frontend:latest`);
|
|
127
162
|
await assertImageArchive('backend-image.tar', `${env.get('PROJECT_NAME')}_backend:latest`);
|