deploy.ps1 4.7 KB

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