HRM-10/deploy.ps1
Mohaimen Hasan 838b8d4480 first commit
2026-11-03 13:06:00 +06:00

215 lines
7.9 KiB
PowerShell

# ============================================================
# CEL HRM - Full Build & Deploy Script (Backend + Frontend)
# Run this ONE script to build everything and produce a ready-
# to-zip publish folder, right inside this repo.
#
# Usage: In terminal, from the repo root -> .\deploy.ps1
# ============================================================
# ---- CONFIGURATION ---- #
# Update these values for each client/environment before running
$baseHref = "/CELHRM_Master/"
$outputFolder = "publish\CELHRM_Master" # relative to this script's folder (repo root)
# These never change
$repoRoot = $PSScriptRoot
$backendProject = Join-Path $repoRoot "HRM.UI\HRM.UI.csproj"
$clientAppPath = Join-Path $repoRoot "HRM.UI\ClientApp"
$versionFilePath = Join-Path $clientAppPath "src\version.json"
$distPath = Join-Path $clientAppPath "dist\babylon\*"
$destinationPath = Join-Path $repoRoot $outputFolder
$wwwrootPath = Join-Path $destinationPath "wwwroot"
# ------------------------ #
# ---- STEP 1: Generate version number ---- #
# Format: YYYYMMDD-NN (e.g. 20260517-01, 20260517-02)
# Counter auto-increments if deployed multiple times on the same day
$today = Get-Date -Format "yyyyMMdd"
$lastCounter = 0
if (Test-Path $versionFilePath) {
$existing = Get-Content $versionFilePath | ConvertFrom-Json
if ($existing.version -match "^(\d{8})-(\d{2})$") {
$existingDate = $Matches[1]
$existingCounter = [int]$Matches[2]
# Same day -> increment counter | New day -> reset to 1
if ($existingDate -eq $today) {
$lastCounter = $existingCounter
}
}
}
$newCounter = $lastCounter + 1
$version = "$today-" + $newCounter.ToString("D2") # D2 = always 2 digits (01, 02, 03...)
$buildTime = (Get-Date).ToUniversalTime().ToString("o")
Write-Host ""
Write-Host "============================================" -ForegroundColor Cyan
Write-Host " CEL HRM - Full Build & Deploy Script" -ForegroundColor Cyan
Write-Host " Version : $version" -ForegroundColor Yellow
Write-Host " BaseHref : $baseHref" -ForegroundColor Gray
Write-Host " Output : $destinationPath" -ForegroundColor Gray
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
# ---- STEP 2: Update src/version.json before build ---- #
# This file gets copied into the dist/ folder during build
# VersionCheckService reads this file to detect new deployments
$versionJson = [PSCustomObject]@{
version = $version
buildTime = $buildTime
} | ConvertTo-Json
$versionJson | Out-File -FilePath $versionFilePath -Encoding utf8
Write-Host "[OK] version.json updated -> $version" -ForegroundColor Green
# ---- STEP 3: Angular production build ---- #
Write-Host "[..] Running Angular production build..." -ForegroundColor Gray
Write-Host ""
Push-Location $clientAppPath
ng build --configuration production --base-href $baseHref
$ngExitCode = $LASTEXITCODE
Pop-Location
if ($ngExitCode -ne 0) {
Write-Host ""
Write-Host "============================================" -ForegroundColor Red
Write-Host " [ERROR] Angular build FAILED!" -ForegroundColor Red
Write-Host " Fix the errors above and try again." -ForegroundColor Red
Write-Host "============================================" -ForegroundColor Red
exit 1
}
Write-Host ""
Write-Host "[OK] Angular build successful" -ForegroundColor Green
# ---- STEP 4: Backend (.NET) publish ---- #
Write-Host ""
Write-Host "[..] Publishing backend (dotnet publish)..." -ForegroundColor Gray
Write-Host ""
# Fresh output each time so removed/renamed files from a previous
# version don't linger in the published folder
if (Test-Path $destinationPath) {
Remove-Item -Path $destinationPath -Recurse -Force
}
New-Item -ItemType Directory -Path $destinationPath -Force | Out-Null
dotnet publish $backendProject -c Release -r win-x64 --self-contained false -o $destinationPath
$dotnetExitCode = $LASTEXITCODE
if ($dotnetExitCode -ne 0) {
Write-Host ""
Write-Host "============================================" -ForegroundColor Red
Write-Host " [ERROR] Backend publish FAILED!" -ForegroundColor Red
Write-Host " Fix the errors above and try again." -ForegroundColor Red
Write-Host "============================================" -ForegroundColor Red
exit 1
}
Write-Host ""
Write-Host "[OK] Backend published -> $destinationPath" -ForegroundColor Green
# ---- STEP 5: Copy Angular dist files into published wwwroot ---- #
if (-not (Test-Path $wwwrootPath)) {
New-Item -ItemType Directory -Path $wwwrootPath -Force | Out-Null
Write-Host "[OK] Created folder: $wwwrootPath" -ForegroundColor Green
}
Copy-Item -Path $distPath -Destination $wwwrootPath -Recurse -Force
Write-Host "[OK] Angular files copied to: $wwwrootPath" -ForegroundColor Green
# ---- STEP 6: Write Angular web.config to wwwroot ---- #
# This is separate from the backend web.config that dotnet publish
# already generated at the root of $destinationPath (for the IIS
# ASP.NET Core Module). This one handles: SPA routing, cache
# headers for index.html, version.json, JS/CSS inside wwwroot.
$angularWebConfig = @"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<!-- Angular SPA routing — rewrites all unknown URLs to index.html -->
<rewrite>
<rules>
<rule name="Angular SPA" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/api" negate="true" />
<!-- Hangfire dashboard (calendar reminders) is served by ASP.NET, not Angular -->
<add input="{REQUEST_URI}" pattern="/hangfire" negate="true" />
</conditions>
<action type="Rewrite" url="$($baseHref)index.html" />
</rule>
</rules>
</rewrite>
<!-- JS/CSS/fonts: 1 year cache (filename hash handles busting) -->
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
</system.webServer>
<!-- index.html: NEVER cache -->
<location path="index.html">
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache, no-store, must-revalidate" />
<add name="Pragma" value="no-cache" />
<add name="Expires" value="-1" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
<!-- version.json: NEVER cache -->
<location path="version.json">
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache, no-store, must-revalidate" />
<add name="Pragma" value="no-cache" />
<add name="Expires" value="-1" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
</configuration>
"@
$angularWebConfig | Out-File -FilePath "$wwwrootPath\web.config" -Encoding utf8
Write-Host "[OK] Angular web.config written to: $wwwrootPath\web.config" -ForegroundColor Green
# ---- DONE ---- #
Write-Host ""
Write-Host "============================================" -ForegroundColor Cyan
Write-Host " Build & Publish COMPLETE!" -ForegroundColor Green
Write-Host " Version : $version" -ForegroundColor Yellow
Write-Host " Folder : $destinationPath" -ForegroundColor Yellow
Write-Host ""
Write-Host " Next steps:" -ForegroundColor White
Write-Host " 1. Zip the folder above" -ForegroundColor White
Write-Host " 2. Deploy the zip to the server PC" -ForegroundColor White
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""