| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- [CmdletBinding()]
- param(
- [switch]$SkipBuild,
- [switch]$SkipCdnRefresh
- )
- $ErrorActionPreference = 'Stop'
- Set-StrictMode -Version Latest
- $projectName = 'voc-profit'
- $basePath = "/dev/$projectName/"
- $bucketPath = "obs://nova-cloud/dev/$projectName"
- $publicUrl = "https://app.fmode.cn$basePath"
- $clientPath = Join-Path $PSScriptRoot 'client'
- $distPath = Join-Path $clientPath 'dist\voc-profit-web\browser'
- $obsutil = 'E:\obsutil_windows_amd64_5.7.9\obsutil.exe'
- $hcloud = 'E:\huaweicloud-cli-windows-amd64\hcloud.exe'
- function Get-RequiredEnvironmentVariable {
- param([Parameter(Mandatory)][string]$Name)
- $value = [Environment]::GetEnvironmentVariable($Name)
- if ([string]::IsNullOrWhiteSpace($value)) {
- throw "Missing required environment variable: $Name"
- }
- return $value
- }
- function Invoke-CheckedCommand {
- param(
- [Parameter(Mandatory)][string]$FilePath,
- [Parameter(Mandatory)][string[]]$CommandArguments
- )
- & $FilePath @CommandArguments
- if ($LASTEXITCODE -ne 0) {
- throw "Command failed with exit code ${LASTEXITCODE}: $FilePath"
- }
- }
- if (-not (Test-Path -LiteralPath $obsutil -PathType Leaf)) {
- throw "obsutil was not found: $obsutil"
- }
- if (-not $SkipCdnRefresh -and -not (Test-Path -LiteralPath $hcloud -PathType Leaf)) {
- throw "hcloud was not found: $hcloud"
- }
- $obsAccessKey = Get-RequiredEnvironmentVariable 'VOC_PROFIT_OBS_ACCESS_KEY'
- $obsSecretKey = Get-RequiredEnvironmentVariable 'VOC_PROFIT_OBS_SECRET_KEY'
- $obsEndpoint = if ($env:VOC_PROFIT_OBS_ENDPOINT) { $env:VOC_PROFIT_OBS_ENDPOINT } else { 'obs.cn-south-1.myhuaweicloud.com' }
- Push-Location $clientPath
- try {
- if (-not $SkipBuild) {
- Invoke-CheckedCommand -FilePath 'npm.cmd' -CommandArguments @('run', 'build', '--', "--base-href=$basePath")
- }
- if (-not (Test-Path -LiteralPath (Join-Path $distPath 'index.html') -PathType Leaf)) {
- throw "Build output is missing: $distPath\index.html"
- }
- $obsAuth = @("-i=$obsAccessKey", "-k=$obsSecretKey", "-e=$obsEndpoint")
- Invoke-CheckedCommand -FilePath $obsutil -CommandArguments (@('sync', $distPath, $bucketPath, '-acl=public-read') + $obsAuth)
- Invoke-CheckedCommand -FilePath $obsutil -CommandArguments (@('chattri', $bucketPath, '-r', '-f', '-acl=public-read') + $obsAuth)
- if (-not $SkipCdnRefresh) {
- $cdnAccessKey = Get-RequiredEnvironmentVariable 'VOC_PROFIT_CDN_ACCESS_KEY'
- $cdnSecretKey = Get-RequiredEnvironmentVariable 'VOC_PROFIT_CDN_SECRET_KEY'
- $cdnRegion = if ($env:VOC_PROFIT_CDN_REGION) { $env:VOC_PROFIT_CDN_REGION } else { 'cn-north-1' }
- Invoke-CheckedCommand -FilePath $hcloud -CommandArguments @(
- 'CDN',
- 'CreateRefreshTasks/v2',
- "--cli-region=$cdnRegion",
- "--refresh_task.urls.1=$publicUrl",
- '--refresh_task.type=directory',
- "--cli-access-key=$cdnAccessKey",
- "--cli-secret-key=$cdnSecretKey"
- )
- }
- Write-Host "Deployment completed: $publicUrl" -ForegroundColor Green
- }
- finally {
- Pop-Location
- }
|