diff --git a/.github/workflows/pr-build.yml b/.github/workflows/pr-build.yml index 65cfaf0..147d3af 100644 --- a/.github/workflows/pr-build.yml +++ b/.github/workflows/pr-build.yml @@ -40,16 +40,14 @@ jobs: - name: Record PR size id: pr-size - run: | - echo "human=$(docker images sprisa/opencode:pr-${{ github.event.number || 'manual' }} --format '{{.Size}}')" >> "$GITHUB_OUTPUT" - echo "bytes=$(docker image inspect sprisa/opencode:pr-${{ github.event.number || 'manual' }} --format '{{.Size}}')" >> "$GITHUB_OUTPUT" + run: bash head/scripts/image-size.sh "sprisa/opencode:pr-${{ github.event.number || 'manual' }}" >> "$GITHUB_OUTPUT" - name: Check if image-relevant files changed id: check-diff if: github.event_name == 'pull_request' run: | git -C head fetch origin "${{ github.base_ref }}" --depth=1 - CHANGED=$(git -C head diff "origin/${{ github.base_ref }}" HEAD --name-only -- Dockerfile version.txt version-v1.txt entrypoint.sh entrypoint-v1.sh scripts/init-home.sh) + CHANGED=$(git -C head diff "origin/${{ github.base_ref }}" HEAD --name-only -- Dockerfile version.txt version-v1.txt entrypoint.sh entrypoint-v1.sh scripts/init-home.sh mise-config.toml mise-instructions.md) if [ -n "$CHANGED" ]; then echo "changed=true" >> "$GITHUB_OUTPUT" else @@ -82,44 +80,40 @@ jobs: - name: Record baseline size if: steps.check-diff.outputs.changed == 'true' id: baseline-size - run: | - echo "human=$(docker images sprisa/opencode:baseline --format '{{.Size}}')" >> "$GITHUB_OUTPUT" - echo "bytes=$(docker image inspect sprisa/opencode:baseline --format '{{.Size}}')" >> "$GITHUB_OUTPUT" + run: bash head/scripts/image-size.sh sprisa/opencode:baseline >> "$GITHUB_OUTPUT" + # Sizes are amd64 and decimal (1 MB = 1,000,000 bytes), like Docker's. - name: Report image size - id: compare + env: + CHANGED: ${{ steps.check-diff.outputs.changed }} + PR_UNPACKED: ${{ steps.pr-size.outputs.unpacked }} + PR_COMPRESSED: ${{ steps.pr-size.outputs.compressed }} + BASE_UNPACKED: ${{ steps.baseline-size.outputs.unpacked }} + BASE_COMPRESSED: ${{ steps.baseline-size.outputs.compressed }} run: | - PR_NUM="${{ github.event.number }}" - TAG="pr-${PR_NUM:-manual}" - PR_HUMAN="${{ steps.pr-size.outputs.human }}" - PR_BYTES=${{ steps.pr-size.outputs.bytes }} - - if [ "${{ steps.check-diff.outputs.changed }}" = "true" ]; then - BASELINE_HUMAN="${{ steps.baseline-size.outputs.human }}" - BASELINE_BYTES=${{ steps.baseline-size.outputs.bytes }} - DIFF_BYTES=$(( PR_BYTES - BASELINE_BYTES )) - if [ "$DIFF_BYTES" -gt 0 ]; then - SIGN="+" - ABS_DIFF=$DIFF_BYTES - else - SIGN="" - ABS_DIFF=$(( -DIFF_BYTES )) - fi - DIFF_HUMAN=$(numfmt --to=iec --suffix=B "$ABS_DIFF" 2>/dev/null || echo "${ABS_DIFF} B") - fi + TAG="pr-${{ github.event.number || 'manual' }}" + human() { numfmt --to=si --round=nearest --format='%.1f' --suffix=B "$1"; } + diff_human() { + local diff=$(( $1 - $2 )) + if [ "$diff" -lt 0 ]; then echo "-$(human $(( -diff )))"; else echo "+$(human "$diff")"; fi + } { echo "## Docker image size" echo "" - echo "| Image | Size |" - echo "|---|---|" - echo "| This PR | $PR_HUMAN |" - - if [ "${{ steps.check-diff.outputs.changed }}" = "true" ]; then - echo "| Baseline (main) | $BASELINE_HUMAN |" - echo "| **Difference** | **${SIGN}${DIFF_HUMAN}** |" + echo "| Image | Unpacked | Compressed (download) |" + echo "|---|---|---|" + if [ "$CHANGED" = "true" ]; then + echo "| Baseline (main) | $(human "$BASE_UNPACKED") | $(human "$BASE_COMPRESSED") |" + fi + echo "| This PR | $(human "$PR_UNPACKED") | $(human "$PR_COMPRESSED") |" + if [ "$CHANGED" = "true" ]; then + echo "| **Difference** | **$(diff_human "$PR_UNPACKED" "$BASE_UNPACKED")** | **$(diff_human "$PR_COMPRESSED" "$BASE_COMPRESSED")** |" fi + } > "$RUNNER_TEMP/size-comment.md" + { + cat "$RUNNER_TEMP/size-comment.md" echo "" echo "### PR layers" echo "" @@ -130,46 +124,17 @@ jobs: --no-trunc | tail -n +2 } >> "$GITHUB_STEP_SUMMARY" - echo "pr: $PR_HUMAN ($PR_BYTES bytes)" - - echo "pr-human=${PR_HUMAN}" >> "$GITHUB_OUTPUT" - echo "diff-human=${SIGN}${DIFF_HUMAN}" >> "$GITHUB_OUTPUT" + cat "$RUNNER_TEMP/size-comment.md" - name: Comment on PR if: github.event_name == 'pull_request' env: GH_TOKEN: ${{ github.token }} run: | - PR_HUMAN="${{ steps.pr-size.outputs.human }}" - - BODY=$(cat </dev/null \ + --body-file "$RUNNER_TEMP/size-comment.md" --edit-last 2>/dev/null \ || gh pr comment "${{ github.event.number }}" \ --repo "${{ github.repository }}" \ - --body "$BODY" + --body-file "$RUNNER_TEMP/size-comment.md" diff --git a/.github/workflows/pr-test.yml b/.github/workflows/pr-test.yml index 29cc9d6..f325093 100644 --- a/.github/workflows/pr-test.yml +++ b/.github/workflows/pr-test.yml @@ -85,6 +85,9 @@ jobs: test "package managers and zerobrew-backed lazy tools work in the real image" \ "bash tests/test-toolchain-e2e.sh opencode-test:latest" + test "Homebrew install, upgrade, uninstall, taps, and update work" \ + "bash tests/test-homebrew-e2e.sh opencode-test:latest" + test "locked tools install without version lookups; user tools resolve latest" \ "bash tests/test-mise-lock-e2e.sh opencode-test:latest" diff --git a/Dockerfile b/Dockerfile index 26f13d3..0b848ce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,15 +55,25 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential pkg-config xz-utils \ && rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*.deb -# 1. Homebrew — partial clone with --filter=blob:none avoids downloading -# all past file versions, saving ~70 MB while retaining the git metadata -# Homebrew expects. The release tag is pinned for reproducible builds. +# 1. Homebrew — shallow clone of the pinned release tag. Homebrew needs its +# .git directory (deleting it breaks brew), but not the project history: +# --depth 1 keeps a working repository without the ~50 MB of past commits +# and trees a --filter=blob:none partial clone still downloads. Cloning a +# tag leaves no origin/main, which makes the first `brew update` print +# "Not a valid ref", so also fetch main's tip (~2 MB) with the normal refspec. +# `brew cleanup` downloads Homebrew's portable Ruby (45 MB); drop it so brew +# fetches it on its first Ruby-backed command instead (~4s), like a fresh +# Homebrew install does. ARG HOMEBREW_VERSION=7.0.11 RUN mkdir -p /home/linuxbrew \ && chown opencode:opencode /home/linuxbrew \ - && sudo -u opencode git clone --filter=blob:none \ + && sudo -u opencode git clone --depth 1 \ --branch "${HOMEBREW_VERSION}" \ https://github.com/Homebrew/brew /home/linuxbrew/.linuxbrew/Homebrew \ + && sudo -u opencode git -C /home/linuxbrew/.linuxbrew/Homebrew \ + config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*' \ + && sudo -u opencode git -C /home/linuxbrew/.linuxbrew/Homebrew \ + fetch --quiet --depth 1 origin main \ && sudo -u opencode git -C /home/linuxbrew/.linuxbrew/Homebrew \ checkout --quiet -B stable "${HOMEBREW_VERSION}" \ && sudo -u opencode mkdir -p /home/linuxbrew/.linuxbrew/bin \ @@ -71,6 +81,7 @@ RUN mkdir -p /home/linuxbrew \ /home/linuxbrew/.linuxbrew/Homebrew/bin/brew /home/linuxbrew/.linuxbrew/bin/brew \ && sudo -u opencode /home/linuxbrew/.linuxbrew/bin/brew cleanup --prune=all \ && sudo -u opencode rm -rf "$(sudo -u opencode /home/linuxbrew/.linuxbrew/bin/brew --cache)" \ + && rm -rf /home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/vendor/portable-ruby \ && rm -rf /home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/test \ && rm -rf /home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/vendor/bundle/ruby/*/cache \ && rm -rf /home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/vendor/bundle/ruby/*/doc \ @@ -85,7 +96,9 @@ ARG MISE_ZEROBREW_PLUGIN_COMMIT=998174989a12910f82c1f6e791a432731ed2647d # 1.5. mise — dev tool manager; pre-approved tools defined in the global config # auto-install on first use at runtime. The release is # pinned so Docker caching cannot silently retain an old moving target. +# The release binary ships with symbols; stripping them saves ~18 MB. RUN curl -fsSL https://mise.run | MISE_VERSION="${MISE_VERSION}" MISE_INSTALL_PATH=/usr/local/bin/mise sh \ + && strip /usr/local/bin/mise \ && mkdir -p /opt/mise \ && MISE_DATA_DIR=/opt/mise mise plugins install zerobrew "https://github.com/kennyg/mise-zerobrew#${MISE_ZEROBREW_PLUGIN_COMMIT}" \ && sed -i '/quoted_path .. " install /i\ cmd.exec(quoted_zb .. " --root " .. quoted_path .. " init")' /opt/mise/plugins/zerobrew/hooks/backend_install.lua @@ -207,8 +220,13 @@ RUN echo "locking mise tools for opencode ${OPENCODE_VERSION}" \ && chown opencode:opencode /etc/mise /etc/mise/mise.lock \ && chown -R opencode:opencode /opt/mise -# Verify runtime and set up login-shell PATH and auto-install handler +# Verify runtime and set up login-shell PATH and auto-install handler. +# The version check leaves Bun's extracted native library (~7 MB) in /tmp and +# opencode logs under /root; Bun re-extracts at runtime, so drop both. +# Only chown the home files this step creates: chown -R over the whole home +# would copy the zerobrew files from the layer above into this one. RUN opencode --version \ + && rm -rf /tmp/.bun-* /tmp/opencode /root/.local/share/opencode /root/.local/state/opencode \ && install -m 0644 /dev/null /etc/mise/empty.toml \ && mkdir -p /etc/opencode \ && printf '%s\n' '{"$schema":"https://opencode.ai/config.json","instructions":["/etc/opencode/mise-instructions.md"]}' > /etc/opencode/opencode.json \ @@ -238,7 +256,7 @@ RUN opencode --version \ && mkdir -p /opt/mise/shims \ && chown opencode:opencode /opt/mise/shims \ && mkdir -p /home/opencode/workspace \ - && chown -R opencode:opencode /home/opencode + && chown -R opencode:opencode /home/opencode/.zshrc /home/opencode/.config /home/opencode/workspace USER opencode ENV HOME=/home/opencode diff --git a/README.md b/README.md index 6712fcf..edc3980 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,14 @@ tells agents to try mise before apt. Other package managers and paths - **Homebrew** (`brew`) is installed under `/home/linuxbrew/.linuxbrew`. It runs on - its own bundled Ruby. + its own Ruby, which it downloads the first time you run a `brew` command that + needs it. That takes a few seconds. Formulas with a prebuilt bottle install + directly. Formulas without one, which includes most third-party taps, count + as builds from source, and Homebrew refuses those unless `gcc` or `clang` is + on `PATH`. That's true even when the formula only copies a prebuilt binary. + For those, run `sudo apt-get install -y gcc` first (about 170 MB). Formulas + that really compile code need `sudo apt-get install -y build-essential` + (about 260 MB). - **zerobrew** (`zb`), a faster Homebrew client, is at `/usr/local/bin/zb`. - **`~/.local/bin`** is on `PATH` and writable, so you can drop your own binaries there. @@ -294,6 +301,7 @@ The end-to-end tests run against a built image: ```sh bash tests/test-server-e2e.sh sprisa/opencode: bash tests/test-toolchain-e2e.sh sprisa/opencode: +bash tests/test-homebrew-e2e.sh sprisa/opencode: bash tests/test-home-tools-e2e.sh sprisa/opencode: bash tests/test-mise-lock-e2e.sh sprisa/opencode: ``` diff --git a/scripts/image-size.sh b/scripts/image-size.sh new file mode 100644 index 0000000..aae54b2 --- /dev/null +++ b/scripts/image-size.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# +# Print a local image's unpacked and compressed sizes in bytes, as +# "unpacked=" and "compressed=" lines (GITHUB_OUTPUT format). +# +# Usage: scripts/image-size.sh +# +# `docker images` and `docker image inspect` report different things depending +# on the image store: with the containerd store (GitHub runners), `docker images` +# shows unpacked plus compressed bytes and `inspect` shows only compressed +# bytes. Measure both directly instead, so every runner reports the same +# numbers. Unpacked is the sum of the layer sizes; compressed is the gzipped +# `docker save` output, close to what `docker pull` downloads. +set -euo pipefail + +IMAGE="${1:?usage: image-size.sh }" + +unpacked="$(docker history --human=false --format '{{.Size}}' "$IMAGE" | awk '{ total += $1 } END { print total }')" +compressed="$(docker save "$IMAGE" | gzip -6 | wc -c | tr -d ' ')" + +echo "unpacked=${unpacked}" +echo "compressed=${compressed}" diff --git a/tests/test-homebrew-e2e.sh b/tests/test-homebrew-e2e.sh new file mode 100644 index 0000000..e42a6db --- /dev/null +++ b/tests/test-homebrew-e2e.sh @@ -0,0 +1,103 @@ +#!/usr/bin/env bash +# Exercise Homebrew end to end in the real image. +# Usage: bash tests/test-homebrew-e2e.sh +# +# The image trims Homebrew to save space: a shallow clone of the pinned release +# and no bundled portable Ruby. Deleting Homebrew's .git broke brew before, so +# this covers the whole lifecycle rather than just `brew --version`. +set -euo pipefail + +IMAGE="${1:?usage: tests/test-homebrew-e2e.sh }" + +echo "Testing Homebrew (${IMAGE}; 15-minute timeout)..." +docker run --rm -i -e GITHUB_TOKEN --entrypoint timeout "$IMAGE" 900 bash -s <<'TEST' +set -euo pipefail +fail() { echo "FAIL: $*" >&2; exit 1; } +ok() { echo "ok: $*"; } + +prefix=/home/linuxbrew/.linuxbrew +repo="$prefix/Homebrew" +cellar="$prefix/Cellar" + +# Setup, before brew touches the network. +[ "$(command -v brew)" = "$prefix/bin/brew" ] || fail "brew is not on PATH (got '$(command -v brew)')" +[ "$(bash -lc 'command -v brew')" = "$prefix/bin/brew" ] || fail "brew is not on PATH in a login shell" +[ "${HOMEBREW_NO_AUTO_UPDATE:-}" = 1 ] || fail "HOMEBREW_NO_AUTO_UPDATE is not set" +[ "${HOMEBREW_INSTALL_FROM_API:-}" = 1 ] || fail "HOMEBREW_INSTALL_FROM_API is not set" +[ -w "$prefix" ] && [ -w "$repo" ] || fail "the opencode user cannot write to Homebrew" +[ ! -d "$repo/Library/Taps/homebrew/homebrew-core" ] || fail "homebrew-core is tapped; the image installs from the API" +ok "brew is on PATH for login and non-login shells, writable, and in API mode" + +[ -d "$repo/.git" ] || fail "Homebrew's .git is missing; brew needs it" +[ "$(git -C "$repo" rev-parse --is-shallow-repository)" = true ] || fail "Homebrew is no longer a shallow clone" +git -C "$repo" rev-parse --verify --quiet refs/remotes/origin/main >/dev/null || fail "origin/main is missing; brew update prints an error" +tag="$(git -C "$repo" describe --tags --exact-match)" || fail "HEAD is not a release tag" +[ "$(brew --version | head -1)" = "Homebrew $tag" ] || fail "brew --version does not match the checked-out tag $tag" +[ ! -e "$repo/Library/Homebrew/vendor/portable-ruby" ] || fail "the image ships portable Ruby (45 MB); brew should download it on first use" +ok "shallow clone of Homebrew $tag with origin/main and no bundled portable Ruby" + +# First Ruby-backed command downloads Homebrew's own portable Ruby. +brew config >/tmp/config.log 2>&1 || { tail -5 /tmp/config.log >&2; fail "brew config failed"; } +grep -q "^Homebrew Ruby: .* => $repo/Library/Homebrew/vendor/portable-ruby/" /tmp/config.log \ + || { grep -i ruby /tmp/config.log >&2; fail "brew is not running on its portable Ruby"; } +ok "first brew command downloads and uses Homebrew's portable Ruby" + +# brew doctor may only report the image's known, harmless warnings: no +# compiler, and the files the build deletes from the Homebrew checkout. +brew doctor >/tmp/doctor.log 2>&1 || true +unexpected="$(grep '^Warning:' /tmp/doctor.log \ + | grep -vE 'No developer tools installed|You have uncommitted modifications to Homebrew/brew' || true)" +[ -z "$unexpected" ] || { cat /tmp/doctor.log >&2; fail "brew doctor reported new warnings: $unexpected"; } +ok "brew doctor reports only the known warnings" + +# Install a bottle with a dependency, link it, and run it. +brew install jq >/tmp/install.log 2>&1 || { tail -5 /tmp/install.log >&2; fail "brew install jq failed"; } +brew deps --installed jq | grep -qx oniguruma || fail "jq's dependency oniguruma was not installed" +[ "$("$prefix/bin/jq" -n '1 + 1')" = 2 ] || fail "Homebrew's jq does not run" +if ldd "$prefix/bin/jq" | grep -q 'not found'; then ldd "$prefix/bin/jq" >&2; fail "jq has unresolved libraries"; fi +ok "brew install resolves dependencies and links working binaries" + +# Upgrade: relabel an installed keg as an older version so it is outdated. +brew install hello >/dev/null 2>&1 || fail "brew install hello failed" +current="$(ls "$cellar/hello")" +brew unlink hello >/dev/null +mv "$cellar/hello/$current" "$cellar/hello/0.1" +brew link --overwrite hello >/dev/null || fail "brew link failed for the relabeled hello" +brew outdated --formula | grep -qx hello || fail "brew outdated did not report the old hello" +brew upgrade >/tmp/upgrade.log 2>&1 || { tail -5 /tmp/upgrade.log >&2; fail "brew upgrade failed"; } +[ "$(ls "$cellar/hello")" = "$current" ] || fail "brew upgrade left hello at $(ls "$cellar/hello")" +"$prefix/bin/hello" --version >/dev/null || fail "upgraded hello does not run" +ok "brew outdated and brew upgrade bring hello back to $current" + +# Uninstall and remove orphaned dependencies. +brew uninstall jq >/dev/null 2>&1 || fail "brew uninstall jq failed" +brew autoremove >/dev/null 2>&1 || fail "brew autoremove failed" +if brew list --formula | grep -qxE 'jq|oniguruma'; then fail "jq or oniguruma is still installed"; fi +[ ! -e "$prefix/bin/jq" ] || fail "jq is still linked" +ok "brew uninstall and autoremove remove jq and oniguruma" + +# Third-party taps need trusting in Homebrew 7. Installing a tap formula +# without a bottle needs a compiler, which the image doesn't ship (the README +# says to install build-essential first), so only check the tap itself here. +brew tap sprisa/tap >/dev/null 2>&1 || fail "brew tap sprisa/tap failed" +brew trust sprisa/tap >/dev/null 2>&1 || fail "brew trust sprisa/tap failed" +brew info sprisa/tap/clonetree >/tmp/tap.log 2>&1 || { tail -5 /tmp/tap.log >&2; fail "brew info for a tap formula failed"; } +ok "brew tap, trust, and info for a third-party tap" + +brew cleanup >/dev/null 2>&1 || fail "brew cleanup failed" +ok "brew cleanup" + +# Last, so the steps above run on the pinned release. Updating moves Homebrew +# to a newer release through the shallow clone, without errors. Exit codes +# alone miss a broken repository: without .git, brew reports +# "Homebrew >=4.3.0 (shallow or no git repository)", and `brew update` re-clones +# every branch and lands on unreleased main (e.g. 7.0.13-128-gfc51ba6). +brew update >/tmp/update.log 2>&1 || { tail -5 /tmp/update.log >&2; fail "brew update failed"; } +if grep -qiE '^(error|fatal)' /tmp/update.log; then grep -iE '^(error|fatal)' /tmp/update.log >&2; fail "brew update printed errors"; fi +updated="$(brew --version | head -1)" +[[ "$updated" =~ ^Homebrew\ [0-9]+\.[0-9]+\.[0-9]+$ ]] || fail "brew update left a non-release version: $updated" +brew config >/dev/null 2>&1 || fail "brew config failed after brew update" +ok "brew update moves from $tag to the release ${updated#Homebrew }" +TEST + +echo "PASS: Homebrew e2e (${IMAGE})"