You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dianshang-houduan/docs/superpowers/plans/2026-07-29-windows-ssh-depl...

231 lines
11 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Windows SSH 一键部署 Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** 用一条 PowerShell 命令构建并发布 Vue 前端与 Spring Boot 后端到 Windows 服务器。
**Architecture:** 本地入口负责检查、构建、压缩和 SSH/SCP 传输;服务器端脚本负责备份、替换、计划任务重启与 Nginx 平滑重载。数据库参数只保存在服务器私密 PSD1 文件。
**Tech Stack:** PowerShell 5.1、OpenSSH、Windows Task Scheduler、Pester、Nginx、Node/Vite、Maven、Java 8。
## Global Constraints
- 所有部署脚本位于 `E:\workspace\dianshang\deploy\automation`,不得改动前后端业务代码。
- 前端为 `e-commerce-internet`,命令为 `npm run build:prod`,产物为 `dist`;后端为 `link_commerce`,命令为 `mvn clean package -DskipTests`,产物为 `target\link_commerce-2.7.12.jar`。
- 远程前端目录为 `D:\nginx-1.19.3.1\html\dist`,后端目录为 `D:\dianshang-test-http-deploy-20260629-093129`,后端端口为 `7548`。
- 不写入或输出任何 SSH 密码、数据库密码或私钥;真实 `deploy.local.psd1` 和 `backend.private.psd1` 均不提交。
- Nginx 重载前必须成功执行 `D:\nginx-1.19.3.1\nginx.exe -t`;发布成功前必须确认 7548 监听且 HTTP 返回 200–399。
---
### Task 1: 建立本地配置和测试基座
**Files:**
- Create: `E:\workspace\dianshang\deploy\automation\deploy.local.psd1.example`
- Create: `E:\workspace\dianshang\deploy\automation\.gitignore`
- Create: `E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1`
**Interfaces:**
- Produces: `Assert-DeploymentConfig -Config <hashtable>`,要求字段为 `ServerHost`、`ServerUser`、`SshPort`、`SshKeyPath`、`RemoteStageRoot`。
- [ ] **Step 1: 写入失败测试**
```powershell
. "$PSScriptRoot\..\deploy.ps1"
Describe 'Assert-DeploymentConfig' {
It '拒绝不存在的 SSH 私钥' {
$config = @{ ServerHost='120.79.161.177'; ServerUser='Administrator'; SshPort=22; SshKeyPath='Z:\missing'; RemoteStageRoot='D:\deployment-stage' }
{ Assert-DeploymentConfig -Config $config } | Should -Throw '*SSH private key*'
}
It '拒绝含密码字段的配置' {
$config = @{ ServerHost='120.79.161.177'; ServerUser='Administrator'; SshPort=22; SshKeyPath=$TestDrive; RemoteStageRoot='D:\deployment-stage'; DatabasePassword='unsafe' }
{ Assert-DeploymentConfig -Config $config } | Should -Throw '*DatabasePassword*'
}
}
```
- [ ] **Step 2: 运行测试确认红灯**
Run: `Invoke-Pester -Path E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1 -Output Detailed`
Expected: FAIL,原因是 `deploy.ps1` 与 `Assert-DeploymentConfig` 尚不存在。
- [ ] **Step 3: 创建非敏感模板和忽略规则**
模板内容为以下哈希表:
```powershell
@{ ServerHost='120.79.161.177'; ServerUser='Administrator'; SshPort=22; SshKeyPath='C:\Users\<WindowsUser>\.ssh\id_ed25519'; RemoteStageRoot='D:\deployment-stage' }
```
`.gitignore` 精确包含:
```text
deploy.local.psd1
artifacts/
*.zip
```
- [ ] **Step 4: 再次运行测试**
Run: `Invoke-Pester -Path E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1 -Output Detailed`
Expected: FAIL,错误仍只指向缺失的入口函数。
### Task 2: 实现本机构建入口
**Files:**
- Create: `E:\workspace\dianshang\deploy\automation\deploy.ps1`
- Modify: `E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1`
**Interfaces:**
- Produces: `Invoke-CheckedNative -FilePath <string> -Arguments <string[]> -Activity <string>` 和 `Invoke-Deployment -ConfigPath <string>`。
- [ ] **Step 1: 加入失败测试,要求构建失败绝不上传**
```powershell
It '前端构建失败时不调用 scp' {
Mock Invoke-CheckedNative { throw 'frontend build failed' } -ParameterFilter { $Activity -eq 'Build frontend' }
Mock Invoke-CheckedNative {} -ParameterFilter { $FilePath -eq 'scp' }
{ Invoke-Deployment -ConfigPath "$TestDrive\deploy.local.psd1" } | Should -Throw '*frontend build failed*'
Should -Invoke Invoke-CheckedNative -ParameterFilter { $FilePath -eq 'scp' } -Times 0
}
```
- [ ] **Step 2: 运行测试确认红灯**
Run: `Invoke-Pester -Path E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1 -Output Detailed`
Expected: FAIL,`Invoke-Deployment` 未定义。
- [ ] **Step 3: 最小实现入口**
实现 `Assert-DeploymentConfig`,拒绝缺字段、无效端口、不存在的私钥以及名含 `password|secret|token` 的字段。实现 `Invoke-CheckedNative`,在命令退出码非 0 时抛出活动名称。实现 `Invoke-Deployment` 前半段:验证 `ssh`、`scp`、`npm`、`mvn` 存在,依次运行前端和后端构建,确认 `dist\index.html` 和 JAR 存在;此时不调用 SCP。
- [ ] **Step 4: 运行测试确认绿灯**
Run: `Invoke-Pester -Path E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1 -Output Detailed`
Expected: PASS,且 `scp` 调用次数为 0。
### Task 3: 实现服务器初始化和后端守护
**Files:**
- Create: `E:\workspace\dianshang\deploy\automation\server\Initialize-CommerceDeployment.ps1`
- Create: `E:\workspace\dianshang\deploy\automation\server\Start-CommerceBackend.ps1`
- Create: `E:\workspace\dianshang\deploy\automation\server\backend.private.psd1.example`
- Modify: `E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1`
**Interfaces:**
- Consumes: `D:\dianshang-test-http-deploy-20260629-093129\backend.private.psd1` 中的 `DatasourceUrl`、`DatasourceUser`、`DatasourcePassword`。
- Produces: 计划任务 `CommerceBackend` 和服务器路径 `D:\dianshang-test-http-deploy-20260629-093129\scripts\Start-CommerceBackend.ps1`。
- [ ] **Step 1: 写入失败测试,禁止密码作为启动脚本参数**
```powershell
It '后端启动脚本从本地私密文件读取密码' {
$content = Get-Content "$PSScriptRoot\..\server\Start-CommerceBackend.ps1" -Raw
$content | Should -Not -Match 'param\([^)]*Password'
$content | Should -Match 'backend\.private\.psd1'
$content | Should -Match 'Start-Process'
}
```
- [ ] **Step 2: 运行测试确认红灯**
Run: `Invoke-Pester -Path E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1 -Output Detailed`
Expected: FAIL,`Start-CommerceBackend.ps1` 不存在。
- [ ] **Step 3: 最小实现服务器脚本**
初始化脚本创建 `scripts`、`logs`、`backups`、`incoming`,只在私密配置不存在时写入模板,并以 `schtasks /Create /TN CommerceBackend /SC ONSTART /RU SYSTEM /RL HIGHEST` 注册计划任务。启动脚本导入私密 PSD1,以 `Get-CimInstance Win32_Process` 只结束命令行包含目标 JAR 完整路径的 Java 进程,再用 `Start-Process java.exe` 启动并重定向标准输出与错误输出至 `logs`。
- [ ] **Step 4: 运行测试确认绿灯**
Run: `Invoke-Pester -Path E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1 -Output Detailed`
Expected: PASS,启动脚本只引用服务器私密 PSD1 且使用后台进程启动。
### Task 4: 实现远程安全发布与 Nginx 重载
**Files:**
- Create: `E:\workspace\dianshang\deploy\automation\server\Publish-Commerce.ps1`
- Modify: `E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1`
**Interfaces:**
- Consumes: `Publish-Commerce.ps1 -ReleaseZip <string>`,ZIP 内含 `frontend\index.html` 与 `backend\link_commerce-2.7.12.jar`。
- Produces: `D:\dianshang-test-http-deploy-20260629-093129\backups\<timestamp>` 和成功标记 `DEPLOY_SUCCESS`。
- [ ] **Step 1: 写入失败测试,规定检查先于重载**
```powershell
It 'Nginx 重载前必须运行配置检查' {
$content = Get-Content "$PSScriptRoot\..\server\Publish-Commerce.ps1" -Raw
$content.IndexOf('nginx.exe -t') | Should -BeGreaterThan -1
$content.IndexOf('nginx.exe -s reload') | Should -BeGreaterThan $content.IndexOf('nginx.exe -t')
}
```
- [ ] **Step 2: 运行测试确认红灯**
Run: `Invoke-Pester -Path E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1 -Output Detailed`
Expected: FAIL,`Publish-Commerce.ps1` 不存在。
- [ ] **Step 3: 最小实现远程发布**
固定顺序为:验证 ZIP;解压到同盘临时目录;验证前端入口和 JAR;备份当前前端目录与 JAR;替换文件;执行 `schtasks /Run /TN CommerceBackend`;最多 60 秒检查 `127.0.0.1:7548`;在 Nginx 根目录运行 `nginx.exe -t` 后运行 `nginx.exe -s reload`;使用 `Invoke-WebRequest http://127.0.0.1/ -UseBasicParsing` 检查状态 200–399;输出 `DEPLOY_SUCCESS` 和备份路径。任何失败必须保留备份并抛出错误。
- [ ] **Step 4: 运行测试确认绿灯**
Run: `Invoke-Pester -Path E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1 -Output Detailed`
Expected: PASS,检查文本位置位于重载文本之前。
### Task 5: 接通上传链路并做真实发布验证
**Files:**
- Modify: `E:\workspace\dianshang\deploy\automation\deploy.ps1`
- Modify: `E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1`
**Interfaces:**
- Produces: `powershell -ExecutionPolicy Bypass -File E:\workspace\dianshang\deploy\automation\deploy.ps1`。
- [ ] **Step 1: 写入失败测试,规定成功路径使用 SCP 和 SSH**
```powershell
It '构建成功后上传 ZIP 并调用远程发布' {
Mock Invoke-CheckedNative {}
Invoke-Deployment -ConfigPath "$TestDrive\deploy.local.psd1"
Should -Invoke Invoke-CheckedNative -ParameterFilter { $FilePath -eq 'scp' -and ($Arguments -join ' ') -match 'release-' } -Times 1
Should -Invoke Invoke-CheckedNative -ParameterFilter { $FilePath -eq 'ssh' -and ($Arguments -join ' ') -match 'Publish-Commerce.ps1' } -Times 1
}
```
- [ ] **Step 2: 运行测试确认红灯**
Run: `Invoke-Pester -Path E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1 -Output Detailed`
Expected: FAIL,未找到 `scp` 与 `ssh` 调用。
- [ ] **Step 3: 最小实现压缩、上传和远程调用**
两个构建成功后创建 `artifacts\release-<yyyyMMdd-HHmmss>.zip`,顶层目录固定为 `frontend` 和 `backend`。通过 SCP 上传服务器脚本与 ZIP 到 `D:\deployment-stage`,再通过 SSH 调用 `Publish-Commerce.ps1 -ReleaseZip <uploaded path>`;远程输出不含 `DEPLOY_SUCCESS` 时入口必须失败。
- [ ] **Step 4: 运行 Pester 全量测试**
Run: `Invoke-Pester -Path E:\workspace\dianshang\deploy\automation\tests\DeployScript.Tests.ps1 -Output Detailed`
Expected: PASS,所有测试通过,输出不含私密配置值。
- [ ] **Step 5: 生成密钥、初始化服务器并验证真实发布**
Run: `ssh-keygen -t ed25519 -f $env:USERPROFILE\.ssh\id_ed25519 -C commerce-deploy`
Run: `powershell -ExecutionPolicy Bypass -File E:\workspace\dianshang\deploy\automation\deploy.ps1 -InitializeOnly`
Run: `powershell -ExecutionPolicy Bypass -File E:\workspace\dianshang\deploy\automation\deploy.ps1`
Expected: 初始化后存在 `CommerceBackend` 计划任务;真实发布输出 `DEPLOY_SUCCESS`、备份路径、7548 端口监听和 Nginx HTTP 200–399。