act-runner-windows-cursed/gitea-runner.ps1
2025-10-19 16:52:53 +03:00

80 lines
2.4 KiB
PowerShell

# Ensure /data equivalent exists
$DataDir = "C:\data"
if (-not (Test-Path $DataDir)) {
New-Item -ItemType Directory -Path $DataDir | Out-Null
}
Set-Location $DataDir
# State file
$RUNNER_STATE_FILE = $env:RUNNER_STATE_FILE
if (-not $RUNNER_STATE_FILE) { $RUNNER_STATE_FILE = ".runner" }
# Config argument
$CONFIG_ARG = ""
if ($env:CONFIG_FILE) {
$CONFIG_ARG = "--config $($env:CONFIG_FILE)"
}
# Extra arguments
$EXTRA_ARGS = ""
if ($env:GITEA_RUNNER_LABELS) {
$EXTRA_ARGS += " --labels $($env:GITEA_RUNNER_LABELS)"
}
if ($env:GITEA_RUNNER_EPHEMERAL) {
$EXTRA_ARGS += " --ephemeral"
}
# Run arguments
$RUN_ARGS = ""
if ($env:GITEA_RUNNER_ONCE) {
$RUN_ARGS = "--once"
}
# Read token from file if not set
if (-not $env:GITEA_RUNNER_REGISTRATION_TOKEN -and (Test-Path $env:GITEA_RUNNER_REGISTRATION_TOKEN_FILE)) {
$env:GITEA_RUNNER_REGISTRATION_TOKEN = Get-Content $env:GITEA_RUNNER_REGISTRATION_TOKEN_FILE -Raw
}
# Check state file
if (-not (Test-Path $RUNNER_STATE_FILE -PathType Leaf)) {
Write-Host "$RUNNER_STATE_FILE is missing or not a regular file"
}
# Initialize variables for registration loop
$try = 0
$success = $false
if (-not (Get-Item $RUNNER_STATE_FILE).Length) {
$maxAttempts = if ($env:GITEA_MAX_REG_ATTEMPTS) { [int]$env:GITEA_MAX_REG_ATTEMPTS } else { 10 }
while (-not $success -and $try -lt $maxAttempts) {
$try++
Write-Host "Attempt #$try to register runner..."
# Build the full command as a string
$regCommand = "C:\act_runner.exe register --instance `"$env:GITEA_INSTANCE_URL`" --token `"$env:GITEA_RUNNER_REGISTRATION_TOKEN`" --name `"$($env:GITEA_RUNNER_NAME -or $env:COMPUTERNAME)`" $CONFIG_ARG $EXTRA_ARGS --no-interactive"
# Execute the command
$regOutput = Invoke-Expression "$regCommand 2>&1"
# Log output
$regOutput | Tee-Object -FilePath "C:\temp\reg.log"
if ($regOutput -match "Runner registered successfully") {
Write-Host "SUCCESS"
$success = $true
} else {
Write-Host "Waiting to retry ..."
Start-Sleep -Seconds 5
}
}
}
# Unset sensitive env vars
Remove-Item Env:\GITEA_RUNNER_REGISTRATION_TOKEN -ErrorAction SilentlyContinue
Remove-Item Env:\GITEA_RUNNER_REGISTRATION_TOKEN_FILE -ErrorAction SilentlyContinue
# Start daemon
$daemonCommand = "C:\act_runner.exe daemon $CONFIG_ARG $RUN_ARGS"
Invoke-Expression "$daemonCommand"