GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [plan] Add BuildKit cache mounts to Dockerfile and CI local cache for self-hosted runner · Issue #270 · sands-lab/h3llo

[plan] Add BuildKit cache mounts to Dockerfile and CI local cache for self-hosted runner #270

New issue
Copy link
New issue
Copy link
Closed
#277
Closed
[plan] Add BuildKit cache mounts to Dockerfile and CI local cache for self-hosted runner#270
#277
Copy link
Labels
agentize:planIssues with implementation plans

Description

@Tonny-Gu
Tonny-Gu
opened on Feb 6, 2026
Issue body actions

Now I have all the information I need. Let me understand the user's resolve selections:

  • 1B: Remove cargo-chef (aggressive)
  • 2B (但不要删掉我的注释): keep-state only, but don't delete the commented-out GHA cache lines
  • 3A: Update all references (docs + source code)

Let me verify compatibility and produce the unified plan.


Implementation Plan: BuildKit Cache Mounts and CI Local Cache

Table of Contents

  • Goal
  • Codebase Analysis
  • Implementation Steps
  • Success Criteria
  • Risks and Mitigations
  • Validation
  • Selection History
  • Refine History

Goal

Add BuildKit --mount=type=cache directives to the Dockerfile to persist cargo registry, git, and compilation artifacts across builds. Remove cargo-chef (replaced by cache mounts). Configure CI caching via keep-state: true for the self-hosted runner. Update all documentation and source code references from docker build to docker buildx build.

Out of scope:

  • Multi-architecture build support
  • Remote cache backends (registry, S3)
  • Changes to application logic or test behavior

Codebase Analysis

File changes:

File Level Purpose
Dockerfile major Remove cargo-chef/planner stages, collapse 5→3 stages, add cache mounts, update header
.github/workflows/ci.yml minor Add keep-state: true to buildx setup (preserve existing GHA cache comments)
docs/test.md medium Update docker build → docker buildx build (3 occurrences + pattern description), add rebuild guidance
tests/e2e/common.rs minor Update error message build command (1 occurrence)
tests/integration/native/tun.rs minor Update rustdoc + panic message (2 occurrences)
tests/integration/native/route.rs minor Update rustdoc + panic message (2 occurrences)

Implementation Steps

Step 1: Rewrite Dockerfile — Remove cargo-chef, add cache mounts, update header

  • File: Dockerfile
  • Changes: Add buildx requirement to header, update usage examples, delete cargo install cargo-chef and planner stage, collapse to 3 stages, add --mount=type=cache with sharing=locked on target dir, copy artifacts to /app/out/ within single RUN, update COPY --from=builder paths
Code Draft
-# Multi-stage Dockerfile for h3llo BareUDP VPN
-# Debian builder + Alpine runtime for static musl binaries
+# syntax=docker/dockerfile:1
+# Multi-stage Dockerfile for h3llo BareUDP VPN (requires Docker Buildx)
+# Debian builder + Alpine runtime for static musl binaries.
+# Uses BuildKit cache mounts to persist cargo registry and compilation
+# artifacts across builds on the same host.
 #
 # Targets:
 #   runtime (default) - Minimal Alpine production image
 #   test              - Adds diagnostic tools for integration tests
 #
 # Usage:
-#   docker build --target runtime -t h3llo:latest .
-#   docker build --target test -t h3llo:test .
+#   docker buildx build --target runtime -t h3llo:latest --load .
+#   docker buildx build --target test -t h3llo:test --load .

-# Stage 1: Chef - Install cargo-chef with Debian (supports libclang dynamic loading)
-FROM rust:slim-trixie AS chef
+# Stage 1: Toolchain - Debian with musl cross-compilation and boring-sys deps
+FROM rust:slim-trixie AS toolchain

 # Install boring-sys dependencies and curl/xz for downloading musl toolchain
 RUN apt-get update && apt-get install -y \
     curl xz-utils \
     cmake make perl golang-go git \
     clang libclang-dev \
     && rm -rf /var/lib/apt/lists/*

 # Download musl cross-compilation toolchain from GitHub (auto-redirects to latest release)
 # Source: https://github.com/cross-tools/musl-cross
 RUN curl -sSfL https://github.com/cross-tools/musl-cross/releases/latest/download/x86_64-unknown-linux-musl.tar.xz \
     | tar -xJf - -C /opt
 ENV PATH="/opt/x86_64-unknown-linux-musl/bin:$PATH"

 # Add musl target
 RUN rustup target add x86_64-unknown-linux-musl

 # Configure compilers for musl target
 ENV CC_x86_64_unknown_linux_musl=x86_64-unknown-linux-musl-gcc
 ENV CXX_x86_64_unknown_linux_musl=x86_64-unknown-linux-musl-g++
 ENV AR_x86_64_unknown_linux_musl=x86_64-unknown-linux-musl-ar
 ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=x86_64-unknown-linux-musl-gcc

-RUN cargo install cargo-chef --locked
 WORKDIR /app

-# Stage 2: Planner - Generate dependency recipe
-FROM chef AS planner
-COPY Cargo.toml Cargo.lock ./
-COPY src ./src
-RUN cargo chef prepare --recipe-path recipe.json
-
-# Stage 3: Builder - Build dependencies then application
-FROM chef AS builder
-COPY --from=planner /app/recipe.json recipe.json
-# Build dependencies (cached as long as recipe.json unchanged)
-RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json
-# Build application and test binaries
+# Stage 2: Builder - Build application and test binaries
+FROM toolchain AS builder
 COPY Cargo.toml Cargo.lock ./
 COPY src ./src
 COPY tests ./tests
-RUN cargo build --release --target x86_64-unknown-linux-musl --bin h3llo && \
+# Cache mounts persist cargo registry/git and compilation artifacts across builds.
+# Target dir is a cache mount so artifacts are NOT stored in image layers —
+# we must copy them to /app/out/ within this same RUN instruction.
+RUN --mount=type=cache,target=/usr/local/cargo/registry \
+    --mount=type=cache,target=/usr/local/cargo/git/db \
+    --mount=type=cache,target=/app/target,sharing=locked \
+    set -e && \
+    cargo build --release --target x86_64-unknown-linux-musl --bin h3llo && \
     cargo test --test integration-container-tun --release --target x86_64-unknown-linux-musl --no-run && \
     cargo test --test integration-container-route --release --target x86_64-unknown-linux-musl --no-run && \
-    strip /app/target/x86_64-unknown-linux-musl/release/h3llo
-# Copy test binaries to known location (they have hash suffixes in deps/)
-RUN mkdir -p /app/test-binaries && \
-    find /app/target/x86_64-unknown-linux-musl/release/deps -name 'integration_container_tun-*' -type f ! -name '*.d' -exec cp {} /app/test-binaries/integration-container-tun \; && \
-    find /app/target/x86_64-unknown-linux-musl/release/deps -name 'integration_container_route-*' -type f ! -name '*.d' -exec cp {} /app/test-binaries/integration-container-route \; && \
-    strip /app/test-binaries/*
+    mkdir -p /app/out && \
+    cp /app/target/x86_64-unknown-linux-musl/release/h3llo /app/out/ && \
+    strip /app/out/h3llo && \
+    find /app/target/x86_64-unknown-linux-musl/release/deps -name 'integration_container_tun-*' -type f ! -name '*.d' -exec cp {} /app/out/integration-container-tun \; && \
+    find /app/target/x86_64-unknown-linux-musl/release/deps -name 'integration_container_route-*' -type f ! -name '*.d' -exec cp {} /app/out/integration-container-route \; && \
+    strip /app/out/*

-# Stage 4: Runtime - Minimal Alpine production image
+# Stage 3: Runtime - Minimal Alpine production image
 FROM alpine:3.21 AS runtime
 RUN apk add --no-cache ca-certificates
-COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/h3llo /usr/local/bin/h3llo
+COPY --from=builder /app/out/h3llo /usr/local/bin/h3llo
 RUN mkdir -p /etc/h3llo
 WORKDIR /etc/h3llo
 ENTRYPOINT ["/usr/local/bin/h3llo"]
 CMD ["-c", "/etc/h3llo/config.yaml"]

-# Stage 5: Test - Adds diagnostic tools for integration tests
+# Stage 3a: Test - Adds diagnostic tools for integration tests
 FROM runtime AS test
 RUN apk add --no-cache iproute2 iputils-ping iperf3
 # Include pre-built test binaries for Container Test Pattern
-COPY --from=builder /app/test-binaries/integration-container-tun /usr/local/bin/
-COPY --from=builder /app/test-binaries/integration-container-route /usr/local/bin/
+COPY --from=builder /app/out/integration-container-tun /usr/local/bin/
+COPY --from=builder /app/out/integration-container-route /usr/local/bin/

Step 2: Update CI workflow — Add keep-state: true, preserve existing comments

  • File: .github/workflows/ci.yml
  • Changes: Add with: keep-state: true to setup-buildx-action. Keep the commented-out # cache-from: type=gha / # cache-to: type=gha,mode=max lines as-is per user request.
Code Draft
       - name: Set up Docker Buildx
         uses: docker/setup-buildx-action@v3
+        with:
+          keep-state: true
       - name: Build Docker test image
         uses: docker/build-push-action@v6
         with:
           context: .
           push: false
           tags: h3llo:test
           target: test
           # cache-from: type=gha
           # cache-to: type=gha,mode=max
           load: true

Step 3: Update docs/test.md — Require docker buildx build and add rebuild guidance

  • File: docs/test.md
  • Changes: Replace 3 occurrences of docker build with docker buildx build --load, add buildx prerequisite, add rebuild requirement note, update Container Test Pattern description at line 167
Code Draft

Prerequisites section (lines 70-76):

 ### Prerequisites

 1. Docker daemon running
-2. Build the test image:
+2. Docker Buildx installed (`docker buildx version` to verify)
+3. Build the test image:
    ```bash
-   docker build --target test -t h3llo:test .
+   docker buildx build --target test -t h3llo:test --load .
    ```
+   **Rebuild required**: The test image embeds compiled h3llo and test binaries.
+   You must rebuild after any change to `src/`, `tests/integration/container/`,
+   `Cargo.toml`, or `Cargo.lock`.

TUN section (line 198):

 ```bash
 # Build test image with embedded binaries
-docker build --target test -t h3llo:test .
+docker buildx build --target test -t h3llo:test --load .

 # Run TUN integration tests
 cargo test --test integration -- tun --ignored --nocapture
 ```

Route section (line 219):

 ```bash
 # Build test image with embedded binaries
-docker build --target test -t h3llo:test .
+docker buildx build --target test -t h3llo:test --load .

 # Run route integration tests
 cargo test --test integration -- route --ignored --nocapture
 ```

Container Test Pattern description (line 167):

-3. Test binaries are built into the Docker `test` image during `docker build`
+3. Test binaries are built into the Docker `test` image during `docker buildx build`

Step 4: Update tests/e2e/common.rs — Error message

  • File: tests/e2e/common.rs
  • Changes: Update build instruction in require_image_and_network() error output (line 28)
Code Draft
         eprintln!(
-            "  docker build --target test -t {}:{} .",
+            "  docker buildx build --target test -t {}:{} --load .",
             TEST_IMAGE, TEST_TAG
         );

Step 5: Update tests/integration/native/tun.rs — Rustdoc and panic message

  • File: tests/integration/native/tun.rs
  • Changes: Update rustdoc (line 10) and panic message (lines 32-33)
Code Draft
 //! Run with:
 //! ```bash
-//! docker build --target test -t h3llo:test .  # build image with embedded binaries
+//! docker buildx build --target test -t h3llo:test --load .  # build image with embedded binaries
 //! cargo test --test integration -- tun --ignored --nocapture
 //! ```
         panic!(
             "Docker image {TEST_IMAGE}:{TEST_TAG} not found. Build with:\n  \
-             docker build --target test -t {TEST_IMAGE}:{TEST_TAG} ."
+             docker buildx build --target test -t {TEST_IMAGE}:{TEST_TAG} --load ."
         );

Step 6: Update tests/integration/native/route.rs — Rustdoc and panic message

  • File: tests/integration/native/route.rs
  • Changes: Update rustdoc (line 10) and panic message (lines 32-33)
Code Draft
 //! Run with:
 //! ```bash
-//! docker build --target test -t h3llo:test .  # build image with embedded binaries
+//! docker buildx build --target test -t h3llo:test --load .  # build image with embedded binaries
 //! cargo test --test integration -- route --ignored --nocapture
 //! ```
         panic!(
             "Docker image {TEST_IMAGE}:{TEST_TAG} not found. Build with:\n  \
-             docker build --target test -t {TEST_IMAGE}:{TEST_TAG} ."
+             docker buildx build --target test -t {TEST_IMAGE}:{TEST_TAG} --load ."
         );

Success Criteria

  • docker buildx build --target test -t h3llo:test --load . builds successfully
  • docker buildx build --target runtime -t h3llo:latest --load . builds successfully
  • Second build is faster than first (cache mounts effective)
  • cargo test --test integration --features test-utils -- --ignored --nocapture passes
  • cargo test --test e2e -- --ignored --nocapture passes
  • CI docker-tests job succeeds with cache hit on second run
  • All docker build references in docs and source are updated to docker buildx build
  • Commented-out GHA cache lines in ci.yml are preserved

Risks and Mitigations

Risk Likelihood Impact Mitigation
Cache mount contents invisible to COPY --from High (certain) High (broken build) Copy artifacts to /app/out/ within same RUN
Cold build regression without cargo-chef (433 crates + boring-sys) Medium Medium (~5 min slower on cache miss) Self-hosted runner rarely loses BuildKit state; keep-state: true preserves volumes across runs
keep-state bug in future setup-buildx-action versions Low Medium Pin action version or add type=local fallback later
sharing=locked serializes concurrent builds on target dir Low Low CI concurrency group already prevents parallel runs
# syntax=docker/dockerfile:1 pulls frontend image on first use Low Low Docker 23+ supports cache mounts natively; syntax directive is for consistency
BuildKit cache corruption Low Low (graceful: full rebuild) docker builder prune --all resets

Validation

Compatibility check: All selections are compatible.

  • Disagreement 1 (1B: remove cargo-chef) affects only Dockerfile
  • Disagreement 2 (2B with comment preservation) affects only .github/workflows/ci.yml
  • Disagreement 3 (3A: update all references) affects only docs/test.md and test source files
  • No conflicting file modifications between any selections

User constraint applied: The commented-out # cache-from: type=gha / # cache-to: type=gha,mode=max lines in ci.yml (lines 51-52) are preserved per user instruction "但不要删掉我的注释".

Selection History

Timestamp Disagreement Options Summary Selected Option User Comments
2026-02-07 21:07 1: Retain or Remove cargo-chef 1A (Bold): Keep chef + add mounts; 1B (Paranoia): Delete chef; 1C (Reducer): Minimal mount-only 1B (Paranoia) —
2026-02-07 21:07 2: CI Cache Strategy 2A (Bold+Paranoia): type=local + rotation; 2B (Code Reducer): keep-state: true only; 2C: Both 2B (Code Reducer) 但不要删掉我的注释
2026-02-07 21:07 3: Source Code Error Message Updates 3A (Bold): Update all 7 references; 3B (Paranoia+Reducer): Docs only 3A (Bold) —

Refine History

Timestamp Summary
— —

Sources:

  • docker/setup-buildx-action - GitHub
  • keep-state bug fix - Issue #428
  • Docker Docs - BuildKit configuration for CI

Activity

  1. Tonny-Gu
    added
    agentize:planIssues with implementation plans
    on Feb 6, 2026
  2. Tonny-Gu
    added 17 commits that reference this issue on Feb 7, 2026

    [#270][docs] Require docker buildx build in test documentation

    ad23ced

    [#270][feat] Add BuildKit cache mounts, remove cargo-chef, enable CI …

    24796ac

    [#270][fix] Guard test binary extraction against stale cache artifacts

    e31791d

    [#270][fix] Cache full cargo git directory including checkouts

    9dda1b9

    [#270][fix] Keep cargo-chef alongside cache mounts (Option 1A)

    076751f

    [#270][docs] Require docker buildx build in test documentation

    2469178

    [#270][feat] Add BuildKit cache mounts, remove cargo-chef, enable CI …

    d842f10

    [#270][fix] Guard test binary extraction against stale cache artifacts

    2814adb

    [#270][fix] Cache full cargo git directory including checkouts

    1eb89e2

    [#270][fix] Keep cargo-chef alongside cache mounts (Option 1A)

    0d001c3

    [#270][docs] Require docker buildx build in test documentation

    8f7d1ba

    [#270][feat] Add BuildKit cache mounts, remove cargo-chef, enable CI …

    13cf2a9

    [#270][fix] Guard test binary extraction against stale cache artifacts

    48744d8

    [#270][fix] Cache full cargo git directory including checkouts

    4615a6e

    [#270][fix] Keep cargo-chef alongside cache mounts (Option 1A)

    e1e567d

    [#270][fix] Use --message-format=json for test binary path extraction

    af222df

    Merge pull request #277 from Tonny-Gu/issue-270

    783303b
  3. Tonny-Gu
    closed this as completedin #277on Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agentize:planIssues with implementation plans

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.