From 568bc59e13ef415b2ffae9688b99915ccd37e303 Mon Sep 17 00:00:00 2001 From: Natalie Bridgers Date: Mon, 28 Sep 2026 15:51:28 -0500 Subject: [PATCH] Enable sccache for GitHub Actions builds --- .ci/dockerfile-hash.yaml | 2 +- .github/workflows/build.yaml | 20 +++++++++++++++++++- .github/workflows/docker.yaml | 15 +++++++++++++-- .github/workflows/golangci-lint.yaml | 14 +++++++++++++- Makefile | 5 +++-- docker/build.Dockerfile | 7 +++++++ util/linux-arm64-sccache.ini | 3 +++ util/windows-amd64-sccache.ini | 3 +++ 8 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 util/linux-arm64-sccache.ini create mode 100644 util/windows-amd64-sccache.ini diff --git a/.ci/dockerfile-hash.yaml b/.ci/dockerfile-hash.yaml index 5e27d0dcb..7436c12d3 100644 --- a/.ci/dockerfile-hash.yaml +++ b/.ci/dockerfile-hash.yaml @@ -1,2 +1,2 @@ variables: - DOCKERFILE_HASH: 28cc8e519521a047f1da9084773cbf08b0428ee7 \ No newline at end of file + DOCKERFILE_HASH: 98a5c34fead8eced780eca85fa561a039ccbaa59 \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 8dd1122ea..f43edddd7 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -14,6 +14,8 @@ jobs: linux: name: ${{ matrix.name }} runs-on: ubuntu-latest + # Only trusted pushes update the shared compiler cache. + cache-mode: ${{ github.event_name == 'push' && 'write' || 'read' }} strategy: matrix: include: @@ -45,6 +47,14 @@ jobs: fetch-depth: 0 ref: ${{ github.event.pull_request.head.sha }} + - name: Export GitHub Actions cache environment + if: matrix.name == 'linux-amd64' || matrix.name == 'linux-arm64' || matrix.name == 'windows-amd64' + uses: actions/github-script@v7 + with: + script: | + core.exportVariable('ACTIONS_RESULTS_URL', process.env.ACTIONS_RESULTS_URL || ''); + core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || ''); + - name: mise check if: matrix.name == 'linux-amd64' uses: jdx/mise-action@v2 @@ -59,6 +69,8 @@ jobs: password: ${{ github.token }} - name: make node + env: + SCCACHE_GHA_RW_MODE: ${{ github.event_name == 'push' && 'READ_WRITE' || 'READ_ONLY' }} run: | sudo apt install podman -y # Ubuntu 24.04's crun (1.14.1) rejects the OCI spec version newer @@ -66,7 +78,13 @@ jobs: # and the 20260726 runner image update made that pairing fatal. sudo curl -fsSL -o /usr/bin/crun https://github.com/containers/crun/releases/download/1.28/crun-1.28-linux-amd64 sudo chmod +x /usr/bin/crun - make in-container BUILDER_TARGET=builder-no-darwin DOCKER_PWD_MOUNT_PATH=/app DOCKER_BUILD_OPTS="--layers --cache-to ghcr.io/streamplace/streamplace --cache-from ghcr.io/streamplace/streamplace" DOCKER_OPTS="-e STREAMPLACE_IGNORE_LEAKS=true -e STREAMPLACE_TEST_COUNT=1 -e CI=true -e GITHUB_ACTION=true" IN_CONTAINER_CMD="${{ matrix.cmd }}" + sccache_opts="" + sccache_cmd="${{ matrix.cmd }}" + if [[ "${{ matrix.name }}" == "linux-amd64" || "${{ matrix.name }}" == "linux-arm64" || "${{ matrix.name }}" == "windows-amd64" ]]; then + sccache_opts="-e ACTIONS_RESULTS_URL -e ACTIONS_RUNTIME_TOKEN" + sccache_cmd="export SCCACHE_GHA_ENABLED=on SCCACHE_GHA_VERSION=1 SCCACHE_GHA_RW_MODE=$SCCACHE_GHA_RW_MODE SCCACHE_BASEDIRS=/app CC='sccache gcc' CXX='sccache g++' RUSTC_WRAPPER=sccache USE_SCCACHE=1; set -e; $sccache_cmd; sccache --show-stats" + fi + make in-container BUILDER_TARGET=builder-no-darwin DOCKER_PWD_MOUNT_PATH=/app DOCKER_BUILD_OPTS="--layers --cache-to ghcr.io/streamplace/streamplace --cache-from ghcr.io/streamplace/streamplace" DOCKER_OPTS="-e STREAMPLACE_IGNORE_LEAKS=true -e STREAMPLACE_TEST_COUNT=1 -e CI=true -e GITHUB_ACTION=true $sccache_opts" IN_CONTAINER_CMD="$sccache_cmd" - name: Publish Test Report if: matrix.name == 'linux-amd64' && (success() || failure()) diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index 6c49b22e8..3368d7bc5 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -89,6 +89,8 @@ jobs: binaries: name: linux binaries runs-on: ubuntu-latest + # build.yaml is the cache writer; this duplicate build only restores. + cache-mode: read outputs: version: ${{ steps.version.outputs.version }} steps: @@ -103,6 +105,13 @@ jobs: fetch-depth: 0 ref: ${{ github.event.pull_request.head.sha }} + - name: Export GitHub Actions cache environment + uses: actions/github-script@v7 + with: + script: | + core.exportVariable('ACTIONS_RESULTS_URL', process.env.ACTIONS_RESULTS_URL || ''); + core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || ''); + - name: Log in to ghcr.io (build-layer cache) uses: docker/login-action@v3 with: @@ -111,14 +120,16 @@ jobs: password: ${{ github.token }} - name: Cross-compile linux amd64 + arm64 + env: + SCCACHE_GHA_RW_MODE: READ_ONLY run: | sudo apt install podman -y make in-container \ BUILDER_TARGET=builder-no-darwin \ DOCKER_PWD_MOUNT_PATH=/app \ DOCKER_BUILD_OPTS="--layers --cache-to ghcr.io/streamplace/streamplace --cache-from ghcr.io/streamplace/streamplace" \ - DOCKER_OPTS="-e CI=true -e GITHUB_ACTION=true" \ - IN_CONTAINER_CMD="make linux-amd64 && make linux-arm64 && go run ./pkg/config/git/git.go -v > sp-version.txt" + DOCKER_OPTS="-e CI=true -e GITHUB_ACTION=true -e ACTIONS_RESULTS_URL -e ACTIONS_RUNTIME_TOKEN" \ + IN_CONTAINER_CMD="export CC='sccache gcc' CXX='sccache g++' RUSTC_WRAPPER=sccache SCCACHE_GHA_ENABLED=on SCCACHE_GHA_VERSION=1 SCCACHE_GHA_RW_MODE=$SCCACHE_GHA_RW_MODE SCCACHE_BASEDIRS=/app USE_SCCACHE=1; set -e; make linux-amd64 && make linux-arm64 && sccache --show-stats && go run ./pkg/config/git/git.go -v > sp-version.txt" - name: Capture version id: version diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml index 7dfb71fbd..27d235553 100644 --- a/.github/workflows/golangci-lint.yaml +++ b/.github/workflows/golangci-lint.yaml @@ -8,6 +8,8 @@ jobs: golangci: name: lint runs-on: ubuntu-latest + # build.yaml is the cache writer; this duplicate build only restores. + cache-mode: read timeout-minutes: 60 steps: - name: Maximize build space @@ -21,6 +23,13 @@ jobs: fetch-tags: true ref: ${{ github.event.pull_request.head.sha }} + - name: Export GitHub Actions cache environment + uses: actions/github-script@v7 + with: + script: | + core.exportVariable('ACTIONS_RESULTS_URL', process.env.ACTIONS_RESULTS_URL || ''); + core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || ''); + - name: Log in to the Container registry uses: docker/login-action@v2 with: @@ -38,7 +47,10 @@ jobs: mkdir -p ~/.config/containers printf '[engine]\nlock_type = "file"\n' >> ~/.config/containers/containers.conf sudo ln -s $(realpath ./hack/golangci-lint) /usr/bin/golangci-lint - make golangci-lint-container BUILDER_TARGET=builder-no-darwin DOCKER_BUILD_OPTS="--layers --cache-to ghcr.io/streamplace/streamplace --cache-from ghcr.io/streamplace/streamplace" + make golangci-lint-container BUILDER_TARGET=builder-no-darwin DOCKER_BUILD_OPTS="--layers --cache-to ghcr.io/streamplace/streamplace --cache-from ghcr.io/streamplace/streamplace" DOCKER_OPTS="-e ACTIONS_RESULTS_URL -e ACTIONS_RUNTIME_TOKEN -e SCCACHE_GHA_ENABLED=on -e SCCACHE_GHA_RW_MODE=READ_ONLY -e SCCACHE_GHA_VERSION=1 -e SCCACHE_BASEDIRS=$PWD -e 'CC=sccache gcc' -e 'CXX=sccache g++' -e RUSTC_WRAPPER=sccache" + + - name: sccache statistics + run: podman exec golangci-lint sccache --show-stats - name: sanity-check wrapper from bash run: diff --git a/Makefile b/Makefile index 808a3ee2a..ca6760b11 100644 --- a/Makefile +++ b/Makefile @@ -639,7 +639,7 @@ linux-arm64: && CC=aarch64-linux-gnu-gcc \ LD=aarch64-linux-gnu-ld \ CROSS_COMPILE=1 \ - MESON_SETUP_OPTS="--cross-file util/linux-arm64-gnu.ini" \ + MESON_SETUP_OPTS="--cross-file util/linux-arm64-gnu.ini $(if $(USE_SCCACHE),--cross-file util/linux-arm64-sccache.ini)" \ BUILDDIR=build-linux-arm64 \ CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \ $(MAKE) -j $(shell nproc) archive @@ -650,7 +650,7 @@ windows-amd64: && CC=x86_64-w64-mingw32-gcc \ LD=x86_64-w64-mingw32-ld \ CROSS_COMPILE=1 \ - MESON_SETUP_OPTS="--cross-file util/windows-amd64-gnu.ini" \ + MESON_SETUP_OPTS="--cross-file util/windows-amd64-gnu.ini $(if $(USE_SCCACHE),--cross-file util/windows-amd64-sccache.ini)" \ BUILDDIR=build-windows-amd64 \ $(MAKE) -j $(shell nproc) archive @@ -770,6 +770,7 @@ dockerfile-hash-precommit: .PHONY: golangci-lint-container golangci-lint-container: docker-build-builder podman run \ + $(DOCKER_OPTS) \ -v $$(pwd):$$(pwd) \ -w $$(pwd) \ -e PKG_CONFIG_PATH=$$(pwd)/build-linux-amd64/lib/pkgconfig \ diff --git a/docker/build.Dockerfile b/docker/build.Dockerfile index 28cc8e519..98a5c34fe 100644 --- a/docker/build.Dockerfile +++ b/docker/build.Dockerfile @@ -98,6 +98,13 @@ RUN curl -L https://github.com/golangci/golangci-lint/releases/download/v${GOLAN && mv golangci-lint-${GOLANGCI_LINT_VERSION}-linux-amd64/golangci-lint /usr/local/bin/ \ && rm -rf golangci-lint.tar.gz golangci-lint-${GOLANGCI_LINT_VERSION}-linux-amd64 +ENV SCCACHE_VERSION 0.18.0 +RUN curl --fail --location https://github.com/mozilla/sccache/releases/download/v${SCCACHE_VERSION}/sccache-v${SCCACHE_VERSION}-x86_64-unknown-linux-musl.tar.gz -o sccache.tar.gz \ + && echo '45f1447fbe231e3037bde351ef70677dd212216c8d62ae7ca409fecc4d6acc89 sccache.tar.gz' | sha256sum --check - \ + && tar -xf sccache.tar.gz \ + && install -m 0755 sccache-v${SCCACHE_VERSION}-x86_64-unknown-linux-musl/sccache /usr/local/bin/sccache \ + && rm -rf sccache.tar.gz sccache-v${SCCACHE_VERSION}-x86_64-unknown-linux-musl + RUN gem install fpm ENV APTLY_VERSION 1.6.2 RUN curl --fail -L https://github.com/aptly-dev/aptly/releases/download/v${APTLY_VERSION}/aptly_${APTLY_VERSION}_linux_amd64.zip -o aptly.zip \ diff --git a/util/linux-arm64-sccache.ini b/util/linux-arm64-sccache.ini new file mode 100644 index 000000000..88a370044 --- /dev/null +++ b/util/linux-arm64-sccache.ini @@ -0,0 +1,3 @@ +[binaries] +c = ['sccache', 'aarch64-linux-gnu-gcc'] +cpp = ['sccache', 'aarch64-linux-gnu-g++'] diff --git a/util/windows-amd64-sccache.ini b/util/windows-amd64-sccache.ini new file mode 100644 index 000000000..c70590246 --- /dev/null +++ b/util/windows-amd64-sccache.ini @@ -0,0 +1,3 @@ +[binaries] +c = ['sccache', 'x86_64-w64-mingw32-gcc'] +cpp = ['sccache', 'x86_64-w64-mingw32-g++'] -- knotmirror 0.0.1