Files
jobtrackingapp/scripts/rebuild-windows-docker.ps1
T

84 lines
3.8 KiB
PowerShell

[CmdletBinding()]
param(
[ValidateRange(1024, 65535)][int]$Port = 3000,
[ValidateSet('desktop-linux', 'default')][string]$DockerContext = 'desktop-linux',
[switch]$NoCache,
[switch]$ValidateOnly
)
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent $PSScriptRoot
$composeFile = Join-Path $repo 'dev/windows-local.compose.yml'
$envFile = Join-Path $repo '.env.windows-local'
$project = 'jobjakt-windows-local'
function Invoke-DockerChecked {
param([string[]]$DockerArguments)
& docker --context $DockerContext @DockerArguments
if ($LASTEXITCODE -ne 0) { throw "Docker failed (exit $LASTEXITCODE). Existing data volumes have been retained." }
}
function New-LocalSecret {
$bytes = New-Object byte[] 32
$random = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try { $random.GetBytes($bytes) } finally { $random.Dispose() }
return ([BitConverter]::ToString($bytes)).Replace('-', '').ToLowerInvariant()
}
Get-Command docker -ErrorAction Stop | Out-Null
$sourceCommit = & git -C $repo rev-parse HEAD
if ($LASTEXITCODE -ne 0) { throw 'Run this script from its existing Git checkout.' }
$sourceBranch = & git -C $repo branch --show-current
Write-Host "Building checkout $sourceBranch at $sourceCommit"
Write-Host 'No fetch, reset, production Compose, image push or volume deletion is performed.'
if (-not (Test-Path -LiteralPath $envFile)) {
# Reuse on later runs: replacing these passwords would break existing volumes.
$lines = @(
"JT_LOCAL_DB_ROOT_PASSWORD=$(New-LocalSecret)",
"JT_LOCAL_DB_PASSWORD=$(New-LocalSecret)",
"JT_LOCAL_JWT_KEY=$(New-LocalSecret)",
'JT_LOCAL_ADMIN_EMAIL=local-admin@example.test',
"JT_LOCAL_ADMIN_PASSWORD=Jt1!$(New-LocalSecret)"
)
[IO.File]::WriteAllLines($envFile, $lines, (New-Object Text.UTF8Encoding($false)))
Write-Host "Created private local credentials: $envFile"
}
$previousPort = $env:JT_LOCAL_PORT
$previousCommit = $env:JT_LOCAL_COMMIT
try {
$env:JT_LOCAL_PORT = [string]$Port
$env:JT_LOCAL_COMMIT = $sourceCommit
$composeArgs = @('compose', '--project-name', $project,
'--env-file', $envFile, '-f', $composeFile)
Invoke-DockerChecked -DockerArguments ($composeArgs + @('config', '--quiet'))
if ($ValidateOnly) {
Write-Host 'Compose configuration is valid; no images built or containers started.'
return
}
$contextJson = & docker context inspect $DockerContext
if ($LASTEXITCODE -ne 0) { throw "Docker context '$DockerContext' was not found. Start Docker Desktop in Linux containers mode." }
$endpoint = ($contextJson | ConvertFrom-Json)[0].Endpoints.docker.Host
if ($endpoint -notlike 'npipe://*') { throw 'This script only starts a local Windows Docker named-pipe engine.' }
$engine = & docker --context $DockerContext info --format '{{.OSType}}' 2>$null
if ($LASTEXITCODE -ne 0 -or $engine -ne 'linux') {
throw 'Start Docker Desktop, select Linux containers, wait until the engine is ready, then rerun this script.'
}
$buildArgs = @('build', 'backend', 'frontend')
if ($NoCache) { $buildArgs = @('build', '--no-cache', 'backend', 'frontend') }
Invoke-DockerChecked -DockerArguments ($composeArgs + $buildArgs)
Invoke-DockerChecked -DockerArguments ($composeArgs + @('up', '-d', '--wait', '--wait-timeout', '300', 'database', 'backend', 'frontend'))
Invoke-DockerChecked -DockerArguments ($composeArgs + @('ps'))
Write-Host "Open http://localhost:$Port"
Write-Host 'Login: local-admin@example.test (password is JT_LOCAL_ADMIN_PASSWORD in the private credentials file).'
Write-Host "Credentials: $envFile"
Write-Host 'Re-running this script rebuilds the current checkout and retains the local database/files.'
}
finally {
$env:JT_LOCAL_PORT = $previousPort
$env:JT_LOCAL_COMMIT = $previousCommit
}