96 lines
2.7 KiB
PowerShell
96 lines
2.7 KiB
PowerShell
param(
|
|
[string]$JavaPath = "$env:USERPROFILE\jenkins-local\jdk-21\jdk-21.0.11+10\bin\java.exe",
|
|
[string]$JenkinsWar = "$env:USERPROFILE\jenkins-local\jenkins.war",
|
|
[string]$JenkinsHome = "$env:USERPROFILE\.jenkins",
|
|
[int]$HttpPort = 8080,
|
|
[int]$AgentPort = 50000,
|
|
[int]$RestartDelaySeconds = 10
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
function Write-Log {
|
|
param([string]$Message)
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
Write-Output "[$timestamp] $Message"
|
|
}
|
|
|
|
function Get-ListeningProcessId {
|
|
param([int]$Port)
|
|
$line = netstat -ano | Select-String -Pattern "LISTENING\s+(\d+)$" | Where-Object {
|
|
$_.Line -match "[:.]$Port\s+"
|
|
} | Select-Object -First 1
|
|
|
|
if (-not $line) {
|
|
return $null
|
|
}
|
|
|
|
if ($line.Line -match "LISTENING\s+(\d+)$") {
|
|
return [int]$Matches[1]
|
|
}
|
|
|
|
return $null
|
|
}
|
|
|
|
function Test-JenkinsProcess {
|
|
param([int]$ProcessId)
|
|
if (-not $ProcessId) {
|
|
return $false
|
|
}
|
|
|
|
$process = Get-CimInstance Win32_Process -Filter "ProcessId = $ProcessId" -ErrorAction SilentlyContinue
|
|
if (-not $process) {
|
|
return $false
|
|
}
|
|
|
|
return ($process.CommandLine -like "*jenkins.war*")
|
|
}
|
|
|
|
if (-not (Test-Path -LiteralPath $JavaPath)) {
|
|
throw "Java path not found: $JavaPath"
|
|
}
|
|
|
|
if (-not (Test-Path -LiteralPath $JenkinsWar)) {
|
|
throw "Jenkins war not found: $JenkinsWar"
|
|
}
|
|
|
|
New-Item -ItemType Directory -Force -Path $JenkinsHome | Out-Null
|
|
|
|
while ($true) {
|
|
$listeningPid = Get-ListeningProcessId -Port $HttpPort
|
|
|
|
if ($listeningPid -and (Test-JenkinsProcess -ProcessId $listeningPid)) {
|
|
Write-Log "Jenkins is already listening on port $HttpPort with pid $listeningPid; monitoring it."
|
|
try {
|
|
Wait-Process -Id $listeningPid
|
|
} catch {
|
|
Write-Log "Existing Jenkins process wait failed: $($_.Exception.Message)"
|
|
}
|
|
} elseif ($listeningPid) {
|
|
Write-Log "Port $HttpPort is occupied by pid $listeningPid, but it is not Jenkins. Retrying in ${RestartDelaySeconds}s."
|
|
Start-Sleep -Seconds $RestartDelaySeconds
|
|
continue
|
|
} else {
|
|
$arguments = @(
|
|
"-Djenkins.install.runSetupWizard=false",
|
|
"-jar", $JenkinsWar,
|
|
"--httpPort=$HttpPort",
|
|
"--agentPort=$AgentPort"
|
|
)
|
|
|
|
Write-Log "Starting local Jenkins controller on port $HttpPort."
|
|
$previousJenkinsHome = $env:JENKINS_HOME
|
|
$env:JENKINS_HOME = $JenkinsHome
|
|
$process = Start-Process -FilePath $JavaPath -ArgumentList $arguments -WorkingDirectory (Split-Path -Parent $JenkinsWar) -NoNewWindow -PassThru
|
|
$env:JENKINS_HOME = $previousJenkinsHome
|
|
try {
|
|
Wait-Process -Id $process.Id
|
|
} catch {
|
|
Write-Log "Started Jenkins process wait failed: $($_.Exception.Message)"
|
|
}
|
|
}
|
|
|
|
Write-Log "Jenkins controller stopped; retrying in ${RestartDelaySeconds}s."
|
|
Start-Sleep -Seconds $RestartDelaySeconds
|
|
}
|