- deploy-iis.ps1 / update-iis.ps1 的发布路径由项目内 publish 改为 C:\F9WebAiTest-IIS - 站点物理路径已切换并验证,发布物与代码目录分离,便于测试与备份
80 lines
3.7 KiB
PowerShell
80 lines
3.7 KiB
PowerShell
# ============================================================
|
||
# 一键更新 IIS 上的「智慧缫丝系统」(f9-web-ai-test)
|
||
# 改动代码后运行:停站点 → 重新发布后端 → 同步前端产物 → 启站点 → 自检
|
||
# 前置:先执行过 deploy-iis.ps1;前端已用 npm run build 构建
|
||
# 必须以【管理员身份】运行(停/启 IIS 站点、替换被占用的程序文件)
|
||
# ============================================================
|
||
$ErrorActionPreference = 'Stop'
|
||
|
||
$root = 'C:\Users\32453\CodeBuddy\20260825142915'
|
||
$webDir = Join-Path $root 'web'
|
||
$distDir = Join-Path $webDir 'dist'
|
||
$apiProject = Join-Path $root 'backend\F9WebAiTest.Api'
|
||
# 发布物放在项目外的独立目录,与代码分离(便于测试与备份)
|
||
$publishDir = 'C:\F9WebAiTest-IIS'
|
||
$appPoolName = 'F9WebAiTest'
|
||
$siteName = 'F9WebAiTest'
|
||
|
||
$logDir = Join-Path $root '_runlog'
|
||
New-Item -ItemType Directory -Force -Path $logDir | Out-Null
|
||
try { Start-Transcript -Path (Join-Path $logDir 'update-iis.log') -Force -ErrorAction Stop | Out-Null } catch { }
|
||
|
||
function Info($m) { Write-Host "[更新] $m" -ForegroundColor Cyan }
|
||
function Warn($m) { Write-Host "[警告] $m" -ForegroundColor Yellow }
|
||
|
||
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
||
throw '请以管理员身份运行本脚本'
|
||
}
|
||
if (-not (Test-Path $distDir)) { throw "前端产物不存在:$distDir(请先在 web 目录执行 npm run build)" }
|
||
|
||
# 必须先加载 WebAdministration,IIS: 驱动器才存在(否则站点判断会误报不存在)
|
||
Import-Module WebAdministration
|
||
if (-not (Get-Website -Name $siteName -ErrorAction SilentlyContinue)) { throw "IIS 站点 $siteName 不存在,请先运行 deploy-iis.ps1" }
|
||
|
||
# ---------- 1. 停站点与应用池(释放 dll 与数据库文件占用)----------
|
||
if ((Get-Website -Name $siteName).State -ne 'Stopped') { Stop-Website -Name $siteName }
|
||
if ((Get-WebAppPoolState -Name $appPoolName).Value -ne 'Stopped') { Stop-WebAppPool -Name $appPoolName }
|
||
Start-Sleep -Seconds 3
|
||
Info '站点已停止,文件已释放'
|
||
|
||
# ---------- 2. 发布后端(Release)----------
|
||
Info '发布后端(Release)...'
|
||
& 'C:\Program Files\dotnet\dotnet.exe' publish $apiProject -c Release -o $publishDir 2>&1 |
|
||
Where-Object { $_ -match 'error|已成功|-> ' } | Select-Object -Last 5
|
||
if ($LASTEXITCODE -ne 0) { throw "dotnet publish 失败(退出码 $LASTEXITCODE)" }
|
||
|
||
# ---------- 3. 同步前端产物到 wwwroot ----------
|
||
$wwwroot = Join-Path $publishDir 'wwwroot'
|
||
if (Test-Path $wwwroot) { Remove-Item $wwwroot -Recurse -Force }
|
||
New-Item -ItemType Directory -Force -Path $wwwroot | Out-Null
|
||
Copy-Item (Join-Path $distDir '*') $wwwroot -Recurse -Force
|
||
Info "前端产物已同步:$wwwroot"
|
||
|
||
# ---------- 4. 目录权限兜底 ----------
|
||
& icacls $publishDir /grant "IIS AppPool\${appPoolName}:(OI)(CI)M" /T /Q | Out-Null
|
||
|
||
# ---------- 5. 启动站点 ----------
|
||
Start-WebAppPool -Name $appPoolName
|
||
Start-Website -Name $siteName
|
||
Start-Sleep -Seconds 5
|
||
Info '站点已启动'
|
||
|
||
# ---------- 6. 自检 ----------
|
||
$url = 'http://localhost/'
|
||
try {
|
||
$r = Invoke-WebRequest -Uri $url -UseBasicParsing -TimeoutSec 40
|
||
Info "首页:HTTP $($r.StatusCode)"
|
||
} catch {
|
||
Warn "首页访问失败:$($_.Exception.Message)"
|
||
}
|
||
try {
|
||
$login = Invoke-RestMethod -Uri "${url}api/auth/login" -Method Post -ContentType 'application/json' `
|
||
-Body '{"userName":"admin","password":"123456"}' -TimeoutSec 40
|
||
Info "登录接口:code=$($login.code) $($login.message)"
|
||
} catch {
|
||
Warn "登录接口调用失败:$($_.Exception.Message)"
|
||
}
|
||
|
||
Write-Host ''
|
||
Info "更新完成,访问地址:$url"
|