feat: stream build output in real-time + log capture and retention
Invoke-RemoteBuild.ps1: - Custom build command: replace buffered collect-then-print with Write-Host streaming inside Invoke-Command (WinRM forwards Write-Host in real-time to the host console) - Set PYTHONUNBUFFERED=1 in the remote session to prevent Python from holding lines in its internal buffer - Same streaming approach for dotnet restore and dotnet build paths - Return value is now just int exit code instead of hashtable Invoke-CIJob.ps1: - Add -LogDir parameter (default F:\CI\Logs) - Add -LogRetentionDays parameter (default 30) - Start-Transcript at job start -> F:\CI\Logs\<JobId>\invoke-ci.log - Stop-Transcript in finally block (guaranteed execution) - Auto-purge log directories older than LogRetentionDays after each job
This commit is contained in:
@@ -108,7 +108,15 @@ param(
|
||||
|
||||
[string] $GuestCredentialTarget = 'BuildVMGuest',
|
||||
|
||||
[string] $VmrunPath = 'C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe'
|
||||
[string] $VmrunPath = 'C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe',
|
||||
|
||||
# Directory for job execution logs. Each job gets its own sub-directory:
|
||||
# $LogDir\$JobId\invoke-ci.log
|
||||
[string] $LogDir = 'F:\CI\Logs',
|
||||
|
||||
# Days to retain job logs. Directories older than this are purged at job end.
|
||||
# Set to 0 to disable retention/purge.
|
||||
[int] $LogRetentionDays = 30
|
||||
)
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
@@ -152,6 +160,19 @@ $hostCloneDir = Join-Path $env:TEMP "ci-src-$JobId"
|
||||
$cloneVmxPath = $null
|
||||
$startTime = Get-Date
|
||||
|
||||
# ── Start transcript ──────────────────────────────────────────────────────────
|
||||
$transcriptStarted = $false
|
||||
try {
|
||||
$jobLogDir = Join-Path $LogDir $JobId
|
||||
if (-not (Test-Path $jobLogDir)) { New-Item -ItemType Directory -Path $jobLogDir -Force | Out-Null }
|
||||
$transcriptPath = Join-Path $jobLogDir 'invoke-ci.log'
|
||||
Start-Transcript -Path $transcriptPath -Force -ErrorAction Stop
|
||||
$transcriptStarted = $true
|
||||
}
|
||||
catch {
|
||||
Write-Warning "[Invoke-CIJob] Could not start transcript: $_"
|
||||
}
|
||||
|
||||
Write-Host "============================================================"
|
||||
Write-Host "[Invoke-CIJob] Job : $JobId"
|
||||
Write-Host "[Invoke-CIJob] Repository : $RepoUrl"
|
||||
@@ -276,6 +297,22 @@ finally {
|
||||
Write-Host "[Cleanup] Removing host source clone ($hostCloneDir)..."
|
||||
Remove-Item $hostCloneDir -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
# ── Stop transcript ───────────────────────────────────────────────────
|
||||
if ($transcriptStarted) {
|
||||
try { Stop-Transcript -ErrorAction SilentlyContinue } catch {}
|
||||
}
|
||||
|
||||
# ── Log retention: purge directories older than $LogRetentionDays days ─
|
||||
if ($LogRetentionDays -gt 0 -and (Test-Path $LogDir)) {
|
||||
$cutoff = (Get-Date).AddDays(-$LogRetentionDays)
|
||||
Get-ChildItem -Path $LogDir -Directory -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.LastWriteTime -lt $cutoff } |
|
||||
ForEach-Object {
|
||||
Remove-Item $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
|
||||
Write-Host "[Cleanup] Purged old log dir: $($_.FullName)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exit $exitCode
|
||||
|
||||
Reference in New Issue
Block a user