more
This commit is contained in:
+163
-100
@@ -4,13 +4,11 @@ param(
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
Write-Host "===================================="
|
||||
Write-Host " Unity CI Build (Windows Agent)"
|
||||
Write-Host " Target: $Target"
|
||||
Write-Host "===================================="
|
||||
Write-Host "Unity CI Build - Target: $Target"
|
||||
|
||||
try {
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Load toolchain config
|
||||
# -----------------------------
|
||||
@@ -19,7 +17,7 @@ try {
|
||||
$unityVersion = $config.unityVersion
|
||||
$unityRoot = $config.windows.unityRoot
|
||||
|
||||
$hubExe = "${env:ProgramFiles}\Unity Hub\Unity Hub.exe"
|
||||
$hubExe = Join-Path $env:ProgramFiles "Unity Hub\Unity Hub.exe"
|
||||
|
||||
if (!(Test-Path $hubExe)) {
|
||||
throw "Unity Hub not found at: $hubExe"
|
||||
@@ -32,133 +30,198 @@ try {
|
||||
|
||||
if (!(Test-Path $unityEditorPath)) {
|
||||
|
||||
Write-Host "Unity $unityVersion not found. Installing via Unity Hub..."
|
||||
Write-Host "Installing Unity $unityVersion..."
|
||||
|
||||
# Install Unity Editor
|
||||
& $hubExe -- --headless install `
|
||||
--version $unityVersion
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "Failed to install Unity $unityVersion"
|
||||
}
|
||||
|
||||
if (!(Test-Path $unityEditorPath)) {
|
||||
throw "Unity installation completed but editor not found: $unityEditorPath"
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Unity found: $unityEditorPath"
|
||||
Write-Host "Unity: $unityVersion"
|
||||
|
||||
# -----------------------------
|
||||
# Ensure required modules
|
||||
# -----------------------------
|
||||
# NOTE:
|
||||
# Unity Hub CLI installs modules per version.
|
||||
# These are cached per editor version, not reinstalled each build.
|
||||
|
||||
$modules = $config.windows.modules
|
||||
|
||||
Write-Host "Ensuring Unity modules: $($modules -join ', ')"
|
||||
if ($modules.Count -gt 0) {
|
||||
|
||||
foreach ($module in $modules) {
|
||||
Write-Host "Checking Unity modules..."
|
||||
|
||||
Write-Host "Checking module: $module"
|
||||
# Query installed editors/modules once
|
||||
$installedEditors = & $hubExe -- --headless editors --installed
|
||||
|
||||
# Check installed modules via Unity Hub
|
||||
$installed = & $hubExe -- --headless editors --installed
|
||||
foreach ($module in $modules) {
|
||||
|
||||
if ($installed -notmatch "$unityVersion.*$module") {
|
||||
if ($installedEditors -notmatch "$unityVersion.*$module") {
|
||||
|
||||
Write-Host "Installing module: $module"
|
||||
Write-Host "Installing module: $module"
|
||||
|
||||
& $hubExe -- --headless install-modules `
|
||||
--version $unityVersion `
|
||||
--module $module
|
||||
& $hubExe -- --headless install-modules `
|
||||
--version $unityVersion `
|
||||
--module $module
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "Failed to install module: $module"
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "Failed to install module: $module"
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Host "Module already installed: $module"
|
||||
}
|
||||
}
|
||||
|
||||
# -----------------------------
|
||||
# Resolve Unity build method
|
||||
# -----------------------------
|
||||
if (!$Target) {
|
||||
Write-Host "No Target Provided"
|
||||
exit
|
||||
if ([string]::IsNullOrWhiteSpace($Target)) {
|
||||
throw "No Target provided."
|
||||
}
|
||||
else {
|
||||
$method = switch ($Target) {
|
||||
"Windows" { "BuildScript.Windows" }
|
||||
"Android" { "BuildScript.Android" }
|
||||
}
|
||||
|
||||
Write-Host "Using Unity method: $method"
|
||||
|
||||
Write-Host "===================================================================="
|
||||
Write-Host "===================== Starting Unity Build ========================="
|
||||
Write-Host "===================================================================="
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
# 1. Define paths
|
||||
$logFile = "$PWD/unity_build.log"
|
||||
$artifactLog = "$PWD/Builds/unity_build.log"
|
||||
|
||||
New-Item -ItemType Directory -Force -Path "$PWD/BuildLog" | Out-Null
|
||||
|
||||
# 2. Build arguments
|
||||
$unityArgs = @(
|
||||
"-batchmode",
|
||||
"-nographics",
|
||||
"-quit",
|
||||
"-projectPath", "$PWD",
|
||||
"-executeMethod", "$method",
|
||||
"-logFile", "$logFile",
|
||||
"-verbose"
|
||||
)
|
||||
|
||||
Write-Host "Starting Unity..."
|
||||
Write-Host "Log: $logFile"
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
$process.WaitForExit()
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
Write-Host "===================================="
|
||||
Write-Host " BUILD SUCCESS: $Target"
|
||||
Write-Host "===================================="
|
||||
$method = switch ($Target) {
|
||||
"Windows" { "BuildScript.Windows" }
|
||||
"Android" { "BuildScript.Android" }
|
||||
default { throw "Unsupported target: $Target" }
|
||||
}
|
||||
|
||||
Write-Host "Build Method: $method"
|
||||
|
||||
# -----------------------------
|
||||
# Paths
|
||||
# -----------------------------
|
||||
$logFile = Join-Path $PWD "unity_build.log"
|
||||
|
||||
$artifactDir = Join-Path $PWD "Builds"
|
||||
$artifactLog = Join-Path $artifactDir "unity_build.log"
|
||||
|
||||
New-Item -ItemType Directory -Force -Path $artifactDir | Out-Null
|
||||
|
||||
# Remove previous log
|
||||
if (Test-Path $logFile) {
|
||||
Remove-Item $logFile -Force
|
||||
}
|
||||
|
||||
# -----------------------------
|
||||
# Unity Arguments
|
||||
# -----------------------------
|
||||
$unityArgs = @(
|
||||
"-batchmode"
|
||||
"-nographics"
|
||||
"-quit"
|
||||
"-projectPath", "$PWD"
|
||||
"-executeMethod", "$method"
|
||||
"-logFile", "$logFile"
|
||||
)
|
||||
|
||||
Write-Host "Starting Unity build..."
|
||||
|
||||
# -----------------------------
|
||||
# Start Unity
|
||||
# -----------------------------
|
||||
$process = Start-Process `
|
||||
-FilePath $unityEditorPath `
|
||||
-ArgumentList $unityArgs `
|
||||
-NoNewWindow `
|
||||
-PassThru
|
||||
|
||||
$process.WaitForExit()
|
||||
|
||||
# -----------------------------
|
||||
# Wait for log file creation
|
||||
# -----------------------------
|
||||
$timeoutSeconds = 30
|
||||
$elapsed = 0
|
||||
|
||||
while (!(Test-Path $logFile) -and $elapsed -lt $timeoutSeconds) {
|
||||
Start-Sleep -Seconds 1
|
||||
$elapsed++
|
||||
}
|
||||
|
||||
if (!(Test-Path $logFile)) {
|
||||
throw "Unity log file was not created."
|
||||
}
|
||||
|
||||
# -----------------------------
|
||||
# Wait for file writes to finish
|
||||
# -----------------------------
|
||||
$previousLength = -1
|
||||
|
||||
do {
|
||||
$currentLength = (Get-Item $logFile).Length
|
||||
|
||||
if ($currentLength -eq $previousLength) {
|
||||
break
|
||||
}
|
||||
|
||||
$previousLength = $currentLength
|
||||
|
||||
Start-Sleep -Milliseconds 500
|
||||
|
||||
} while ($true)
|
||||
|
||||
# -----------------------------
|
||||
# Archive log
|
||||
# -----------------------------
|
||||
Copy-Item $logFile $artifactLog -Force
|
||||
|
||||
# -----------------------------
|
||||
# Build failed
|
||||
# -----------------------------
|
||||
if ($process.ExitCode -ne 0) {
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "===== Unity Build Failed ====="
|
||||
Write-Host "Exit Code: $($process.ExitCode)"
|
||||
Write-Host ""
|
||||
|
||||
if (Test-Path $logFile) {
|
||||
Write-Host "===== Last 500 Log Lines ====="
|
||||
Get-Content $logFile -Tail 500
|
||||
}
|
||||
|
||||
exit $process.ExitCode
|
||||
}
|
||||
|
||||
# -----------------------------
|
||||
# Build succeeded
|
||||
# -----------------------------
|
||||
Write-Host ""
|
||||
Write-Host "===== Build Summary ====="
|
||||
|
||||
if (Test-Path $logFile) {
|
||||
|
||||
Get-Content $logFile |
|
||||
Select-String `
|
||||
-Pattern @(
|
||||
"Build completed"
|
||||
"Build succeeded"
|
||||
"Build finished"
|
||||
"Total time"
|
||||
) `
|
||||
-SimpleMatch |
|
||||
ForEach-Object {
|
||||
Write-Host $_.Line
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "===== Last 50 Log Lines ====="
|
||||
|
||||
Get-Content $logFile -Tail 50
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "SUCCESS: $Target"
|
||||
|
||||
}
|
||||
catch {
|
||||
throw
|
||||
}
|
||||
finally {
|
||||
Write-Host "Cleaning Up"
|
||||
# Stop-Process -Name "Unity", "Unity Hub" -Force
|
||||
Write-Error $_.Exception.Message
|
||||
exit 1
|
||||
}
|
||||
finally {
|
||||
Write-Host "Cleanup complete."
|
||||
# Stop-Process -Name "Unity","Unity Hub" -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user