diff --git a/README.md b/README.md index 401bc8b..dd7d72b 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,23 @@ with: docker-build-no-cache: 'true' ``` +#### Weekly cache rotation + +Layers like `apt-get upgrade` or `apk upgrade` are served from the GitHub Actions layer cache as long as the +base image and the `RUN` line are unchanged, so OS and browser security updates never arrive. Set +`docker-build-cache-rotation: 'weekly'` to refresh them once per ISO week without touching the Dockerfile: + +```yaml +with: + docker-build-cache-rotation: 'weekly' +``` + +The week is added to the cache scope (e.g. `buildkit-2026-W48`, `amd64-2026-W48` in multi-arch builds). The first +build of each week starts from an empty cache and re-runs every layer; later builds that week reuse the fresh +cache. Unlike `docker-build-no-cache`, the fresh cache is written back, so the next push on `main` does not read +stale layers again. It is off by default, and `docker-build-no-cache: 'true'` takes precedence. A repo that is +not built for a week still keeps its old image; trigger a scheduled build for those. + ### Multi-Arch Images Our recovery/failover regions have no ARM capacity, so images deployed there must ship both `linux/amd64` and `linux/arm64`. The action never cross-compiles or emulates: each architecture is built natively on its own runner, and the results are combined into one manifest list afterwards. @@ -281,6 +298,7 @@ Notes: | `multiarch-digests-path` | Directory holding the per-architecture digest files | `/tmp/gitops-action-digests` | | `docker-build-provenance` | Generate [provenance](https://docs.docker.com/build/attestations/slsa-provenance/) attestation for the build | `false` | | `docker-build-no-cache` | Re-execute Dockerfile steps and skip external cache import/export. Base images are always checked for updates. | `false` | +| `docker-build-cache-rotation` | Rotate the layer cache scope: `weekly` starts a fresh cache each ISO week (refreshes apt/apk layers). Empty keeps one cache. Ignored with `docker-build-no-cache: true`. | `''` | | `docker-disable-retagging` | Disables retagging of existing images and run a new build instead | `false` | | `deployment-annotations` | Stamp deployment-tracking annotations (`deploy.staffbase.com/*`) onto updated GitOps manifests. See [Deployment tracking annotations](#deployment-tracking-annotations) | `false` | | `deployment-domain` | Key namespace for deployment-tracking metadata. Used verbatim for annotation keys (`/...`) and reversed to reverse-DNS for label keys (`com.staffbase.deploy.*`) | `deploy.staffbase.com` | diff --git a/action.yml b/action.yml index 5df65a4..ec816e9 100644 --- a/action.yml +++ b/action.yml @@ -59,6 +59,10 @@ inputs: description: 'Re-execute Dockerfile steps without cached build results or external cache import/export. Base images are always checked for updates independently of this setting.' required: false default: 'false' + docker-build-cache-rotation: + description: "Rotate the GitHub Actions layer cache so OS package layers (apt/apk upgrade) are refreshed. Empty (default) keeps one cache forever; 'weekly' starts a fresh cache scope each ISO week. Ignored when docker-build-no-cache is true." + required: false + default: '' docker-build-outputs: description: "Custom output destinations (e.g., type=registry,push=true,compression=zstd,force-compression=true). When set, this replaces the default push behavior - include push=true if pushing is desired." required: false @@ -174,6 +178,7 @@ runs: INPUT_DOCKER_BUILD_PLATFORMS: ${{ inputs.docker-build-platforms }} INPUT_DOCKER_BUILD_OUTPUTS: ${{ inputs.docker-build-outputs }} INPUT_DOCKER_BUILD_NO_CACHE: ${{ inputs.docker-build-no-cache }} + INPUT_DOCKER_BUILD_CACHE_ROTATION: ${{ inputs.docker-build-cache-rotation }} INPUT_DOCKER_REGISTRY: ${{ steps.preparation.outputs.primary_registry }} INPUT_DOCKER_IMAGE: ${{ inputs.docker-image }} INPUT_TAG_LIST: ${{ steps.preparation.outputs.tag_list }} diff --git a/scripts/resolve-build-config.sh b/scripts/resolve-build-config.sh index e75db84..018f565 100755 --- a/scripts/resolve-build-config.sh +++ b/scripts/resolve-build-config.sh @@ -10,7 +10,7 @@ # Optional env vars: INPUT_MULTIARCH_MODE, INPUT_DOCKER_BUILD_PLATFORMS, # INPUT_DOCKER_BUILD_OUTPUTS, INPUT_TAG_LIST, INPUT_PUSH, # INPUT_DOCKER_REGISTRY, INPUT_DOCKER_IMAGE, -# INPUT_DOCKER_BUILD_NO_CACHE +# INPUT_DOCKER_BUILD_NO_CACHE, INPUT_DOCKER_BUILD_CACHE_ROTATION # # Outputs (via GITHUB_OUTPUT): arch, platforms, tags, build_outputs, # cache_suffix, cache_from, cache_to @@ -62,9 +62,32 @@ echo "Build platforms: ${PLATFORMS}" # CACHE_SUFFIX is appended verbatim to the type=gha cache refs. The parallel # matrix jobs must not share one cache scope, or they keep overwriting each # other's manifest. Empty outside build mode to keep the default scope. -CACHE_SUFFIX="" +# +# docker-build-cache-rotation=weekly adds the ISO week to the scope, so the +# first build of each week starts from an empty cache and re-runs layers such as +# apt/apk upgrades; later builds in that week reuse the fresh cache. Unlike +# no-cache, the refreshed cache is written back, so the next push on main does +# not read stale layers again. +CACHE_SCOPE="" if [[ "$MODE" == "build" ]]; then - CACHE_SUFFIX=",scope=${ARCH}" + CACHE_SCOPE="$ARCH" +fi + +case "${INPUT_DOCKER_BUILD_CACHE_ROTATION:-}" in + "") ;; + weekly) + WEEK="$(date -u +%G-W%V)" + CACHE_SCOPE="${CACHE_SCOPE:-buildkit}-${WEEK}" + ;; + *) + log_error "docker-build-cache-rotation must be empty or 'weekly'." + exit 1 + ;; +esac + +CACHE_SUFFIX="" +if [[ -n "$CACHE_SCOPE" ]]; then + CACHE_SUFFIX=",scope=${CACHE_SCOPE}" fi CACHE_FROM="" diff --git a/tests/resolve-build-config.bats b/tests/resolve-build-config.bats index 70dcb4b..8587978 100644 --- a/tests/resolve-build-config.bats +++ b/tests/resolve-build-config.bats @@ -14,6 +14,14 @@ setup() { export INPUT_PUSH="true" } +# Pin the ISO week by putting a fake `date` first on PATH. +stub_week() { + mkdir -p "${TEST_TEMP_DIR}/bin" + printf '#!/usr/bin/env bash\necho "%s"\n' "$1" > "${TEST_TEMP_DIR}/bin/date" + chmod +x "${TEST_TEMP_DIR}/bin/date" + export PATH="${TEST_TEMP_DIR}/bin:${PATH}" +} + teardown() { teardown_common } @@ -139,3 +147,44 @@ teardown() { assert_failure assert_output --partial "docker-build-no-cache must be 'true' or 'false'" } + +@test "weekly cache rotation adds the ISO week to the default cache scope" { + export INPUT_DOCKER_BUILD_CACHE_ROTATION="weekly" + stub_week "2026-W48" + run "$SCRIPT" + assert_success + assert_output_value "cache_suffix" ",scope=buildkit-2026-W48" + assert_output_value "cache_from" "type=gha,scope=buildkit-2026-W48" + assert_output_value "cache_to" "type=gha,mode=max,scope=buildkit-2026-W48" +} + +@test "weekly cache rotation keeps the per-architecture scope in build mode" { + export INPUT_DOCKER_BUILD_CACHE_ROTATION="weekly" + stub_week "2026-W48" + export INPUT_MULTIARCH_MODE="build" + run "$SCRIPT" + assert_success + assert_output_value "cache_from" "type=gha,scope=amd64-2026-W48" + assert_output_value "cache_to" "type=gha,mode=max,scope=amd64-2026-W48" +} + +@test "weekly cache rotation uses the current ISO week" { + export INPUT_DOCKER_BUILD_CACHE_ROTATION="weekly" + run "$SCRIPT" + assert_success + assert_output_value "cache_from" "type=gha,scope=buildkit-$(date -u +%G-W%V)" +} + +@test "no-cache wins over cache rotation and invalid rotation values fail" { + export INPUT_DOCKER_BUILD_CACHE_ROTATION="weekly" + export INPUT_DOCKER_BUILD_NO_CACHE="true" + run "$SCRIPT" + assert_success + assert_output_value "cache_from" "" + assert_output_value "cache_to" "" + + export INPUT_DOCKER_BUILD_CACHE_ROTATION="daily" + run "$SCRIPT" + assert_failure + assert_output --partial "docker-build-cache-rotation must be empty or 'weekly'" +}