99 lines
3.2 KiB
PowerShell
99 lines
3.2 KiB
PowerShell
param(
|
|
[string]$HostIp = $env:MOTORSIGNAL_PROD_HOST,
|
|
[string]$User = $(if ($env:MOTORSIGNAL_PROD_USER) { $env:MOTORSIGNAL_PROD_USER } else { "motorsignal-admin" }),
|
|
[string]$Key = $env:MOTORSIGNAL_PROD_SSH_KEY,
|
|
[switch]$BuildRemote
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
if (-not $HostIp) {
|
|
Write-Error "HostIp parameter or MOTORSIGNAL_PROD_HOST environment variable is required."
|
|
exit 1
|
|
}
|
|
|
|
$SshKeyArgs = @()
|
|
if ($Key) {
|
|
if (-not (Test-Path $Key)) {
|
|
Write-Error "Specified SSH key file not found: $Key"
|
|
exit 1
|
|
}
|
|
$SshKeyArgs = @("-i", $Key)
|
|
}
|
|
|
|
$Timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
|
|
|
|
if ($BuildRemote) {
|
|
# Ultra-lean mode: Upload only source files (~100 KB total)
|
|
$TarName = "website-src-$Timestamp.tar.gz"
|
|
$TempTar = "$PSScriptRoot\$TarName"
|
|
|
|
Write-Host ">>> 1. Packaging source code (~100 KB)..." -ForegroundColor Cyan
|
|
tar -czf "$TempTar" -C website app components lib public package.json tsconfig.json next.config.ts
|
|
|
|
Write-Host ">>> 2. Uploading source code to server..." -ForegroundColor Cyan
|
|
scp @SshKeyArgs "$TempTar" "${User}@${HostIp}:/tmp/$TarName"
|
|
Remove-Item "$TempTar" -Force
|
|
|
|
Write-Host ">>> 3. Building and restarting on server..." -ForegroundColor Cyan
|
|
$RemoteCmd = @"
|
|
bash -c '
|
|
set -euo pipefail
|
|
TAR=/tmp/$TarName
|
|
TARGET=/opt/motorsignal/current/website
|
|
|
|
echo "Updating source files in `$TARGET..."
|
|
sudo tar -xzf "`$TAR" -C "`$TARGET"
|
|
rm -f "`$TAR"
|
|
|
|
cd "`$TARGET"
|
|
echo "Running Next.js production build on server..."
|
|
npm run build
|
|
|
|
echo "Restarting motorsignal-website service..."
|
|
sudo systemctl restart motorsignal-website
|
|
echo "Done!"
|
|
'
|
|
"@
|
|
ssh -t @SshKeyArgs "${User}@${HostIp}" $RemoteCmd
|
|
} else {
|
|
# Fast local build: Strip Next.js cache & dev artifacts (~2 MB total instead of 500 MB)
|
|
Write-Host ">>> 1. Building Next.js application locally..." -ForegroundColor Cyan
|
|
Set-Location -Path "$PSScriptRoot\..\website"
|
|
npm run build
|
|
Set-Location -Path "$PSScriptRoot\.."
|
|
|
|
$TarName = "website-update-$Timestamp.tar.gz"
|
|
$TempTar = "$PSScriptRoot\$TarName"
|
|
|
|
tar -czf "$TempTar" --exclude="cache" --exclude="dev" --exclude="standalone" -C website .next/server .next/static .next/types .next/BUILD_ID .next/*.json public package.json app components lib
|
|
|
|
$SizeMb = [math]::Round(((Get-Item "$TempTar").Length / 1MB), 2)
|
|
Write-Host ">>> 3. Uploading $SizeMb MB to server..." -ForegroundColor Cyan
|
|
scp @SshKeyArgs "$TempTar" "${User}@${HostIp}:/tmp/$TarName"
|
|
Remove-Item "$TempTar" -Force
|
|
|
|
Write-Host ">>> 4. Activating update on server..." -ForegroundColor Cyan
|
|
$RemoteCmd = @"
|
|
bash -c '
|
|
set -euo pipefail
|
|
TAR=/tmp/$TarName
|
|
TARGET=/opt/motorsignal/current/website
|
|
|
|
echo "Extracting runtime to `$TARGET..."
|
|
sudo tar -xzf "`$TAR" -C "`$TARGET"
|
|
rm -f "`$TAR"
|
|
|
|
echo "Restarting motorsignal-website service..."
|
|
sudo systemctl restart motorsignal-website
|
|
echo "Service restarted."
|
|
'
|
|
"@
|
|
ssh -t @SshKeyArgs "${User}@${HostIp}" $RemoteCmd
|
|
}
|
|
|
|
Write-Host ">>> 5. Checking website response..." -ForegroundColor Green
|
|
Start-Sleep -Seconds 2
|
|
$StatusCode = (Invoke-WebRequest -Uri "https://motorsignal.app/account" -Method Head -UseBasicParsing).StatusCode
|
|
Write-Host ">>> Deployed successfully! HTTP Status: $StatusCode" -ForegroundColor Green
|