Repository navigation
Build the shared Rust base image flyingrobots/rust-dev #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6237f9e
Build the shared Rust base image flyingrobots/rust-dev
flyingrobots fe3d3cd
Address review findings on the base image
flyingrobots 20a0afb
Scope a shellcheck SC2016 exception in smoke-image
flyingrobots 25b2dfe
Create HOME for runs and keep leftover containers for inspection
flyingrobots 18fa29d
Keep the copy out of reach of planted cargo config and bound the buil…
flyingrobots 93bc316
Remove only containers this run created and scope the toolchain claim
flyingrobots 32eb731
Declare the upstream base image as a retained build input
flyingrobots f23bbe7
Keep repository-chosen code out of the fetch phase
flyingrobots 5295c22
Enforce timeouts by killing the docker client, not by SIGALRM
flyingrobots File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| image: | ||
| name: Build and smoke-test rust-dev | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 45 | ||
| steps: | ||
| - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 | ||
| - name: Shellcheck | ||
| run: shellcheck scripts/* tests/smoke-image | ||
| - name: Build flyingrobots/rust-dev:1.96.0 | ||
| run: scripts/build-image 1.96.0 | ||
| - name: Smoke test | ||
| run: tests/smoke-image 1.96.0 | ||
| - name: Unchanged rebuild reuses the cache (same image ID) | ||
| run: | | ||
| first="$(docker image inspect -f '{{.Id}}' flyingrobots/rust-dev:1.96.0)" | ||
| scripts/build-image 1.96.0 | ||
| second="$(docker image inspect -f '{{.Id}}' flyingrobots/rust-dev:1.96.0)" | ||
| echo "first=$first second=$second" | ||
| test "$first" = "$second" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # Shared Rust development image for the flyingrobots repositories. | ||
| # Rules: docs/hygiene.md. Build with scripts/build-image, which passes the digest from base-digests. | ||
|
|
||
| # Fallback only. scripts/build-image always passes BASE_IMAGE from images/rust-dev/base-digests; | ||
| # keep this line equal to the 1.96.0 entry there. | ||
| ARG BASE_IMAGE=rust@sha256:58fe97504a0e4cbba5d85599619a589923d3e779472a6fb0840d58d1c4ba99d7 | ||
| FROM ${BASE_IMAGE} | ||
|
|
||
| ARG RUST_VERSION=1.96.0 | ||
| # Published checksums: https://github.com/EmbarkStudios/cargo-deny/releases/download/<version>/ | ||
| # cargo-deny-<version>-<triple>.tar.gz.sha256 | ||
| ARG CARGO_DENY_VERSION=0.20.2 | ||
| ARG CARGO_DENY_SHA256_AARCH64=995c82be0defc7a025cae49a2aa2644ce8245c9a3318fc4103907c6a285e8c7d | ||
| ARG CARGO_DENY_SHA256_X86_64=9f12ed4c49936e09b48bf862b595cde2fe64fcbd9d74dfacac6131ca824c8d5f | ||
| # Set by BuildKit (the default builder). An empty value fails the build below. | ||
| ARG TARGETARCH | ||
|
|
||
| LABEL org.flyingrobots.owner="devcontainers" \ | ||
| org.flyingrobots.role="base-image" \ | ||
| org.flyingrobots.rust-version="${RUST_VERSION}" \ | ||
| org.opencontainers.image.source="https://github.com/flyingrobots/devcontainers" | ||
|
|
||
| # The pinned base must carry the requested toolchain. | ||
| RUN test "$(rustc --version | cut -d' ' -f2)" = "${RUST_VERSION}" | ||
|
|
||
| # Known gap (docs/hygiene.md section 4): these components come from the rustup server at build | ||
| # time. rustup checks each one against the channel manifest for this exact version; the manifest | ||
| # itself is not pinned in this repository. | ||
| RUN rustup component add clippy rustfmt \ | ||
| && rustup target add wasm32-unknown-unknown | ||
|
|
||
| RUN set -eu; \ | ||
| case "${TARGETARCH}" in \ | ||
| arm64) triple=aarch64-unknown-linux-musl; sum="${CARGO_DENY_SHA256_AARCH64}" ;; \ | ||
| amd64) triple=x86_64-unknown-linux-musl; sum="${CARGO_DENY_SHA256_X86_64}" ;; \ | ||
| *) echo "unsupported or empty TARGETARCH '${TARGETARCH}' (build with BuildKit)" >&2; exit 1 ;; \ | ||
| esac; \ | ||
| name="cargo-deny-${CARGO_DENY_VERSION}-${triple}"; \ | ||
| curl -fsSL -o /tmp/deny.tgz "https://github.com/EmbarkStudios/cargo-deny/releases/download/${CARGO_DENY_VERSION}/${name}.tar.gz"; \ | ||
| echo "${sum} /tmp/deny.tgz" | sha256sum -c -; \ | ||
| tar -xzf /tmp/deny.tgz -C /tmp; \ | ||
| install -m 0755 "/tmp/${name}/cargo-deny" /usr/local/bin/cargo-deny; \ | ||
| rm -rf /tmp/deny.tgz "/tmp/${name}" | ||
|
|
||
| # Unprivileged user (fixed UID per docs/hygiene.md section 2) and the mount points for the named | ||
| # volumes (section 5). New named volumes copy the ownership of these directories, so UID 10001 can | ||
| # write to them. The copy lives at /work, the root of its own volume: its only parent directory is | ||
| # the root filesystem, so no writable directory above the copy can hold a .cargo/config.toml that | ||
| # cargo would read. The work volume itself persists, so the runner must empty it before each | ||
| # copy-in (docs/hygiene.md section 2). CARGO_HOME stays at the image path /usr/local/cargo, with only its registry and | ||
| # git caches as volumes. CARGO_HOME and the toolchain are read-only only because every run uses | ||
| # --read-only: the base image leaves those directories world-writable. | ||
| RUN groupadd --gid 10001 dev \ | ||
| && useradd --uid 10001 --gid 10001 --no-create-home --home-dir /tmp/home --shell /usr/sbin/nologin dev \ | ||
| && mkdir -p /work /scratch /target /usr/local/cargo/registry /usr/local/cargo/git /tmp/home \ | ||
| && chown 10001:10001 /work /scratch /target /usr/local/cargo/registry /usr/local/cargo/git /tmp/home | ||
|
|
||
| # HOME is /tmp/home. The image creates it; a run that mounts a fresh tmpfs on /tmp must create it | ||
| # again (devrun and tests/smoke-image do). | ||
| # No RUSTUP_TOOLCHAIN: a repository's rust-toolchain.toml channel must match this image. Otherwise | ||
| # rustup tries to install the other toolchain, which fails on the read-only root. This catches an | ||
| # honest mismatch only: a repository can still run its own compiler (a path toolchain, build.rustc). | ||
| # docs/hygiene.md section 2. CARGO_INCREMENTAL=0 keeps audit | ||
| # builds free of incremental state. GIT_CONFIG_NOSYSTEM=1 ignores any system git config. | ||
| ENV CARGO_TARGET_DIR=/target \ | ||
| CARGO_INCREMENTAL=0 \ | ||
| HOME=/tmp/home \ | ||
| GIT_CONFIG_NOSYSTEM=1 | ||
|
|
||
| USER 10001:10001 | ||
| WORKDIR /work |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Rust version, then the pinned rust image index digest (multi-arch). | ||
| # Update a line only on purpose, with the new digest from `docker buildx imagetools inspect rust:<version>`. | ||
| 1.96.0 rust@sha256:58fe97504a0e4cbba5d85599619a589923d3e779472a6fb0840d58d1c4ba99d7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #!/usr/bin/env bash | ||
| # Build flyingrobots/rust-dev:<rust-version> from the digest pinned in images/rust-dev/base-digests. | ||
| # On the workstation it runs under the git-locks keys host/heavy-work and host/docker/devcontainers-dev-run/ | ||
| # (docs/hygiene.md section 10), with a 30-minute timeout inside a 40-minute lease (section 7), and it | ||
| # refuses to start below 50 GiB free on the host or in the Docker VM (section 8). Time budget inside | ||
| # the lease: base pull 300 s + free-space check 60 s + build 1800 s = 2160 s, below the 2400 s lease. | ||
| set -euo pipefail | ||
| version="${1:?usage: scripts/build-image <rust-version>}" | ||
| root="$(cd "$(dirname "$0")/.." && pwd)" | ||
| base="$(awk -v v="$version" '$1 == v { print $2 }' "$root/images/rust-dev/base-digests")" | ||
| if [[ -z "$base" ]]; then | ||
| echo "build-image: no pinned digest for Rust $version in images/rust-dev/base-digests" >&2 | ||
| exit 2 | ||
| fi | ||
| tag="flyingrobots/rust-dev:$version" | ||
| build=(docker build | ||
| --file "$root/images/rust-dev/Dockerfile" | ||
| --build-arg "BASE_IMAGE=$base" | ||
| --build-arg "RUST_VERSION=$version" | ||
| --label "org.flyingrobots.base-digest=$base" | ||
| # No provenance or SBOM attestations: they carry a per-build timestamp, so an unchanged rebuild | ||
| # would get a new image ID (issue #2 acceptance criterion: same ID on an unchanged rebuild). | ||
| --provenance=false --sbom=false | ||
| --tag "$tag" | ||
| "$root/images/rust-dev") | ||
| timeout_s=1800 | ||
| limits=(--pids-limit 32 --memory 128m --memory-swap 128m --cpus 0.5 --ulimit nofile=256:256) | ||
|
|
||
| # Only a docker-driver builder loads the result into the local image store under its tag (section 4). | ||
| driver="$(docker buildx inspect | awk '$1 == "Driver:" { print $2; exit }')" | ||
| if [[ "$driver" != "docker" ]]; then | ||
| echo "build-image: the selected buildx builder uses driver '${driver:-unknown}'; select a docker-driver builder first" >&2 | ||
| exit 4 | ||
| fi | ||
|
|
||
| # GitHub Actions runners are single-use machines with no shared git-locks store. Keep the timeout. | ||
| if [[ "${GITHUB_ACTIONS:-}" == "true" ]]; then | ||
| exec timeout "$timeout_s" "${build[@]}" | ||
| fi | ||
|
|
||
| if [[ -z "${DEVCONTAINERS_LOCK_HELD:-}" ]]; then | ||
| # Default store: the shared workstation store on this macOS machine. | ||
| exec env GIT_LOCKS_STORE="${GIT_LOCKS_STORE:-/Users/Shared/git-locks/workstation.git}" DEVCONTAINERS_LOCK_HELD=1 \ | ||
| git locks with --job "devcontainers-build-$(uuidgen)" --holder "${USER:-agent}-build-image" \ | ||
| --ttl 2400 --wait 600 --note "build $tag" host/heavy-work host/docker/devcontainers-dev-run/ -- "$0" "$@" | ||
| fi | ||
|
|
||
| # perl is the timeout tool, because macOS has no coreutils timeout by default. | ||
| # deadline SECONDS COMMAND...: run COMMAND; after SECONDS, kill it with SIGKILL and return 124. A | ||
| # plain alarm is not enough: docker run and docker start -a forward SIGALRM to the container instead | ||
| # of exiting, so the client would keep waiting. Killing the client does not stop a container. | ||
| deadline() { | ||
| perl -e '$t = shift; $p = fork; die "fork: $!" unless defined $p; | ||
| if (!$p) { exec @ARGV or exit 127 } | ||
| $SIG{ALRM} = sub { kill "KILL", $p; waitpid $p, 0; exit 124 }; | ||
| alarm $t; waitpid $p, 0; exit($? & 127 ? 128 + ($? & 127) : $? >> 8)' "$@" | ||
| } | ||
|
|
||
| if ! docker image inspect "$base" >/dev/null 2>&1; then | ||
| echo "build-image: pulling $base" >&2 | ||
| deadline 300 docker pull --quiet "$base" >/dev/null | ||
| fi | ||
| # The free-space container's / is on the image store's filesystem only for overlay storage. Refuse | ||
| # other storage, such as a stargz snapshotter, rather than measure the wrong filesystem. | ||
| storage="$(docker info -f '{{.Driver}}')" | ||
| case "$storage" in | ||
| overlay2|overlayfs) ;; | ||
| *) echo "build-image: cannot check free space for Docker storage driver '$storage'; use overlay2 or overlayfs" >&2; exit 3 ;; | ||
| esac | ||
| min_kib=$((50 * 1024 * 1024)) | ||
| host_free="$(df -Pk "$root" | awk 'NR == 2 { print $4 }')" | ||
| run_name=devcontainers-dev-run | ||
| run_id="build-$(uuidgen)" | ||
| # A leftover from an interrupted run stays for inspection; this script never removes it. A failed | ||
| # listing stops the script (set -e), so an error never reads as "no leftover". | ||
| leftover="$(docker ps -a -q --filter "name=^${run_name}\$")" | ||
| if [[ -n "$leftover" ]]; then | ||
| echo "build-image: leftover container $run_name exists (interrupted run?); inspect it, then: docker rm -f $run_name" >&2 | ||
| exit 3 | ||
| fi | ||
| # The deadline stops only the docker client, so on failure remove the container this run started. It | ||
| # carries this run's own label, so a container that someone else created under the name stays. | ||
| vm_out="$(deadline 60 docker run --rm --pull never --name "$run_name" --network none \ | ||
| --label org.flyingrobots.owner=devcontainers --label org.flyingrobots.role=dev-run \ | ||
| --label "org.flyingrobots.run=$run_id" \ | ||
| --read-only --cap-drop ALL --security-opt no-new-privileges --user 10001:10001 "${limits[@]}" \ | ||
| --entrypoint df "$base" -Pk /)" || { | ||
| mine="$(docker ps -a -q --filter "label=org.flyingrobots.run=$run_id" || true)" | ||
| [[ -z "$mine" ]] || docker rm -f "$mine" >/dev/null 2>&1 || true | ||
| echo "build-image: could not measure Docker VM free space (failed or timed out)" >&2 | ||
| exit 3 | ||
| } | ||
| vm_free="$(awk 'NR == 2 { print $4 }' <<<"$vm_out")" | ||
| [[ "$vm_free" =~ ^[0-9]+$ ]] || { echo "build-image: unexpected df output: $vm_out" >&2; exit 3; } | ||
| if (( host_free < min_kib || vm_free < min_kib )); then | ||
| echo "build-image: refusing to start: less than 50 GiB free (host ${host_free} KiB, Docker VM ${vm_free} KiB)" >&2 | ||
| exit 3 | ||
| fi | ||
|
|
||
| deadline "$timeout_s" "${build[@]}" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.