| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <#
- ====================================================================
- Fmode 官网一键发布 —— deploy.ps1(改良版,Windows 本地用)
- 参考原 deploy.ps1 的逻辑重写:增量同步、失败即停、发布后校验、输出在线链接
- 用法(在项目根目录,例如 C:\work\nova\nova-admin):
- .\deploy.ps1 # 只上传 dist\nova-aigc(不构建)
- .\deploy.ps1 -Build # 先 ng build 再上传
- .\deploy.ps1 -Build -Prune # 先清空桶再上传(等同原脚本行为,慎用)
- .\deploy.ps1 -Dist dist\nova-aigc -Bucket www-ai.fmode.cn -Domain ai.fmode.cn
- 凭据(AK/SK)从环境变量读,不写死在脚本里:
- setx OBS_AK "你的AK" :: 只设一次,之后新开终端生效
- setx OBS_SK "你的SK"
- (临时用: $env:OBS_AK="..."; $env:OBS_SK="...")
- ====================================================================
- #>
- [CmdletBinding()]
- param(
- [string]$Dist = "dist\nova-aigc",
- [string]$Bucket = "www-ai.fmode.cn",
- [string]$Domain = "ai.fmode.cn",
- [string]$Prefix = "", # 桶内前缀,根目录留空;带前缀写 xxx(不要斜杠)
- [string]$Endpoint = "obs.cn-south-1.myhuaweicloud.com",
- [string]$CdnRegion = "cn-north-1",
- [switch]$Build,
- [switch]$Prune, # 清空桶内该前缀(破坏性)
- [switch]$SkipRefresh,
- [string]$BuildCmd = "ng build nova-aigc"
- )
- $ErrorActionPreference = "Stop"
- function Ok($m){ Write-Host " [OK] $m" -ForegroundColor Green }
- function Warn($m){ Write-Host " [!] $m" -ForegroundColor Yellow }
- function Die($m){ Write-Host " [X] $m" -ForegroundColor Red; exit 1 }
- if (-not (Get-Command obsutil -ErrorAction SilentlyContinue)) { Die "找不到 obsutil,请先安装并加入 PATH" }
- if (-not $env:OBS_AK -or -not $env:OBS_SK) { Die "请先设置 OBS_AK / OBS_SK 环境变量(凭证不要写进脚本)" }
- $EP = "https://$Endpoint"
- $Key = if ($Prefix) { "$Prefix/" } else { "" }
- $UrlBase = "https://$Domain" + $(if ($Prefix) { "/$Prefix" } else { "" })
- $Ap = @("-i=$env:OBS_AK","-k=$env:OBS_SK","-e=$EP")
- Write-Host "==== 发布 $Domain ====" -ForegroundColor Cyan
- Write-Host " 桶前缀 : obs://$Bucket/$Key"
- Write-Host " 发布目录: $Dist"
- Write-Host " 在线地址: $UrlBase/"
- # ---- 1. 构建 ----
- if ($Build) {
- Write-Host " >> 构建:$BuildCmd"
- $env:NODE_OPTIONS = "--max-old-space-size=24192"
- cmd /c $BuildCmd; if ($LASTEXITCODE -ne 0) { Die "构建失败,已中止(线上保持原样)" }
- Ok "构建完成"
- }
- if (-not (Test-Path (Join-Path $Dist "index.html"))) { Die "发布目录里没有 index.html:$Dist" }
- # ---- 2. 清空(可选,破坏性) ----
- if ($Prune) {
- Warn "清空 obs://$Bucket/$Key …(上传失败会导致线上空窗)"
- & obsutil rm "obs://$Bucket/$Key" -r -f @Ap | Out-Null
- Ok "已清空"
- }
- # ---- 3. 同步上传(public-read) ----
- Write-Host " >> 同步上传…"
- & obsutil sync $Dist "obs://$Bucket/$Key" -acl=public-read @Ap
- if ($LASTEXITCODE -ne 0) { Die "上传失败" }
- & obsutil chattri "obs://$Bucket/$Key" -r -f -acl=public-read @Ap | Out-Null
- Ok "上传完成"
- # ---- 4. 刷 CDN ----
- if (-not $SkipRefresh) {
- if (-not (Get-Command hcloud -ErrorAction SilentlyContinue)) { Warn "找不到 hcloud,跳过 CDN 刷新" }
- else {
- $urls = @("https://$Domain/","https://$Domain/index.html")
- if ($Prefix) { $urls += @("https://$Domain/$Prefix/","https://$Domain/$Prefix/index.html") }
- $args = @(); for ($i=0; $i -lt $urls.Count; $i++) { $args += "--refresh_task.urls.$($i+1)=$($urls[$i])" }
- Write-Host " >> 刷新 CDN:$($urls -join ' ')"
- & hcloud CDN CreateRefreshTasks/v2 --cli-region=$CdnRegion @args --cli-access-key=$env:OBS_AK --cli-secret-key=$env:OBS_SK | Out-Null
- Ok "已提交刷新任务(10~30 秒生效)"
- }
- }
- # ---- 5. 校验 ----
- Start-Sleep -Seconds 15
- $local = (Get-FileHash (Join-Path $Dist "index.html") -Algorithm MD5).Hash.ToLower()
- $resp = Invoke-WebRequest -Uri "$UrlBase/index.html?cb=$(Get-Date -UFormat %s)" -UseBasicParsing
- $live = [BitConverter]::ToString([System.Security.Cryptography.MD5]::Create().ComputeHash($resp.RawContentStream.ToArray())).Replace("-","").ToLower()
- Write-Host "--------------------------------------------------"
- if ($resp.StatusCode -eq 200 -and $live -eq $local) { Ok "发布成功(线上与本地一致)" }
- else { Warn "线上校验不一致:HTTP=$($resp.StatusCode) live=$($live.Substring(0,8)) local=$($local.Substring(0,8))(CDN 可能仍在生效)" }
- Write-Host " 在线地址 : $UrlBase/index.html" -ForegroundColor Cyan
- Write-Host " 站点首页 : $UrlBase/" -ForegroundColor Cyan
- Write-Host "--------------------------------------------------"
|