<# .SYNOPSIS Test the latest local main commit and fast-forward production to it. .DESCRIPTION Uses a fixed commit and an isolated worktree. PushBranch optionally publishes production to origin; without it promotion stays local. Does not run Docker. #> [CmdletBinding()] param([switch] $PushBranch, [switch] $NoTests) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' . "$PSScriptRoot/_common.ps1" $Source = Setting SOURCE 'main' $Branch = Setting BRANCH 'production' $Python = Setting PYTHON (Join-Path $root '.venv\Scripts\python.exe') Push-Location $root try { $commit = git rev-parse --verify "$Source^{commit}" Assert-Ok 'resolve source commit' $commit = $commit.Trim() # ------------------------------------------------------- test the commit # In a throwaway worktree, not here. The tests then run against exactly the # tree that is about to become production, with none of the working copy's # uncommitted edits in the way. Worktrees have no .venv of their own, so the # interpreter comes from the main checkout and the code from the worktree. if (-not $NoTests) { Write-Step "tests on $Source" if (-not (Test-Path $Python)) { throw "no interpreter at $Python - set PYTHON in deploy\release.env" } $base = Join-Path ([IO.Path]::GetTempPath()) ("$Name-test-" + [guid]::NewGuid().ToString('N').Substring(0, 8)) $tree = Join-Path $base $Name try { git worktree add --detach $tree $commit | Out-Null Assert-Ok 'git worktree add' Push-Location $tree try { # Your project's checks. A gate that is red for weeks is not a # gate -- it is a flag you learn to type -- so take one out when # it stops meaning anything, and put it back when it is green. # # The starter's dev group is pytest and httpx, so pytest is the # only gate that can run out of the box. Add ruff and mypy to # [dependency-groups] dev in pyproject.toml, then uncomment the # two lines below -- a gate whose tool is not installed fails # every release for a reason that has nothing to do with the code. & $Python -m pytest -q; Assert-Ok 'pytest' # & $Python -m ruff check .; Assert-Ok 'ruff' # & $Python -m mypy --cache-dir (Join-Path $root '.mypy_cache') # Assert-Ok 'mypy' } finally { Pop-Location } } finally { git worktree remove --force $tree 2>$null | Out-Null Remove-ReleaseTemp @($base) } } # ------------------------------------------------------------- promote # Fast-forward only. If production has commits main does not, that is a # release made from somewhere else, and this stops rather than discarding it. Write-Step "promote $Source -> $Branch" git show-ref --verify --quiet "refs/heads/$Branch" if ($LASTEXITCODE -eq 0) { git merge-base --is-ancestor $Branch $commit if ($LASTEXITCODE -ne 0) { Write-Host "$Branch is not an ancestor of $Source - it has commits of its own." -ForegroundColor Red throw "look at: git log --oneline $Source..$Branch" } } # Git publication is optional and independent of the image. git branch -f $Branch $commit; Assert-Ok 'git branch -f' if ($PushBranch) { git push origin "${commit}:refs/heads/$Branch" Assert-Ok 'publish production branch' } Write-Host "Promoted $Branch to $commit" } finally { Pop-Location }