more
This commit is contained in:
+31
-21
@@ -99,8 +99,15 @@ try {
|
||||
Write-Host "===================== Starting Unity Build ========================="
|
||||
Write-Host "===================================================================="
|
||||
|
||||
# 1. Define arguments cleanly using an array
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
# 1. Define paths
|
||||
$logFile = "$PWD/unity_build.log"
|
||||
$artifactLog = "$PWD/BuildLog/unity_build.log"
|
||||
|
||||
New-Item -ItemType Directory -Force -Path "$PWD/BuildLog" | Out-Null
|
||||
|
||||
# 2. Build arguments
|
||||
$unityArgs = @(
|
||||
"-batchmode",
|
||||
"-nographics",
|
||||
@@ -110,30 +117,33 @@ try {
|
||||
"-logFile", "$logFile"
|
||||
)
|
||||
|
||||
# 2. Start the Unity process in the background
|
||||
$process = Start-Process -FilePath $unityEditorPath -ArgumentList $unityArgs -PassThru -NoNewWindow
|
||||
Write-Host "Starting Unity..."
|
||||
Write-Host "Log: $logFile"
|
||||
|
||||
# 3. Wait for the log file to be created by Unity
|
||||
while (!(Test-Path $logFile) -and !$process.HasExited) {
|
||||
Start-Sleep -Milliseconds 100
|
||||
# 3. Start Unity
|
||||
$process = Start-Process `
|
||||
-FilePath $unityEditorPath `
|
||||
-ArgumentList $unityArgs `
|
||||
-NoNewWindow `
|
||||
-PassThru
|
||||
|
||||
# 4. Stream log live (simple + reliable)
|
||||
while (-not $process.HasExited) {
|
||||
if (Test-Path $logFile) {
|
||||
Get-Content $logFile -Tail 20 -ErrorAction SilentlyContinue | ForEach-Object {
|
||||
Write-Host $_
|
||||
}
|
||||
}
|
||||
Start-Sleep -Milliseconds 500
|
||||
}
|
||||
|
||||
# 4. Stream the log live into the TeamCity console
|
||||
if (Test-Path $logFile) {
|
||||
# Get-Content -Wait streams the file until the process kills it
|
||||
$logJob = Start-Job -ScriptBlock { Get-Content $using:logFile -Wait }
|
||||
|
||||
# Wait for Unity to completely finish building
|
||||
$process.WaitForExit()
|
||||
|
||||
# Stop streaming and clean up
|
||||
Stop-Job $logJob
|
||||
Receive-Job $logJob
|
||||
Remove-Job $logJob
|
||||
Remove-Item $logFile -Force
|
||||
}
|
||||
$process.WaitForExit()
|
||||
|
||||
# 5. Bubble up the exit code so TeamCity catches errors
|
||||
# 5. Copy Log to Builds Folder
|
||||
Copy-Item $logFile $artifactLog -Force
|
||||
|
||||
|
||||
# 6. Exit handling
|
||||
if ($process.ExitCode -ne 0) {
|
||||
Write-Error "Unity build failed with exit code $($process.ExitCode)"
|
||||
exit $process.ExitCode
|
||||
|
||||
Reference in New Issue
Block a user