From 50e8eaa62ae99051669062bd678884358db88f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Schl=C3=B6ffel?= Date: Tue, 9 Jun 2026 10:47:17 +0200 Subject: [PATCH] Cache Rust build artifacts for Alpine packages --- .gitea/workflows/build.yml | 5 +++ README.md | 7 ++++ packaging/alpine/local/greptimedb/APKBUILD | 9 +++-- scripts/apk/Dockerfile | 8 +++- scripts/apk/build.sh | 45 ++++++++++++++++++++++ 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 6a472c7..fddb785 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -25,10 +25,15 @@ jobs: PACKAGE_USER: "${{ secrets.PACKAGE_USER }}" PACKAGE_TOKEN: "${{ secrets.PACKAGE_TOKEN }}" PACKAGER: "Joachim Schlöffel " + CARGO_HOME: "/cache/public-alpine-packages/greptimedb/cargo" + RUSTUP_HOME: "/cache/public-alpine-packages/greptimedb/rustup" + CARGO_TARGET_DIR: "/cache/public-alpine-packages/greptimedb/target" steps: - name: Prepare Environment run: | apk add --no-cache --update abuild-rootbld alpine-sdk atools-apkbuild-lint bash ca-certificates curl doas git nodejs sudo tar + mkdir -p "$CARGO_HOME" "$RUSTUP_HOME" "$CARGO_TARGET_DIR" + chown 1000:1000 /cache /cache/public-alpine-packages /cache/public-alpine-packages/greptimedb "$CARGO_HOME" "$RUSTUP_HOME" "$CARGO_TARGET_DIR" - name: Checkout uses: actions/checkout@v3 diff --git a/README.md b/README.md index 9f69881..6f86bcd 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,13 @@ ALPINE_PACKAGE=greptimedb mise run apk:build ALPINE_PACKAGE=greptimedb SKIP_BUILD=1 mise run apk:test-install ``` +Docker-backed build tasks keep Cargo registry, Rustup, and Cargo target caches in named Docker volumes. The default volume scope is derived from the Git remote owner/repository and is split by package and architecture. Override it when needed: + +```sh +ALPINE_APK_CACHE_SCOPE=public-alpine-packages ALPINE_PACKAGE=greptimedb mise run apk:build +ALPINE_APK_CACHE_PREFIX=alpine-apk-ci ALPINE_PACKAGE=greptimedb mise run apk:build +``` + Use the test shell to inspect the package in Alpine with the current build installed: diff --git a/packaging/alpine/local/greptimedb/APKBUILD b/packaging/alpine/local/greptimedb/APKBUILD index c622e68..d79ac34 100644 --- a/packaging/alpine/local/greptimedb/APKBUILD +++ b/packaging/alpine/local/greptimedb/APKBUILD @@ -54,8 +54,9 @@ source=" _rust_toolchain="nightly-2026-03-21" _cargo_profile="nightly" -export CARGO_HOME="$srcdir/cargo" -export RUSTUP_HOME="$srcdir/rustup" +export CARGO_HOME="${CARGO_HOME:-$srcdir/cargo}" +export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$builddir/target}" +export RUSTUP_HOME="${RUSTUP_HOME:-$srcdir/rustup}" export CARGO_BUILD_JOBS="${CARGO_BUILD_JOBS:-2}" export CARGO_PROFILE_NIGHTLY_CODEGEN_UNITS="${CARGO_PROFILE_NIGHTLY_CODEGEN_UNITS:-16}" export CARGO_PROFILE_NIGHTLY_DEBUG="${CARGO_PROFILE_NIGHTLY_DEBUG:-false}" @@ -80,11 +81,11 @@ build() { } check() { - "$builddir"/target/"$_cargo_profile"/greptime --version + "$CARGO_TARGET_DIR"/"$_cargo_profile"/greptime --version } package() { - install -Dm755 "$builddir"/target/"$_cargo_profile"/greptime \ + install -Dm755 "$CARGO_TARGET_DIR"/"$_cargo_profile"/greptime \ "$pkgdir"/usr/bin/greptime local _config for _config in datanode flownode frontend metasrv standalone; do diff --git a/scripts/apk/Dockerfile b/scripts/apk/Dockerfile index b36772b..1c58b7b 100644 --- a/scripts/apk/Dockerfile +++ b/scripts/apk/Dockerfile @@ -18,9 +18,15 @@ RUN addgroup -g "${BUILDER_GID}" builder \ && adduser -D -u "${BUILDER_UID}" -G builder builder \ && addgroup builder abuild \ && addgroup builder wheel \ - && mkdir -p /var/cache/distfiles /home/builder/packages \ + && mkdir -p \ + /var/cache/distfiles \ + /home/builder/.cache/cargo \ + /home/builder/.cache/cargo-target \ + /home/builder/.cache/rustup \ + /home/builder/packages \ && chgrp abuild /var/cache/distfiles /home/builder/packages \ && chmod g+w /var/cache/distfiles /home/builder/packages \ + && chown -R builder:builder /home/builder/.cache \ && printf 'permit nopass :wheel\n' > /etc/doas.d/wheel.conf \ && printf '%%wheel ALL=(ALL) NOPASSWD: ALL\n' > /etc/sudoers.d/wheel diff --git a/scripts/apk/build.sh b/scripts/apk/build.sh index 39b3851..9b0d061 100755 --- a/scripts/apk/build.sh +++ b/scripts/apk/build.sh @@ -19,6 +19,39 @@ if [[ "${builder_gid}" == "0" ]]; then builder_gid=1000 fi +cache_scope_from_remote() { + local remote + local path + local owner + local repo + + remote="$(git -C "${repo_root}" remote get-url origin 2>/dev/null || true)" + remote="${remote%.git}" + + case "${remote}" in + ssh://*/*/*) + path="${remote#ssh://*/}" + ;; + http://*/*/*|https://*/*/*) + path="${remote#*://*/}" + ;; + *:*) + path="${remote#*:}" + ;; + *) + path="$(basename "${repo_root}")" + ;; + esac + + owner="$(basename "$(dirname "${path}")")" + repo="$(basename "${path}")" + if [[ -n "${owner}" && "${owner}" != "." && -n "${repo}" ]]; then + printf '%s-%s\n' "${owner}" "${repo}" | sed 's/[^A-Za-z0-9_.-]/-/g' + else + printf '%s\n' "$(basename "${repo_root}")" | sed 's/[^A-Za-z0-9_.-]/-/g' + fi +} + validate_arch() { case "$1" in x86_64|aarch64) ;; @@ -32,11 +65,17 @@ run_for_arch() { local package_dir="$3" local image_name local container_package_dir + local package_name + local cache_scope + local cache_prefix validate_arch "${arch}" apk_validate_package_dir "${package_dir}" + package_name="$(apk_package_name "${package_dir}")" container_package_dir="$(apk_container_package_dir "${repo_root}" "${package_dir}")" image_name="${ALPINE_APK_BUILDER_IMAGE:-alpine-apk-builder:${arch}}" + cache_scope="${ALPINE_APK_CACHE_SCOPE:-$(cache_scope_from_remote)}" + cache_prefix="${ALPINE_APK_CACHE_PREFIX:-alpine-apk}-${cache_scope}-${package_name}-${arch}" docker build \ --platform "${build_platform}" \ @@ -57,7 +96,13 @@ run_for_arch() { -e "CARCH=${arch}" \ -e "ALPINE_REPO_NAME=${repo_name}" \ -e "APKBUILD_DIR=${container_package_dir}" \ + -e "CARGO_HOME=/home/builder/.cache/cargo" \ + -e "CARGO_TARGET_DIR=/home/builder/.cache/cargo-target" \ -e "PACKAGER=${PACKAGER:-Joachim Schlöffel }" \ + -e "RUSTUP_HOME=/home/builder/.cache/rustup" \ + -v "${cache_prefix}-cargo:/home/builder/.cache/cargo" \ + -v "${cache_prefix}-cargo-target:/home/builder/.cache/cargo-target" \ + -v "${cache_prefix}-rustup:/home/builder/.cache/rustup" \ -v "${repo_root}:/work" \ -v "${repo_root}/.cache/abuild:/home/builder/.abuild" \ -v "${repo_root}/.cache/apk-distfiles:/var/cache/distfiles" \