Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 (`<domain>/...`) and reversed to reverse-DNS for label keys (`com.staffbase.deploy.*`) | `deploy.staffbase.com` |
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
Expand Down
29 changes: 26 additions & 3 deletions scripts/resolve-build-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=""
Expand Down
49 changes: 49 additions & 0 deletions tests/resolve-build-config.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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'"
}
Loading