Skip to content

Commit e996972

Browse files
gfraiteurclaude
andcommitted
Push locally cached Docker images to registry if not already uploaded
When an image exists locally but not in the registry, push it asynchronously instead of skipping. Uses docker manifest inspect to check registry presence. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a2df3fb commit e996972

File tree

2 files changed

+92
-72
lines changed

2 files changed

+92
-72
lines changed

DockerBuild.ps1

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,49 +1413,62 @@ $envVarAssignments$gitConfigCommands$postInitCommands
14131413
# Registry authentication and pull logic
14141414
$builtNewImage = $false
14151415
$dockerConfigArg = @()
1416+
$imageExistsInRegistry = $false
14161417

14171418
if ($dockerRegistry -and -not $NoBuildImage -and -not $existingContainerId)
14181419
{
1419-
# Check if image already exists locally before pulling from registry
1420+
# Create a temporary Docker config directory to avoid credential helper issues
1421+
# (e.g., docker-credential-desktop not found when using Docker Engine without Desktop)
1422+
$tempDockerConfig = Join-Path $env:TEMP "docker-config-$( New-Guid )"
1423+
New-Item -ItemType Directory -Path $tempDockerConfig -Force | Out-Null
1424+
@{ auths = @{ } } | ConvertTo-Json | Set-Content (Join-Path $tempDockerConfig "config.json")
1425+
$dockerConfigArg = @("--config", $tempDockerConfig)
1426+
1427+
# Authenticate to registry
1428+
$dockerPassword = $env:DOCKER_PASSWORD
1429+
$dockerUsername = $env:DOCKER_USERNAME
1430+
if ($dockerPassword -and $dockerUsername)
1431+
{
1432+
Write-Host "Authenticating to registry..." -ForegroundColor Gray
1433+
$dockerPassword | docker @dockerConfigArg login $dockerRegistry --username $dockerUsername --password-stdin 2> $null
1434+
if ($LASTEXITCODE -ne 0)
1435+
{
1436+
Write-Host "Warning: Registry authentication failed. Pull/push may fail." -ForegroundColor Yellow
1437+
}
1438+
}
1439+
else
1440+
{
1441+
Write-Host "Warning: DOCKER_USERNAME/DOCKER_PASSWORD not set. Registry pull/push may fail." -ForegroundColor Yellow
1442+
}
1443+
1444+
# Check if image already exists locally
14201445
docker image inspect $ImageTag *> $null
14211446
if ($LASTEXITCODE -eq 0)
14221447
{
14231448
Write-Host "Using locally cached image: $ImageTag" -ForegroundColor Green
14241449
$NoBuildImage = $true
1425-
}
1426-
else
1427-
{
1428-
# Create a temporary Docker config directory to avoid credential helper issues
1429-
# (e.g., docker-credential-desktop not found when using Docker Engine without Desktop)
1430-
$tempDockerConfig = Join-Path $env:TEMP "docker-config-$( New-Guid )"
1431-
New-Item -ItemType Directory -Path $tempDockerConfig -Force | Out-Null
1432-
@{ auths = @{ } } | ConvertTo-Json | Set-Content (Join-Path $tempDockerConfig "config.json")
1433-
$dockerConfigArg = @("--config", $tempDockerConfig)
1434-
1435-
# Authenticate to registry
1436-
$dockerPassword = $env:DOCKER_PASSWORD
1437-
$dockerUsername = $env:DOCKER_USERNAME
1438-
if ($dockerPassword -and $dockerUsername)
1450+
1451+
# Check if image also exists in registry; if not, push it
1452+
docker @dockerConfigArg manifest inspect $ImageTag *> $null
1453+
if ($LASTEXITCODE -eq 0)
14391454
{
1440-
Write-Host "Authenticating to registry..." -ForegroundColor Gray
1441-
$dockerPassword | docker @dockerConfigArg login $dockerRegistry --username $dockerUsername --password-stdin 2> $null
1442-
if ($LASTEXITCODE -ne 0)
1443-
{
1444-
Write-Host "Warning: Registry authentication failed. Pull/push may fail." -ForegroundColor Yellow
1445-
}
1455+
$imageExistsInRegistry = $true
14461456
}
14471457
else
14481458
{
1449-
Write-Host "Warning: DOCKER_USERNAME/DOCKER_PASSWORD not set. Registry pull/push may fail." -ForegroundColor Yellow
1459+
Write-Host "Image not yet in registry, will push after container run." -ForegroundColor Cyan
14501460
}
1451-
1452-
# Try to pull the image
1461+
}
1462+
else
1463+
{
1464+
# Try to pull the image from registry
14531465
Write-Host "Checking registry for existing image: $ImageTag" -ForegroundColor Cyan
14541466
docker @dockerConfigArg pull $ImageTag 2> $null
14551467
if ($LASTEXITCODE -eq 0)
14561468
{
14571469
Write-Host "Using cached image from registry." -ForegroundColor Green
14581470
$NoBuildImage = $true
1471+
$imageExistsInRegistry = $true
14591472
}
14601473
else
14611474
{
@@ -1540,17 +1553,6 @@ RUN if [ -n "`$MOUNTPOINTS" ]; then \
15401553
}
15411554

15421555
$builtNewImage = $true
1543-
1544-
# Auto-push to registry after successful build (async)
1545-
$pushJob = $null
1546-
if ($dockerRegistry)
1547-
{
1548-
Write-Host "Starting async push to registry: $ImageTag" -ForegroundColor Cyan
1549-
$script:RegistryPushJob = Start-Job -ScriptBlock {
1550-
docker @using:dockerConfigArg push $using:ImageTag 2>&1
1551-
$LASTEXITCODE
1552-
}
1553-
}
15541556
}
15551557
else
15561558
{
@@ -1562,8 +1564,16 @@ RUN if [ -n "`$MOUNTPOINTS" ]; then \
15621564
{
15631565
Write-Host "Skipping image build (-NoBuildImage specified)." -ForegroundColor Yellow
15641566
}
1567+
}
15651568

1566-
1569+
# Auto-push to registry if image is not already there (after build or from local cache)
1570+
if ($dockerRegistry -and -not $imageExistsInRegistry -and -not $existingContainerId)
1571+
{
1572+
Write-Host "Starting async push to registry: $ImageTag" -ForegroundColor Cyan
1573+
$script:RegistryPushJob = Start-Job -ScriptBlock {
1574+
docker @using:dockerConfigArg push $using:ImageTag 2>&1
1575+
$LASTEXITCODE
1576+
}
15671577
}
15681578

15691579

src/PostSharp.Engineering.BuildTools/Resources/DockerBuild.ps1

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,49 +1413,62 @@ $envVarAssignments$gitConfigCommands$postInitCommands
14131413
# Registry authentication and pull logic
14141414
$builtNewImage = $false
14151415
$dockerConfigArg = @()
1416+
$imageExistsInRegistry = $false
14161417

14171418
if ($dockerRegistry -and -not $NoBuildImage -and -not $existingContainerId)
14181419
{
1419-
# Check if image already exists locally before pulling from registry
1420+
# Create a temporary Docker config directory to avoid credential helper issues
1421+
# (e.g., docker-credential-desktop not found when using Docker Engine without Desktop)
1422+
$tempDockerConfig = Join-Path $env:TEMP "docker-config-$( New-Guid )"
1423+
New-Item -ItemType Directory -Path $tempDockerConfig -Force | Out-Null
1424+
@{ auths = @{ } } | ConvertTo-Json | Set-Content (Join-Path $tempDockerConfig "config.json")
1425+
$dockerConfigArg = @("--config", $tempDockerConfig)
1426+
1427+
# Authenticate to registry
1428+
$dockerPassword = $env:DOCKER_PASSWORD
1429+
$dockerUsername = $env:DOCKER_USERNAME
1430+
if ($dockerPassword -and $dockerUsername)
1431+
{
1432+
Write-Host "Authenticating to registry..." -ForegroundColor Gray
1433+
$dockerPassword | docker @dockerConfigArg login $dockerRegistry --username $dockerUsername --password-stdin 2> $null
1434+
if ($LASTEXITCODE -ne 0)
1435+
{
1436+
Write-Host "Warning: Registry authentication failed. Pull/push may fail." -ForegroundColor Yellow
1437+
}
1438+
}
1439+
else
1440+
{
1441+
Write-Host "Warning: DOCKER_USERNAME/DOCKER_PASSWORD not set. Registry pull/push may fail." -ForegroundColor Yellow
1442+
}
1443+
1444+
# Check if image already exists locally
14201445
docker image inspect $ImageTag *> $null
14211446
if ($LASTEXITCODE -eq 0)
14221447
{
14231448
Write-Host "Using locally cached image: $ImageTag" -ForegroundColor Green
14241449
$NoBuildImage = $true
1425-
}
1426-
else
1427-
{
1428-
# Create a temporary Docker config directory to avoid credential helper issues
1429-
# (e.g., docker-credential-desktop not found when using Docker Engine without Desktop)
1430-
$tempDockerConfig = Join-Path $env:TEMP "docker-config-$( New-Guid )"
1431-
New-Item -ItemType Directory -Path $tempDockerConfig -Force | Out-Null
1432-
@{ auths = @{ } } | ConvertTo-Json | Set-Content (Join-Path $tempDockerConfig "config.json")
1433-
$dockerConfigArg = @("--config", $tempDockerConfig)
1434-
1435-
# Authenticate to registry
1436-
$dockerPassword = $env:DOCKER_PASSWORD
1437-
$dockerUsername = $env:DOCKER_USERNAME
1438-
if ($dockerPassword -and $dockerUsername)
1450+
1451+
# Check if image also exists in registry; if not, push it
1452+
docker @dockerConfigArg manifest inspect $ImageTag *> $null
1453+
if ($LASTEXITCODE -eq 0)
14391454
{
1440-
Write-Host "Authenticating to registry..." -ForegroundColor Gray
1441-
$dockerPassword | docker @dockerConfigArg login $dockerRegistry --username $dockerUsername --password-stdin 2> $null
1442-
if ($LASTEXITCODE -ne 0)
1443-
{
1444-
Write-Host "Warning: Registry authentication failed. Pull/push may fail." -ForegroundColor Yellow
1445-
}
1455+
$imageExistsInRegistry = $true
14461456
}
14471457
else
14481458
{
1449-
Write-Host "Warning: DOCKER_USERNAME/DOCKER_PASSWORD not set. Registry pull/push may fail." -ForegroundColor Yellow
1459+
Write-Host "Image not yet in registry, will push after container run." -ForegroundColor Cyan
14501460
}
1451-
1452-
# Try to pull the image
1461+
}
1462+
else
1463+
{
1464+
# Try to pull the image from registry
14531465
Write-Host "Checking registry for existing image: $ImageTag" -ForegroundColor Cyan
14541466
docker @dockerConfigArg pull $ImageTag 2> $null
14551467
if ($LASTEXITCODE -eq 0)
14561468
{
14571469
Write-Host "Using cached image from registry." -ForegroundColor Green
14581470
$NoBuildImage = $true
1471+
$imageExistsInRegistry = $true
14591472
}
14601473
else
14611474
{
@@ -1540,17 +1553,6 @@ RUN if [ -n "`$MOUNTPOINTS" ]; then \
15401553
}
15411554

15421555
$builtNewImage = $true
1543-
1544-
# Auto-push to registry after successful build (async)
1545-
$pushJob = $null
1546-
if ($dockerRegistry)
1547-
{
1548-
Write-Host "Starting async push to registry: $ImageTag" -ForegroundColor Cyan
1549-
$script:RegistryPushJob = Start-Job -ScriptBlock {
1550-
docker @using:dockerConfigArg push $using:ImageTag 2>&1
1551-
$LASTEXITCODE
1552-
}
1553-
}
15541556
}
15551557
else
15561558
{
@@ -1562,8 +1564,16 @@ RUN if [ -n "`$MOUNTPOINTS" ]; then \
15621564
{
15631565
Write-Host "Skipping image build (-NoBuildImage specified)." -ForegroundColor Yellow
15641566
}
1567+
}
15651568

1566-
1569+
# Auto-push to registry if image is not already there (after build or from local cache)
1570+
if ($dockerRegistry -and -not $imageExistsInRegistry -and -not $existingContainerId)
1571+
{
1572+
Write-Host "Starting async push to registry: $ImageTag" -ForegroundColor Cyan
1573+
$script:RegistryPushJob = Start-Job -ScriptBlock {
1574+
docker @using:dockerConfigArg push $using:ImageTag 2>&1
1575+
$LASTEXITCODE
1576+
}
15671577
}
15681578

15691579

0 commit comments

Comments
 (0)