deploy.ps1 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. [CmdletBinding()]
  2. param(
  3. [switch]$SkipBuild,
  4. [switch]$SkipCdnRefresh
  5. )
  6. $ErrorActionPreference = 'Stop'
  7. Set-StrictMode -Version Latest
  8. $projectName = 'voc-profit'
  9. $basePath = "/dev/$projectName/"
  10. $bucketPath = "obs://nova-cloud/dev/$projectName"
  11. $publicUrl = "https://app.fmode.cn$basePath"
  12. $clientPath = Join-Path $PSScriptRoot 'client'
  13. $distPath = Join-Path $clientPath 'dist\voc-profit-web\browser'
  14. $obsutil = 'E:\obsutil_windows_amd64_5.7.9\obsutil.exe'
  15. $hcloud = 'E:\huaweicloud-cli-windows-amd64\hcloud.exe'
  16. function Get-RequiredEnvironmentVariable {
  17. param([Parameter(Mandatory)][string]$Name)
  18. $value = [Environment]::GetEnvironmentVariable($Name)
  19. if ([string]::IsNullOrWhiteSpace($value)) {
  20. throw "Missing required environment variable: $Name"
  21. }
  22. return $value
  23. }
  24. function Invoke-CheckedCommand {
  25. param(
  26. [Parameter(Mandatory)][string]$FilePath,
  27. [Parameter(Mandatory)][string[]]$CommandArguments
  28. )
  29. & $FilePath @CommandArguments
  30. if ($LASTEXITCODE -ne 0) {
  31. throw "Command failed with exit code ${LASTEXITCODE}: $FilePath"
  32. }
  33. }
  34. if (-not (Test-Path -LiteralPath $obsutil -PathType Leaf)) {
  35. throw "obsutil was not found: $obsutil"
  36. }
  37. if (-not $SkipCdnRefresh -and -not (Test-Path -LiteralPath $hcloud -PathType Leaf)) {
  38. throw "hcloud was not found: $hcloud"
  39. }
  40. $obsAccessKey = Get-RequiredEnvironmentVariable 'VOC_PROFIT_OBS_ACCESS_KEY'
  41. $obsSecretKey = Get-RequiredEnvironmentVariable 'VOC_PROFIT_OBS_SECRET_KEY'
  42. $obsEndpoint = if ($env:VOC_PROFIT_OBS_ENDPOINT) { $env:VOC_PROFIT_OBS_ENDPOINT } else { 'obs.cn-south-1.myhuaweicloud.com' }
  43. Push-Location $clientPath
  44. try {
  45. if (-not $SkipBuild) {
  46. Invoke-CheckedCommand -FilePath 'npm.cmd' -CommandArguments @('run', 'build', '--', "--base-href=$basePath")
  47. }
  48. if (-not (Test-Path -LiteralPath (Join-Path $distPath 'index.html') -PathType Leaf)) {
  49. throw "Build output is missing: $distPath\index.html"
  50. }
  51. $obsAuth = @("-i=$obsAccessKey", "-k=$obsSecretKey", "-e=$obsEndpoint")
  52. Invoke-CheckedCommand -FilePath $obsutil -CommandArguments (@('sync', $distPath, $bucketPath, '-acl=public-read') + $obsAuth)
  53. Invoke-CheckedCommand -FilePath $obsutil -CommandArguments (@('chattri', $bucketPath, '-r', '-f', '-acl=public-read') + $obsAuth)
  54. if (-not $SkipCdnRefresh) {
  55. $cdnAccessKey = Get-RequiredEnvironmentVariable 'VOC_PROFIT_CDN_ACCESS_KEY'
  56. $cdnSecretKey = Get-RequiredEnvironmentVariable 'VOC_PROFIT_CDN_SECRET_KEY'
  57. $cdnRegion = if ($env:VOC_PROFIT_CDN_REGION) { $env:VOC_PROFIT_CDN_REGION } else { 'cn-north-1' }
  58. Invoke-CheckedCommand -FilePath $hcloud -CommandArguments @(
  59. 'CDN',
  60. 'CreateRefreshTasks/v2',
  61. "--cli-region=$cdnRegion",
  62. "--refresh_task.urls.1=$publicUrl",
  63. '--refresh_task.type=directory',
  64. "--cli-access-key=$cdnAccessKey",
  65. "--cli-secret-key=$cdnSecretKey"
  66. )
  67. }
  68. Write-Host "Deployment completed: $publicUrl" -ForegroundColor Green
  69. }
  70. finally {
  71. Pop-Location
  72. }