CI installs via local Verdaccio and prefers Gitea copies of node/postgres/nginx so staging and prod can still ship when Docker Hub and npmjs are unreachable.
173 lines
5.3 KiB
PowerShell
173 lines
5.3 KiB
PowerShell
# Copy Hub (or Arvan) base images into the Gitea registry so CI/compose can run
|
|
# when Docker Hub is unreachable. ASCII only: Windows PowerShell 5.1 + act_runner.
|
|
#
|
|
# Run from repo root after docker login to REGISTRY_HOST:
|
|
# .\infrastructure\scripts\seed-gitea-base-images.ps1 -RegistryPrefix "host.docker.internal:3000/admin"
|
|
#
|
|
# Also invoked by Gitea workflows after docker login.
|
|
param(
|
|
[Parameter(Mandatory = $true)][string]$RegistryPrefix,
|
|
[AllowEmptyString()]
|
|
[string]$ExtraSources = ''
|
|
)
|
|
|
|
$ErrorActionPreference = 'Continue'
|
|
$prefix = $RegistryPrefix.Trim().TrimEnd('/')
|
|
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
$repoRoot = (Resolve-Path (Join-Path $scriptDir '..\..')).Path
|
|
$nodeDockerfile = Join-Path $repoRoot 'infrastructure\docker\dyolink-node.Dockerfile'
|
|
|
|
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 5 }
|
|
}
|
|
return $false
|
|
}
|
|
|
|
function Publish-ToGitea([string]$Src, [string]$Dest) {
|
|
if ($Src -ne $Dest) {
|
|
Write-Host "Tagging $Src as $Dest"
|
|
docker tag $Src $Dest
|
|
if ($LASTEXITCODE -ne 0) { return $false }
|
|
}
|
|
Write-Host "docker push $Dest"
|
|
docker push $Dest
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "Could not push $Dest - local tag remains for this machine."
|
|
return $false
|
|
}
|
|
return $true
|
|
}
|
|
|
|
function Ensure-GiteaImage([string]$Dest, [string[]]$Sources) {
|
|
if (Test-Image $Dest) {
|
|
Write-Host "Already local: $Dest"
|
|
docker push $Dest 2>&1 | Out-Null
|
|
return $true
|
|
}
|
|
if (Invoke-Pull $Dest 1) {
|
|
Write-Host "Pulled from Gitea: $Dest"
|
|
return $true
|
|
}
|
|
foreach ($src in $Sources) {
|
|
if ([string]::IsNullOrWhiteSpace($src)) { continue }
|
|
if (Test-Image $src) {
|
|
Write-Host "Found local $src"
|
|
if (Publish-ToGitea $src $Dest) { return $true }
|
|
}
|
|
}
|
|
foreach ($src in $Sources) {
|
|
if ([string]::IsNullOrWhiteSpace($src)) { continue }
|
|
if (Invoke-Pull $src 2) {
|
|
if (Publish-ToGitea $src $Dest) { return $true }
|
|
}
|
|
}
|
|
Write-Host "FAILED to seed $Dest"
|
|
return $false
|
|
}
|
|
|
|
Write-Host "Seeding Gitea base images at $prefix"
|
|
|
|
$extraList = New-Object System.Collections.ArrayList
|
|
if (-not [string]::IsNullOrWhiteSpace($ExtraSources)) {
|
|
foreach ($part in ($ExtraSources -split ',')) {
|
|
$s = $part.Trim()
|
|
if ($s.Length -gt 0) { [void]$extraList.Add($s) }
|
|
}
|
|
}
|
|
|
|
$ok = $true
|
|
|
|
$nodeDest = "$prefix/node:20-bookworm-slim"
|
|
$nodeSources = New-Object System.Collections.ArrayList
|
|
foreach ($s in $extraList) { [void]$nodeSources.Add($s) }
|
|
foreach ($s in @(
|
|
'docker.arvancloud.ir/library/node:20-bookworm-slim',
|
|
'public.ecr.aws/docker/library/node:20-bookworm-slim',
|
|
'mirror.gcr.io/library/node:20-bookworm-slim',
|
|
'node:20-bookworm-slim'
|
|
)) {
|
|
if (-not $nodeSources.Contains($s)) { [void]$nodeSources.Add($s) }
|
|
}
|
|
if (-not (Ensure-GiteaImage $nodeDest @($nodeSources.ToArray()))) { $ok = $false }
|
|
|
|
$pgDest = "$prefix/postgres:15-alpine"
|
|
if (-not (Ensure-GiteaImage $pgDest @(
|
|
'docker.arvancloud.ir/library/postgres:15-alpine',
|
|
'postgres:15-alpine'
|
|
))) { $ok = $false }
|
|
|
|
$ngxDest = "$prefix/nginx:alpine"
|
|
if (-not (Ensure-GiteaImage $ngxDest @(
|
|
'docker.arvancloud.ir/library/nginx:alpine',
|
|
'nginx:alpine'
|
|
))) { $ok = $false }
|
|
|
|
$cbDest = "$prefix/certbot:latest"
|
|
if (-not (Ensure-GiteaImage $cbDest @(
|
|
'docker.arvancloud.ir/certbot/certbot:latest',
|
|
'certbot/certbot:latest'
|
|
))) {
|
|
Write-Host "WARN: certbot not seeded (prod Linux only). Re-run while online."
|
|
}
|
|
|
|
$verDest = "$prefix/verdaccio:5"
|
|
if (-not (Ensure-GiteaImage $verDest @(
|
|
'docker.arvancloud.ir/verdaccio/verdaccio:5',
|
|
'verdaccio/verdaccio:5'
|
|
))) {
|
|
Write-Host "WARN: verdaccio image not in Gitea. Keep the running container; do not prune."
|
|
}
|
|
|
|
$dyoDest = "$prefix/dyolink-node:20-bookworm-slim"
|
|
if (Test-Image $dyoDest) {
|
|
Write-Host "Already local: $dyoDest"
|
|
docker push $dyoDest 2>&1 | Out-Null
|
|
} elseif (Invoke-Pull $dyoDest 1) {
|
|
Write-Host "Pulled from Gitea: $dyoDest"
|
|
} else {
|
|
if (-not (Test-Path $nodeDockerfile)) {
|
|
Write-Host "Missing $nodeDockerfile"
|
|
$ok = $false
|
|
} elseif (-not (Test-Image $nodeDest)) {
|
|
Write-Host "Cannot build dyolink-node without $nodeDest"
|
|
$ok = $false
|
|
} else {
|
|
Write-Host "Building $dyoDest from $nodeDest (installs dumb-init; needs apt once)"
|
|
$ctx = Join-Path $repoRoot 'infrastructure\docker'
|
|
docker build --build-arg "NODE_IMAGE=$nodeDest" -t $dyoDest -f $nodeDockerfile $ctx
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "FAILED to build $dyoDest"
|
|
$ok = $false
|
|
} else {
|
|
docker push $dyoDest
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "Could not push $dyoDest - local tag remains."
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (-not $ok) {
|
|
Write-Host "One or more base images could not be seeded. App builds may still work if Gitea already has dyolink-node."
|
|
Write-Host "While online, re-run this script. Do not docker system prune -a on the runner."
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "Gitea base images ready:"
|
|
Write-Host " $dyoDest"
|
|
Write-Host " $nodeDest"
|
|
Write-Host " $pgDest"
|
|
Write-Host " $ngxDest"
|
|
Write-Host " $cbDest"
|
|
Write-Host " $verDest"
|
|
exit 0
|