Skip to content

Commit 7adfd57

Browse files
committed
DockerBuild.ps1: fixed -Interactive, added directory hash to image name.
1 parent 430bb14 commit 7adfd57

File tree

4 files changed

+54
-28
lines changed

4 files changed

+54
-28
lines changed

Build.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ if (-not $Interactive -or $BuildArgs)
6060
Set-Location $previousLocation
6161
}
6262
}
63+
64+
if ( $Interactive ) {
65+
Write-Host "Entering interactive PowerShell." -ForegroundColor Green
66+
}

DockerBuild.ps1

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,20 @@ if ( [string]::IsNullOrEmpty($ImageName))
9696
# Get full path without drive name (e.g., "C:\src\Metalama.Compiler" becomes "src\Metalama.Compiler")
9797
$fullPath = $PSScriptRoot -replace '^[A-Za-z]:\\', ''
9898
# Sanitize path to valid Docker image name (lowercase alphanumeric and hyphens only)
99-
$ImageName = $fullPath.ToLower() -replace '[^a-z0-9\-]', '-' -replace '-+', '-' -replace '^-|-$', ''
99+
$ImageTag = $fullPath.ToLower() -replace '[^a-z0-9\-]', '-' -replace '-+', '-' -replace '^-|-$', ''
100100
# Ensure it doesn't start with a hyphen and has at least one character
101-
if ([string]::IsNullOrEmpty($ImageName) -or $ImageName -match '^-')
101+
if ([string]::IsNullOrEmpty($ImageTag) -or $ImageTag -match '^-')
102102
{
103-
$ImageName = "docker-build-image"
103+
$ImageTag = "docker-build-image"
104104
}
105-
Write-Host "Generated image name from directory: $ImageName" -ForegroundColor Cyan
105+
Write-Host "Generated image name from directory: $ImageTag" -ForegroundColor Cyan
106+
} else
107+
{
108+
# Generate a hash of the repo directory tagging (4 bytes, 8 hex chars)
109+
$hashBytes = (New-Object -TypeName System.Security.Cryptography.SHA256Managed).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($PSScriptRoot))
110+
$directoryHash = [System.BitConverter]::ToString($hashBytes, 0, 4).Replace("-", "").ToLower()
111+
$ImageTag = "$ImageName`:$directoryHash"
112+
Write-Host "Image will be tagged as: $ImageTag" -ForegroundColor Cyan
106113
}
107114

108115
# When building locally (as opposed as on the build agent), we must do a complete cleanup because
@@ -255,16 +262,16 @@ Write-Host "Mount points: " $mountPointsAsString -ForegroundColor Gray
255262
Write-Host "Git directories: " $gitDirectoriesAsString -ForegroundColor Gray
256263

257264
# Kill all containers
258-
docker ps -q --filter "ancestor=$ImageName" | ForEach-Object {
265+
docker ps -q --filter "ancestor=$ImageTag" | ForEach-Object {
259266
Write-Host "Killing container $_"
260267
docker kill $_
261268
}
262269

263270
# Building the image.
264271
if (-not $NoBuildImage)
265272
{
266-
Write-Host "Building the image." -ForegroundColor Green
267-
Get-Content -Raw Dockerfile | docker build -t $ImageName --build-arg GITDIRS="$gitDirectoriesAsString" --build-arg MOUNTPOINTS="$mountPointsAsString" -f - $dockerContextDirectory
273+
Write-Host "Building the image with tag: $ImageTag" -ForegroundColor Green
274+
Get-Content -Raw Dockerfile | docker build -t $ImageTag --build-arg GITDIRS="$gitDirectoriesAsString" --build-arg MOUNTPOINTS="$mountPointsAsString" -f - $dockerContextDirectory
268275
if ($LASTEXITCODE -ne 0)
269276
{
270277
Write-Host "Docker build failed with exit code $LASTEXITCODE" -ForegroundColor Red
@@ -293,23 +300,25 @@ if (-not $BuildImage)
293300
if ($Interactive)
294301
{
295302
$pwshArgs = "-NoExit"
296-
$BuildArgs = @("-Interactive") + $BuildArgs
297-
$dockerArgs = @("-it")
303+
$BuildArgs = @("-Interactive") + $BuildArgs
304+
$dockerArgs = @("-it")
305+
$pwshExitCommand = ""
298306
}
299307
else
300308
{
301309
$pwshArgs = "-NonInteractive"
302-
$dockerArgs = @()
310+
$dockerArgs = @()
311+
$pwshExitCommand = "exit `$LASTEXITCODE`;"
303312
}
304313

305314
$buildArgsString = $BuildArgs -join " "
306-
$VolumeMappingsAsString = $VolumeMappings -join " "
307-
$dockerArgsAsString = $dockerArgs -join " "
315+
$VolumeMappingsAsString = $VolumeMappings -join " "
316+
$dockerArgsAsString = $dockerArgs -join " "
308317

309318

310-
Write-Host "Executing: ``docker run --rm --memory= 12g $VolumeMappingsAsString -w $SourceDirName $dockerArgsAsString $ImageName pwsh $pwshArgs -Command `"& .\Build.ps1 $buildArgsString`; exit `$LASTEXITCODE`"``." -ForegroundColor Cyan
319+
Write-Host "Executing: ``docker run --rm --memory=12g $dockerArgsAsString $VolumeMappingsAsString -w $SourceDirName $ImageTag pwsh $pwshArgs -Command `"& .\Build.ps1 $buildArgsString`; $pwshExitCommand`"" -ForegroundColor Cyan
311320

312-
docker run --rm --memory=12g @VolumeMappings -w $SourceDirName @dockerArgs $ImageName pwsh $pwshArgs -Command "& .\Build.ps1 $buildArgsString`; exit `$LASTEXITCODE; "
321+
docker run --rm --memory=12g $dockerArgs @VolumeMappings -w $SourceDirName @dockerArgs $ImageTag pwsh $pwshArgs -Command "& .\Build.ps1 $buildArgsString`; $pwshExitCommand; "
313322
if ($LASTEXITCODE -ne 0)
314323
{
315324
Write-Host "Docker run (build) failed with exit code $LASTEXITCODE" -ForegroundColor Red

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ if (-not $Interactive -or $BuildArgs)
6060
Set-Location $previousLocation
6161
}
6262
}
63+
64+
if ( $Interactive ) {
65+
Write-Host "Entering interactive PowerShell." -ForegroundColor Green
66+
}

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,20 @@ if ( [string]::IsNullOrEmpty($ImageName))
9696
# Get full path without drive name (e.g., "C:\src\Metalama.Compiler" becomes "src\Metalama.Compiler")
9797
$fullPath = $PSScriptRoot -replace '^[A-Za-z]:\\', ''
9898
# Sanitize path to valid Docker image name (lowercase alphanumeric and hyphens only)
99-
$ImageName = $fullPath.ToLower() -replace '[^a-z0-9\-]', '-' -replace '-+', '-' -replace '^-|-$', ''
99+
$ImageTag = $fullPath.ToLower() -replace '[^a-z0-9\-]', '-' -replace '-+', '-' -replace '^-|-$', ''
100100
# Ensure it doesn't start with a hyphen and has at least one character
101-
if ([string]::IsNullOrEmpty($ImageName) -or $ImageName -match '^-')
101+
if ([string]::IsNullOrEmpty($ImageTag) -or $ImageTag -match '^-')
102102
{
103-
$ImageName = "docker-build-image"
103+
$ImageTag = "docker-build-image"
104104
}
105-
Write-Host "Generated image name from directory: $ImageName" -ForegroundColor Cyan
105+
Write-Host "Generated image name from directory: $ImageTag" -ForegroundColor Cyan
106+
} else
107+
{
108+
# Generate a hash of the repo directory tagging (4 bytes, 8 hex chars)
109+
$hashBytes = (New-Object -TypeName System.Security.Cryptography.SHA256Managed).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($PSScriptRoot))
110+
$directoryHash = [System.BitConverter]::ToString($hashBytes, 0, 4).Replace("-", "").ToLower()
111+
$ImageTag = "$ImageName`:$directoryHash"
112+
Write-Host "Image will be tagged as: $ImageTag" -ForegroundColor Cyan
106113
}
107114

108115
# When building locally (as opposed as on the build agent), we must do a complete cleanup because
@@ -255,16 +262,16 @@ Write-Host "Mount points: " $mountPointsAsString -ForegroundColor Gray
255262
Write-Host "Git directories: " $gitDirectoriesAsString -ForegroundColor Gray
256263

257264
# Kill all containers
258-
docker ps -q --filter "ancestor=$ImageName" | ForEach-Object {
265+
docker ps -q --filter "ancestor=$ImageTag" | ForEach-Object {
259266
Write-Host "Killing container $_"
260267
docker kill $_
261268
}
262269

263270
# Building the image.
264271
if (-not $NoBuildImage)
265272
{
266-
Write-Host "Building the image." -ForegroundColor Green
267-
Get-Content -Raw Dockerfile | docker build -t $ImageName --build-arg GITDIRS="$gitDirectoriesAsString" --build-arg MOUNTPOINTS="$mountPointsAsString" -f - $dockerContextDirectory
273+
Write-Host "Building the image with tag: $ImageTag" -ForegroundColor Green
274+
Get-Content -Raw Dockerfile | docker build -t $ImageTag --build-arg GITDIRS="$gitDirectoriesAsString" --build-arg MOUNTPOINTS="$mountPointsAsString" -f - $dockerContextDirectory
268275
if ($LASTEXITCODE -ne 0)
269276
{
270277
Write-Host "Docker build failed with exit code $LASTEXITCODE" -ForegroundColor Red
@@ -293,23 +300,25 @@ if (-not $BuildImage)
293300
if ($Interactive)
294301
{
295302
$pwshArgs = "-NoExit"
296-
$BuildArgs = @("-Interactive") + $BuildArgs
297-
$dockerArgs = @("-it")
303+
$BuildArgs = @("-Interactive") + $BuildArgs
304+
$dockerArgs = @("-it")
305+
$pwshExitCommand = ""
298306
}
299307
else
300308
{
301309
$pwshArgs = "-NonInteractive"
302-
$dockerArgs = @()
310+
$dockerArgs = @()
311+
$pwshExitCommand = "exit `$LASTEXITCODE`;"
303312
}
304313

305314
$buildArgsString = $BuildArgs -join " "
306-
$VolumeMappingsAsString = $VolumeMappings -join " "
307-
$dockerArgsAsString = $dockerArgs -join " "
315+
$VolumeMappingsAsString = $VolumeMappings -join " "
316+
$dockerArgsAsString = $dockerArgs -join " "
308317

309318

310-
Write-Host "Executing: ``docker run --rm --memory= 12g $VolumeMappingsAsString -w $SourceDirName $dockerArgsAsString $ImageName pwsh $pwshArgs -Command `"& .\Build.ps1 $buildArgsString`; exit `$LASTEXITCODE`"``." -ForegroundColor Cyan
319+
Write-Host "Executing: ``docker run --rm --memory=12g $dockerArgsAsString $VolumeMappingsAsString -w $SourceDirName $ImageTag pwsh $pwshArgs -Command `"& .\Build.ps1 $buildArgsString`; $pwshExitCommand`"" -ForegroundColor Cyan
311320

312-
docker run --rm --memory=12g @VolumeMappings -w $SourceDirName @dockerArgs $ImageName pwsh $pwshArgs -Command "& .\Build.ps1 $buildArgsString`; exit `$LASTEXITCODE; "
321+
docker run --rm --memory=12g $dockerArgs @VolumeMappings -w $SourceDirName @dockerArgs $ImageTag pwsh $pwshArgs -Command "& .\Build.ps1 $buildArgsString`; $pwshExitCommand; "
313322
if ($LASTEXITCODE -ne 0)
314323
{
315324
Write-Host "Docker run (build) failed with exit code $LASTEXITCODE" -ForegroundColor Red

0 commit comments

Comments
 (0)