<# .SYNOPSIS Build a local Docker image from the committed production branch. .DESCRIPTION Does not test, promote, push Git branches, use SSH, or start a container. The image stays local. Use 3-push-image.ps1 to publish it separately. #> [CmdletBinding()] param() Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' . "$PSScriptRoot/_common.ps1" $Image = Setting IMAGE -Required $Branch = Setting BRANCH 'production' $Platform = Setting PLATFORM 'linux/amd64' Push-Location $root try { $commit = git rev-parse --verify "$Branch^{commit}" Assert-Ok 'resolve production commit' $commit = $commit.Trim() $sha = (git rev-parse --short $commit).Trim() Assert-Ok 'resolve image tag' $tagged = "${Image}:${sha}" $latest = "${Image}:latest" # --------------------------------------------------------------- build # `git archive` to a file and unpack it, rather than piping it. A PowerShell # pipeline carries text: it would re-encode a tar stream and hand docker a # corrupted context. Writing the archive out and building from the directory # keeps the same guarantee -- the context is the committed tree and nothing # else -- without a byte going through a pipe. # # The Dockerfile comes out of that same tree, at deploy/Dockerfile, so the # recipe is versioned with the code it builds. Docker reads the ignore rules # from deploy/Dockerfile.dockerignore beside it. Write-Step "build $tagged" $ctx = Join-Path ([IO.Path]::GetTempPath()) "$Name-build-$sha" $tar = "$ctx.tar" Remove-ReleaseTemp @($ctx, $tar) New-Item -ItemType Directory -Path $ctx | Out-Null try { git archive --format=tar -o $tar $commit; Assert-Ok 'git archive' tar -xf $tar -C $ctx; Assert-Ok 'tar -xf' $dockerfile = Join-Path $ctx 'deploy/Dockerfile' docker build --platform $Platform -f $dockerfile -t $tagged -t $latest $ctx Assert-Ok 'docker build' docker image ls $Image --format ' {{.Tag}} {{.Size}}' } finally { Remove-ReleaseTemp @($ctx, $tar) } Write-Host "Image: $tagged" } finally { Pop-Location }