#!/usr/bin/env bash # Test the latest local main commit, fast-forward production to it, and export # it for the image build on Windows. # # bash deploy/1-test-and-promote.sh [--push-branch] [--no-tests] # # Uses a fixed commit and an isolated worktree. --push-branch optionally # publishes production to origin; without it promotion stays local. --no-tests # skips the gates. The export is the production tree in /// # and its sha in //production, RELEASE_DIR defaulting to # release/ beside the checkout. Uncommitted work is never included. set -euo pipefail # shellcheck source=_common.sh . "$(dirname "${BASH_SOURCE[0]}")/_common.sh" push_branch=0 no_tests=0 for arg in "$@"; do case "$arg" in --push-branch) push_branch=1 ;; --no-tests) no_tests=1 ;; -h|--help) sed -n '2,11p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 0 ;; *) die "unknown option $arg" ;; esac done need_git SOURCE=$(setting SOURCE main) BRANCH=$(setting BRANCH production) PYTHON=$(setting PYTHON "$root/.venv/bin/python") cd "$root" commit=$(resolve_commit "$SOURCE" source) # ----------------------------------------------------------- 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. test-commit.sh makes the worktree, runs pytest, # ruff, mypy and the client's check in it, and removes it. Worktrees have no # .venv of their own, so the interpreter comes from this checkout. if ((! no_tests)); then step "tests on $SOURCE" run_or_die tests env PYTHON="$PYTHON" sh "$here/test-commit.sh" "$commit" fi # ------------------------------------------------------------------- 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. step "promote $SOURCE -> $BRANCH" if git show-ref --verify --quiet "refs/heads/$BRANCH"; then if ! git merge-base --is-ancestor "$BRANCH" "$commit"; then echo "$BRANCH is not an ancestor of $SOURCE - it has commits of its own." >&2 die "look at: git log --oneline $SOURCE..$BRANCH" fi fi # Git publication is optional and independent of the image. run_or_die 'git branch -f' git branch -f "$BRANCH" "$commit" if ((push_branch)); then run_or_die 'publish production branch' git push origin "$commit:refs/heads/$BRANCH" fi echo "Promoted $BRANCH to $commit" # -------------------------------------------------------------------- export # The image is built on Windows by wslc (2-build-image.ps1), which reads this # folder through \\wsl.localhost. A tree rather than a tar, so Windows has # nothing to unpack. wslc hands the Dockerfile to the builder on stdin, so the # builder only finds the ignore rules at the root of the context: # Dockerfile.dockerignore is copied there as .dockerignore. A tree that exists # for this sha is kept, not rewritten, because wslc can build from a stale view # of a folder deleted and recreated at the same path (microsoft/WSL#41287). # The promote has happened by now; when the export fails, rerun this step. name=$(basename "$root") release=${RELEASE_DIR:-$(dirname "$root")/release}/$name sha=$(git rev-parse --short "$commit") || die 'resolve image tag failed' tree=$release/$sha step "export $BRANCH ($sha) to $tree" mkdir -p -- "$release" 2>/dev/null && [[ -w $release ]] || die "cannot write $release; $BRANCH is promoted, rerun this step once it is writable" if [[ ! -d $tree ]]; then part=$release/.part-$sha rm -rf -- "$part" mkdir -- "$part" git archive --format=tar "$commit" | tar -x -C "$part" || die 'export failed' if [[ -f $part/deploy/Dockerfile.dockerignore ]]; then cp -- "$part/deploy/Dockerfile.dockerignore" "$part/.dockerignore" fi mv -- "$part" "$tree" fi for old in "$release"/*/; do [[ ${old%/} == "$tree" ]] || rm -rf -- "${old%/}" done printf '%s\n' "$sha" > "$release/production" echo "Exported $tree"