$ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' $Log = 'C:\OEM\gitea-runner-install.log' Start-Transcript -Path $Log -Append function Invoke-Installer([string]$File, [string]$Arguments, [int[]]$SuccessCodes = @(0)) { $process = Start-Process -FilePath $File -ArgumentList $Arguments -Wait -PassThru if ($process.ExitCode -notin $SuccessCodes) { throw "$File failed with exit code $($process.ExitCode)" } } function Download([string]$Url, [string]$Path) { # Always log the exact request: a thrown Invoke-WebRequest otherwise hides # which vendor URL returned the HTTP error in the OEM transcript. Write-Host "Downloading [$Url] to [$Path]" try { Invoke-WebRequest -UseBasicParsing -Uri $Url -OutFile $Path -TimeoutSec 600 -MaximumRedirection 10 if (-not (Test-Path $Path) -or (Get-Item -LiteralPath $Path).Length -le 0) { throw 'The server returned no file data.' } } catch { $status = 'unknown' if ($_.Exception.Response) { try { $status = [string]$_.Exception.Response.StatusCode } catch { } } Remove-Item -LiteralPath $Path -Force -ErrorAction SilentlyContinue throw "Download failed from [$Url] (HTTP $status): $($_.Exception.Message)" } } try { New-Item -ItemType Directory -Force -Path 'C:\ci-tools', 'C:\ci-tools\downloads', 'C:\ci-tools\cargo', 'C:\ci-tools\rustup', 'C:\ci\gitea-runner' | Out-Null # Node is needed by JavaScript-based Gitea Actions. Pin to the current Node 24 LTS line. Write-Host 'Resolving the latest Node.js 24 LTS release from https://nodejs.org/dist/index.json.' $nodeRelease = Invoke-RestMethod -Uri 'https://nodejs.org/dist/index.json' | Where-Object { $_.version -match '^v24\.' -and $_.lts } | Select-Object -First 1 if (-not $nodeRelease) { throw 'Could not find a Node.js 24 LTS release.' } $nodeMsi = "C:\ci-tools\downloads\node-$($nodeRelease.version)-x64.msi" Download "https://nodejs.org/dist/$($nodeRelease.version)/node-$($nodeRelease.version)-x64.msi" $nodeMsi Invoke-Installer "$env:SystemRoot\System32\msiexec.exe" "/i `"$nodeMsi`" /qn /norestart" @(0, 3010) # The release asset's filename includes its version; GitHub does not # provide a generic Git-64-bit.exe alias. Resolve the asset from the API. Write-Host 'Resolving the latest Git for Windows x64 installer from GitHub Releases.' $gitRelease = Invoke-RestMethod -UseBasicParsing ` -Uri 'https://api.github.com/repos/git-for-windows/git/releases/latest' ` -Headers @{ 'User-Agent' = 'gitea-vm-runner-provisioner'; 'Accept' = 'application/vnd.github+json' } $gitAsset = $gitRelease.assets | Where-Object { $_.name -match '^Git-[0-9.]+-64-bit\.exe$' } | Select-Object -First 1 if (-not $gitAsset) { throw 'GitHub latest release did not contain a versioned x64 Git installer.' } $gitInstaller = "C:\ci-tools\downloads\$($gitAsset.name)" Download $gitAsset.browser_download_url $gitInstaller Invoke-Installer $gitInstaller '/VERYSILENT /NORESTART /SP- /DIR="C:\Program Files\Git"' @(0, 3010) # MSVC compatibility lane plus the MSYS2 UCRT64 GCC lane for C++26 reflection/contracts. $vsInstaller = 'C:\ci-tools\downloads\vs_BuildTools.exe' Download 'https://aka.ms/vs/17/release/vs_BuildTools.exe' $vsInstaller Invoke-Installer $vsInstaller '--quiet --wait --norestart --nocache --installPath C:\BuildTools --add Microsoft.VisualStudio.Workload.VCTools --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 --add Microsoft.VisualStudio.Component.Windows11SDK.22621' @(0, 3010) $msysInstaller = 'C:\ci-tools\downloads\msys2-x86_64-latest.exe' Download 'https://github.com/msys2/msys2-installer/releases/latest/download/msys2-x86_64-latest.exe' $msysInstaller Invoke-Installer $msysInstaller '--confirm-command --accept-messages --root C:\msys64 --quiet' @(0, 3010) $bash = 'C:\msys64\usr\bin\bash.exe' if (-not (Test-Path $bash)) { throw 'MSYS2 install did not create C:\msys64\usr\bin\bash.exe' } # The first self-update can close the MSYS2 process. Running it twice is intentional. & $bash -lc 'pacman -Syu --noconfirm' & $bash -lc 'pacman -Syu --noconfirm' if ($LASTEXITCODE -ne 0) { throw 'MSYS2 package database/system update failed.' } & $bash -lc 'pacman -S --needed --noconfirm mingw-w64-ucrt-x86_64-toolchain mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-ninja mingw-w64-ucrt-x86_64-vulkan-headers mingw-w64-ucrt-x86_64-vulkan-loader mingw-w64-ucrt-x86_64-vulkan-tools' if ($LASTEXITCODE -ne 0) { throw 'MSYS2 UCRT64 toolchain installation failed.' } # Install the official Vulkan SDK for MSVC builds and validation tools. $vulkanVersion = '1.4.363.0' $vulkanInstaller = "C:\ci-tools\downloads\VulkanSDK-$vulkanVersion-Installer.exe" Download "https://sdk.lunarg.com/sdk/download/$vulkanVersion/windows/VulkanSDK-$vulkanVersion-Installer.exe" $vulkanInstaller Invoke-Installer $vulkanInstaller "--accept-licenses --default-answer --confirm-command --root C:\VulkanSDK\$vulkanVersion --quiet" @(0, 3010) # Install Rust in a stable machine-wide location (the runner task uses LocalSystem). [Environment]::SetEnvironmentVariable('CARGO_HOME', 'C:\ci-tools\cargo', 'Machine') [Environment]::SetEnvironmentVariable('RUSTUP_HOME', 'C:\ci-tools\rustup', 'Machine') $env:CARGO_HOME = 'C:\ci-tools\cargo' $env:RUSTUP_HOME = 'C:\ci-tools\rustup' $rustup = 'C:\ci-tools\downloads\rustup-init.exe' Download 'https://win.rustup.rs/x86_64' $rustup Invoke-Installer $rustup '-y --default-toolchain nightly --no-modify-path' @(0) # Ensure the native toolchain paths are available to jobs launched by Task Scheduler. $machinePath = [Environment]::GetEnvironmentVariable('Path', 'Machine') $additions = @( 'C:\msys64\ucrt64\bin', 'C:\msys64\usr\bin', 'C:\Program Files\Git\cmd', 'C:\Program Files\nodejs', 'C:\ci-tools\cargo\bin', "C:\VulkanSDK\$vulkanVersion\Bin", 'C:\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin' ) foreach ($entry in $additions) { if ($machinePath -notlike "*$entry*") { $machinePath = "$entry;$machinePath" } } [Environment]::SetEnvironmentVariable('Path', $machinePath, 'Machine') # A registration is permitted only after the real C++26 feature probe compiles and runs. $gxx = 'C:\msys64\ucrt64\bin\g++.exe' $probe = 'C:\OEM\cxx26-feature-probe.cpp' $probeExe = 'C:\ci-tools\cxx26-feature-probe.exe' if (-not (Test-Path $gxx)) { throw 'GCC 16 UCRT64 compiler was not installed.' } & $gxx -std=c++26 -fcontracts -freflection -fcontract-evaluation-semantic=enforce $probe -o $probeExe if ($LASTEXITCODE -ne 0) { throw 'GCC C++26 contracts + reflection probe did not compile; runner intentionally left unregistered.' } & $probeExe if ($LASTEXITCODE -ne 0) { throw 'GCC C++26 contracts + reflection probe failed at runtime; runner intentionally left unregistered.' } # The existing repository secret is an agenix EnvironmentFile: TOKEN=. # dockur/windows copies this OEM directory to C:\OEM on first automatic installation. $registrationFile = 'C:\OEM\runner-token' if (Test-Path $registrationFile) { $tokenLine = Get-Content $registrationFile | Where-Object { $_ -match '^TOKEN=' } | Select-Object -First 1 if (-not $tokenLine) { throw 'C:\OEM\runner-token must contain TOKEN=.' } $token = $tokenLine.Substring(6).Trim().Trim('"').Trim("'") if (-not $token) { throw 'The Gitea registration token is empty.' } $tokenFile = 'C:\ci-tools\registration-token.tmp' Set-Content -NoNewline -Path $tokenFile -Value $token $acl = Get-Acl $tokenFile $acl.SetAccessRuleProtection($true, $false) $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('SYSTEM', 'FullControl', 'Allow') $acl.SetAccessRule($rule) Set-Acl -Path $tokenFile -AclObject $acl Push-Location 'C:\ci\gitea-runner' try { $runnerVersion = '5.0.0' $runnerExe = 'C:\ci\gitea-runner\gitea-runner.exe' Download "https://dl.gitea.com/gitea-runner/$runnerVersion/gitea-runner-$runnerVersion-windows-amd64.exe" $runnerExe & $runnerExe register --no-interactive --instance 'https://git.faumaray.com' --token-file $tokenFile --name 'ci-windows-x64' --labels 'windows-x64:host' if ($LASTEXITCODE -ne 0) { throw 'Gitea runner registration failed.' } } finally { Pop-Location Remove-Item -Force -ErrorAction SilentlyContinue $tokenFile Remove-Item -Force -ErrorAction SilentlyContinue $registrationFile } @' @echo off set "CARGO_HOME=C:\ci-tools\cargo" set "RUSTUP_HOME=C:\ci-tools\rustup" set "VULKAN_SDK=C:\VulkanSDK\1.4.363.0" set "PATH=C:\msys64\ucrt64\bin;C:\msys64\usr\bin;C:\Program Files\Git\cmd;C:\Program Files\nodejs;C:\ci-tools\cargo\bin;C:\VulkanSDK\1.4.363.0\Bin;C:\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;%PATH%" cd /d C:\ci\gitea-runner C:\ci\gitea-runner\gitea-runner.exe daemon >> C:\ci\gitea-runner\runner.log 2>&1 '@ | Set-Content -Encoding Ascii 'C:\ci\gitea-runner\run.cmd' & schtasks.exe /Create /TN 'Gitea CI Runner' /SC ONSTART /RU SYSTEM /RL HIGHEST /TR 'cmd.exe /d /c C:\ci\gitea-runner\run.cmd' /F if ($LASTEXITCODE -ne 0) { throw 'Could not create the Gitea runner startup task.' } & schtasks.exe /Run /TN 'Gitea CI Runner' } else { Write-Warning 'No runner token was mounted at C:\OEM\runner-token; toolchains are installed but this Windows guest is not registered.' } } catch { Write-Error $_ throw } finally { Stop-Transcript }