param( [string]$KeyFile = '', [string]$RemoteHost = 'root@119.45.34.106', [string]$RemoteDir = '/opt/canteen-frontend' ) $ErrorActionPreference = 'Stop' $root = Split-Path -Parent $PSScriptRoot Set-Location $root . (Join-Path $PSScriptRoot 'tencent-prod-env.ps1') $prodEnv = Read-TencentProdEnv -Root $root if (-not $PSBoundParameters.ContainsKey('RemoteHost')) { $RemoteHost = "root@$($prodEnv.H5_IP)" } if (-not $KeyFile) { $KeyFile = Join-Path $root 'deploy\id_rsa_tencent' } if (-not (Test-Path $KeyFile)) { Write-Error "SSH key not found: $KeyFile" } Write-Host 'Building frontend...' -ForegroundColor Cyan npm run build if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } $distDir = Join-Path $root 'dist' $bundleFileCount = 0 $uploadFiles = @( (Join-Path $distDir 'index.html'), (Join-Path $distDir 'config.js') ) Write-Host "Deploying to $RemoteHost ..." -ForegroundColor Cyan & ssh -i $KeyFile -o StrictHostKeyChecking=accept-new $RemoteHost "mkdir -p $RemoteDir" foreach ($f in $uploadFiles) { & scp -i $KeyFile -o StrictHostKeyChecking=accept-new $f "${RemoteHost}:${RemoteDir}/" if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } } Get-ChildItem -Path (Join-Path $distDir '*.js') | Where-Object { $_.Name -ne 'config.js' } | ForEach-Object { $bundleFileCount += 1 & scp -i $KeyFile -o StrictHostKeyChecking=accept-new $_.FullName "${RemoteHost}:${RemoteDir}/" if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } } if ($bundleFileCount -eq 0) { Write-Error "No *.js found in dist/" } $cssFileCount = 0 Get-ChildItem -Path (Join-Path $distDir '*.css') | ForEach-Object { $cssFileCount += 1 & scp -i $KeyFile -o StrictHostKeyChecking=accept-new $_.FullName "${RemoteHost}:${RemoteDir}/" if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } } if ($cssFileCount -eq 0) { Write-Error "No *.css found in dist/" } $nginxConf = Join-Path $root 'deploy\nginx.canteen.conf' $nginxScript = Join-Path $root 'scripts\setup-prod-frontend-nginx.sh' if ((Test-Path $nginxConf) -and (Test-Path $nginxScript)) { $renderedNginx = Write-RenderedFrontendNginx -Root $root -ApiPrivateIp $prodEnv.API_PRIVATE_IP Write-Host "Frontend nginx -> API internal $($prodEnv.API_PRIVATE_IP):3001" -ForegroundColor DarkGray & scp -i $KeyFile -o StrictHostKeyChecking=accept-new $renderedNginx "${RemoteHost}:${RemoteDir}/nginx.canteen.conf" Remove-Item $renderedNginx -Force -ErrorAction SilentlyContinue & scp -i $KeyFile -o StrictHostKeyChecking=accept-new $nginxScript "${RemoteHost}:/tmp/setup-prod-frontend-nginx.sh" & ssh -i $KeyFile -o StrictHostKeyChecking=accept-new $RemoteHost "chmod +x /tmp/setup-prod-frontend-nginx.sh && bash /tmp/setup-prod-frontend-nginx.sh ${RemoteDir}/nginx.canteen.conf" } Write-Host 'Done. Normal refresh (F5) will load the latest version.' -ForegroundColor Green