72 lines
2.1 KiB
PowerShell
72 lines
2.1 KiB
PowerShell
# Prefer a Gitea-hosted node:20-alpine so docker build does not HEAD registry-1.docker.io.
|
|
# Order: local Gitea tag -> pull Gitea -> local Docker Hub tag -> pull Hub (retries) -> tag/push Gitea.
|
|
# ASCII only: Windows PowerShell 5.1 + act_runner mis-parses backtick escapes in this file.
|
|
param(
|
|
[Parameter(Mandatory = $true)][string]$RegistryPrefix,
|
|
[string]$OutFile = '.ci-node-image',
|
|
[string]$HubImage = 'node:20-alpine'
|
|
)
|
|
|
|
$ErrorActionPreference = 'Continue'
|
|
$mirror = "$RegistryPrefix/node:20-alpine"
|
|
$nl = [char]10
|
|
|
|
function Test-Image([string]$Name) {
|
|
docker image inspect $Name 2>&1 | Out-Null
|
|
return ($LASTEXITCODE -eq 0)
|
|
}
|
|
|
|
function Invoke-Pull([string]$Name, [int]$Attempts) {
|
|
for ($i = 1; $i -le $Attempts; $i++) {
|
|
Write-Host "docker pull $Name (attempt $i/$Attempts)"
|
|
docker pull $Name
|
|
if ($LASTEXITCODE -eq 0) { return $true }
|
|
if ($i -lt $Attempts) { Start-Sleep -Seconds (10 * $i) }
|
|
}
|
|
return $false
|
|
}
|
|
|
|
function Save-Choice([string]$Name) {
|
|
$utf8 = New-Object System.Text.UTF8Encoding $false
|
|
$path = Join-Path (Get-Location) $OutFile
|
|
[System.IO.File]::WriteAllText($path, ($Name + $nl), $utf8)
|
|
Write-Host "NODE_IMAGE=$Name"
|
|
}
|
|
|
|
if (Test-Image $mirror) {
|
|
Write-Host "Using local $mirror"
|
|
Save-Choice $mirror
|
|
exit 0
|
|
}
|
|
|
|
if (Invoke-Pull $mirror 2) {
|
|
Save-Choice $mirror
|
|
exit 0
|
|
}
|
|
|
|
if (-not (Test-Image $HubImage)) {
|
|
if (-not (Invoke-Pull $HubImage 5)) {
|
|
Write-Host "Cannot pull $HubImage from Docker Hub (TLS timeout or blocked)."
|
|
Write-Host "On the Windows runner, when Hub is reachable:"
|
|
Write-Host " docker pull $HubImage"
|
|
Write-Host " docker tag $HubImage $mirror"
|
|
Write-Host " docker push $mirror"
|
|
Write-Host "Then re-run this workflow."
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
Write-Host "Tagging $HubImage as $mirror"
|
|
docker tag $HubImage $mirror
|
|
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
|
|
docker push $mirror
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "Could not push $mirror - docker build will use the local tag (legacy builder)."
|
|
$flag = Join-Path (Get-Location) '.ci-use-legacy-builder'
|
|
New-Item -ItemType File -Path $flag -Force | Out-Null
|
|
}
|
|
|
|
Save-Choice $mirror
|
|
exit 0
|