Files
vpn-dns-gateway/gitea/local-runner/runner.ps1
vsun1 ed0a862657
vpn-dns-gateway/pipeline/pr-main This commit looks good
Move CI runners to Droplet and enable application HTTPS
2026-11-15 12:49:06 -05:00

87 lines
4.9 KiB
PowerShell

[CmdletBinding()]
param(
[ValidateSet('Check', 'Register', 'Start', 'Logs', 'Stop')]
[string]$Action = 'Check',
[switch]$AllowLaptopFallback
)
$ErrorActionPreference = 'Stop'
if ($Action -in @('Register', 'Start') -and -not $AllowLaptopFallback) {
throw 'This runner now runs on The-Ocean. Stop the remote copy first, then pass -AllowLaptopFallback for an intentional laptop fallback. Never run both copies of the registration.'
}
$composeFile = Join-Path $PSScriptRoot 'compose.yaml'
$composeArgs = @('compose', '--project-name', 'ssw590-local-runner', '-f', $composeFile)
$runnerImage = 'docker.io/gitea/runner@sha256:66d80966792e621c9761c47919644198d35fd1c297e9a01e69ed3c1ae37db0c7'
$instanceUrl = 'https://git.yebilly.me/'
$runnerName = 'ssw590-vince-local'
function Assert-DockerSuccess([string]$Operation) {
if ($LASTEXITCODE -ne 0) { throw "$Operation failed (exit $LASTEXITCODE)." }
}
Get-Command docker -ErrorAction Stop | Out-Null
$engineType = & docker info --format '{{.OSType}}'
Assert-DockerSuccess 'Docker Desktop connection'
if ($engineType -ne 'linux') { throw 'Switch Docker Desktop to Linux containers first.' }
Write-Host "Docker engine: $engineType"
& docker @composeArgs config --quiet
Assert-DockerSuccess 'Compose configuration'
switch ($Action) {
'Check' {
& docker run --rm --entrypoint gitea-runner $runnerImage --version
Assert-DockerSuccess 'Runner version check'
& docker run --rm --entrypoint wget $runnerImage -q -T 20 -O /dev/null $instanceUrl
Assert-DockerSuccess 'HTTPS connection from runner image to Gitea'
Write-Host 'CHECK PASSED: Docker Linux engine, Compose configuration, runner version, and Gitea HTTPS.'
Write-Host "Name: $runnerName | Label: latex-local | Capacity: 1 | Mode: Docker"
Write-Host 'Scope: determined by the repository registration token you select.'
Write-Host 'SCREENSHOT: capture this summary and the version output; no token is involved.'
}
'Register' {
# Do not overwrite an existing runner identity or start a second daemon.
$registrationState = & docker @composeArgs run --rm -T --no-deps --entrypoint sh runner -c 'if test -s /data/.runner; then echo registered; else echo unregistered; fi'
Assert-DockerSuccess 'Runner registration storage check'
if ($registrationState -eq 'registered') { throw 'Already registered. Use -Action Start instead.' }
if ($registrationState -ne 'unregistered') { throw 'Unexpected runner registration status.' }
Write-Host 'Use the token from repository Settings > Actions > Runners.'
Write-Host 'Do not screenshot the token page or run a transcript during registration.'
$secureToken = Read-Host 'Paste registration token (input is hidden)' -AsSecureString
$tokenPointer = [IntPtr]::Zero
try {
$tokenPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureToken)
$tokenText = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($tokenPointer).Trim()
if ([string]::IsNullOrWhiteSpace($tokenText)) { throw 'Token is empty.' }
# stdin avoids saving the value in a file, command argument, or container environment.
$tokenText | & docker @composeArgs run --rm -T --no-deps --entrypoint sh runner /register-from-stdin.sh --config /config.yaml register --no-interactive --instance $instanceUrl --name $runnerName --token-file /dev/stdin
Assert-DockerSuccess 'Runner registration'
}
finally {
$tokenText = $null
if ($tokenPointer -ne [IntPtr]::Zero) {
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($tokenPointer)
}
if ($null -ne $secureToken) { $secureToken.Dispose() }
}
Write-Host 'Registered. Run this script with -Action Start, then refresh the Gitea Runners page.'
}
'Start' {
$registrationState = & docker @composeArgs run --rm -T --no-deps --entrypoint sh runner -c 'if test -s /data/.runner; then echo registered; else echo unregistered; fi'
Assert-DockerSuccess 'Runner registration storage check'
if ($registrationState -ne 'registered') { throw 'No existing registration. Run -Action Register first.' }
& docker @composeArgs up -d runner
Assert-DockerSuccess 'Runner start'
& docker @composeArgs ps
Assert-DockerSuccess 'Runner status'
Write-Host 'SCREENSHOT: refresh Gitea Settings > Actions > Runners; capture the online runner and latex-local label, with tokens hidden.'
Write-Host 'Use -Action Logs to watch its output in PowerShell.'
}
'Logs' {
Write-Host 'Ctrl+C stops watching logs; it leaves the runner running.'
& docker @composeArgs logs --follow --tail 100 runner
}
'Stop' {
& docker @composeArgs stop runner
Assert-DockerSuccess 'Runner stop'
Write-Host 'Runner stopped; registration volume retained.'
}
}