You are reading documentation for 0.4.1. The latest release in the 0.4 line is 0.4.2.

The latest stable release is 0.8.4.

Distributed Multi-Job Builds

How to use the distributed worker/aggregator mode to cache builds that run as multiple parallel jobs.

Why distributed mode exists

When multiple build jobs run in parallel and each publishes an independent full-cache generation, those generations diverge. Restoring only the newest one loses dependency downloads and other cache changes that exist solely in the other workers’ generations.

Distributed mode solves this with delta exchange:

  • Each worker job uploads assumed changed files and path-only activity observations for aggregate partitions, plus complete snapshots for any single-writer partitions it owns. It does not write the base cache directly.
  • An aggregator job, which waits for all workers to complete, downloads every delta, merges them, and saves the merged result as the new base cache entry.

Observed parallel changes in aggregate partitions are combined. Single-writer partitions use only their designated writer’s complete snapshot. On the next run, all workers start from a cache that reflects the combined output of every job from the previous run.

sequenceDiagram
    participant BC as Base cache
    participant WA as Worker A
    participant WB as Worker B
    participant AG as Aggregator

    BC-->>WA: restore
    BC-->>WB: restore
    WA->>WA: build + compute delta
    WB->>WB: build + compute delta
    WA-->>AG: upload Δ-A artifact
    WB-->>AG: upload Δ-B artifact
    AG->>AG: download Δ-A + Δ-B
    AG->>AG: merge deltas
    AG->>BC: save merged cache entry

How delta exchange works

  1. Worker prepare restores the base and records a cutoff after its own reads/writes.
  2. The build runs normally.
  3. Worker finalize scans metadata before its own content reads and uploads selected upserts/usage. Empty observation sets still produce completion envelopes. Workers never save the base.
  4. Aggregate prepare validates/applies selected envelopes and retains upstream activity before C. Used-only paths do not recreate absent files.
  5. Aggregate finalize performs eligible GC and requires successful publication for upserts, GC removal or a nonempty miss. Usage alone does not require a generation. Base reuse follows observed metadata and may miss byte rewrites with identical metadata. Aggregate partitions exchange no deletion tombstones. A single-writer snapshot replaces its complete configured membership, including removals, and metadata-only changes independently require publication.

On pull-request events, read-only mode performs no exchange: workers upload nothing, and an aggregator returns skipped-read-only without listing, downloading, validating, applying, or deleting artifacts. This makes a static topology safe, but a workflow should avoid allocating the aggregator runner when it can express the writable-event condition directly.

Gradle metadata writer

Gradle’s dependency-metadata partition is enabled by default and uses single-writer publication. Choose one existing worker as its metadata writer and pass the same partition configuration to every worker and the aggregator. The writer is identified by Mammoth’s producer job identity, which defaults to the workflow job ID. If you set github-job-name for a matrix or reusable workflow, use that exact identity for writer and dependent-jobs too. Matrix producers must have distinct identities; a writer designates one producer, not the entire matrix.

Only that job’s complete configured metadata set replaces the previous snapshot. Other workers' artifact downloads still aggregate normally; their local dependency-metadata updates do not. Choose a writer whose build resolves the dependencies you want covered. Metadata learned only by other jobs may need resolution again later. A validated snapshot qualifies even if its build failed.

An aggregator owns the complete snapshot when no writer is configured or when it names itself. In either case its own post-build metadata wins. If another job is named, that job’s snapshot or the prepared fallback remains authoritative despite the aggregator’s local changes.

Complete workflow with an existing worker as writer

The following workflow is also available in the repository as examples/gradle-distributed.yml. Adapt the module tasks and replace <commit-sha> with the Mammoth version you deploy.

 1name: Gradle distributed cache
 2on: [push, pull_request, workflow_dispatch]
 3
 4permissions:
 5  contents: read
 6
 7env:
 8  MAMMOTH_CACHE_PARTITIONS: '[{"id":"dependency-metadata","writer":"worker-a"}]'
 9
10jobs:
11  worker-a:
12    runs-on: ubuntu-latest
13    steps:
14      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd
15      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
16        with:
17          distribution: temurin
18          java-version: '21'
19      - uses: buildish-tooling/buildish-mammoth-cache/actions/github/gradle@<commit-sha>
20        with:
21          job-mode: distributed-worker
22          cache-partitions: ${{ env.MAMMOTH_CACHE_PARTITIONS }}
23      - run: ./gradlew :module-a:build
24
25  worker-b:
26    runs-on: ubuntu-latest
27    steps:
28      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd
29      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
30        with:
31          distribution: temurin
32          java-version: '21'
33      - uses: buildish-tooling/buildish-mammoth-cache/actions/github/gradle@<commit-sha>
34        with:
35          job-mode: distributed-worker
36          cache-partitions: ${{ env.MAMMOTH_CACHE_PARTITIONS }}
37      - run: ./gradlew :module-b:build
38
39  aggregator:
40    needs: [worker-a, worker-b]
41    if: ${{ always() && github.event_name != 'pull_request' && github.event_name != 'pull_request_target' }}
42    runs-on: ubuntu-latest
43    steps:
44      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd
45      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
46        with:
47          distribution: temurin
48          java-version: '21'
49      - uses: buildish-tooling/buildish-mammoth-cache/actions/github/gradle@<commit-sha>
50        with:
51          job-mode: distributed-aggregator
52          cache-partitions: ${{ env.MAMMOTH_CACHE_PARTITIONS }}
53          dependent-jobs: worker-a, worker-b

This chooses worker-a without an extra job. Because both workers are also listed in dependent-jobs, both ordinary completion envelopes remain required. needs ensures their post actions finish before aggregate prepare performs artifact discovery.

Optional metadata writer that can be skipped

A writer reference is optional snapshot discovery. To permit a metadata-only job to be skipped, include it in needs for ordering, but omit it from dependent-jobs. The ordinary build worker remains required. This example is available as examples/gradle-optional-metadata-writer.yml. It runs a second build to populate the writer’s metadata; use an existing suitable worker when that extra work is unnecessary.

 1name: Gradle cache with optional metadata writer
 2on:
 3  push:
 4  pull_request:
 5  workflow_dispatch:
 6    inputs:
 7      run_metadata:
 8        description: Run the metadata writer
 9        type: boolean
10        default: true
11
12permissions:
13  contents: read
14
15env:
16  MAMMOTH_CACHE_PARTITIONS: '[{"id":"dependency-metadata","writer":"metadata"}]'
17
18jobs:
19  build:
20    runs-on: ubuntu-latest
21    steps:
22      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd
23      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
24        with:
25          distribution: temurin
26          java-version: '21'
27      - uses: buildish-tooling/buildish-mammoth-cache/actions/github/gradle@<commit-sha>
28        with:
29          job-mode: distributed-worker
30          cache-partitions: ${{ env.MAMMOTH_CACHE_PARTITIONS }}
31      - run: ./gradlew build
32
33  metadata:
34    if: ${{ github.event_name != 'pull_request' && github.event_name != 'pull_request_target' && (github.event_name != 'workflow_dispatch' || inputs.run_metadata) }}
35    runs-on: ubuntu-latest
36    steps:
37      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd
38      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
39        with:
40          distribution: temurin
41          java-version: '21'
42      - uses: buildish-tooling/buildish-mammoth-cache/actions/github/gradle@<commit-sha>
43        with:
44          job-mode: distributed-worker
45          cache-partitions: ${{ env.MAMMOTH_CACHE_PARTITIONS }}
46      - run: ./gradlew build
47
48  optional-aggregator:
49    needs: [build, metadata]
50    if: ${{ always() && github.event_name != 'pull_request' && github.event_name != 'pull_request_target' }}
51    runs-on: ubuntu-latest
52    steps:
53      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd
54      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
55        with:
56          distribution: temurin
57          java-version: '21'
58      - uses: buildish-tooling/buildish-mammoth-cache/actions/github/gradle@<commit-sha>
59        with:
60          job-mode: distributed-aggregator
61          cache-partitions: ${{ env.MAMMOTH_CACHE_PARTITIONS }}
62          dependent-jobs: build

On a manual run with run_metadata: false, the aggregator still runs and imports the required build envelope. The missing metadata contribution leaves the previous snapshot unchanged and produces a warning and GitHub summary entry. If no previous snapshot exists, metadata remains absent. If the metadata job does run, only its metadata snapshot is imported; its ordinary artifact updates are ignored because it is not in dependent-jobs.

If that job is also required for ordinary cached files, list it in dependent-jobs; skipping it will then fail aggregation as a missing required worker. A malformed, ambiguous, corrupt, or incompatible writer contribution fails validation rather than silently falling back. An explicit empty snapshot is valid and clears the configured partition; it differs from an absent snapshot.

Defaults and operational behavior

With no writer configured, distributed mode preserves the previous metadata, warns, and explains how to select a writer in the GitHub summary. Configure the same writer everywhere: writer and publication policy affect the cache family, so changing them starts a cold cache. Existing cache families are cold misses after this protocol update too.

The metadata partition must start without managed files. A miss leaves it empty at restore time. Excluded files remain. Explicit include/exclude overrides are allowed, and their effect on Gradle’s internal metadata relationships is the user’s responsibility.

Authoritative snapshot bytes may be held independently of a job’s mutable Gradle data. An assigned writer uploads its complete managed snapshot. Standalone jobs normally publish their own observed snapshot. Snapshot partitions skip Mammoth’s per-file GC. Artifact GC continues independently, so metadata can refer to evicted artifacts and offline completeness is not guaranteed. See Cache partitions and Gradle cache internals.

Maven workflow example

 1jobs:
 2  worker-a:
 3    runs-on: ubuntu-latest
 4    permissions:
 5      contents: read
 6    steps:
 7      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd
 8      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
 9        with:
10          distribution: temurin
11          java-version: '21'
12      - uses: buildish-tooling/buildish-mammoth-cache/actions/github/maven@<commit-sha>
13        with:
14          job-mode: distributed-worker
15      - run: mvn -pl module-a verify
16
17  worker-b:
18    runs-on: ubuntu-latest
19    permissions:
20      contents: read
21    steps:
22      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd
23      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
24        with:
25          distribution: temurin
26          java-version: '21'
27      - uses: buildish-tooling/buildish-mammoth-cache/actions/github/maven@<commit-sha>
28        with:
29          job-mode: distributed-worker
30      - run: mvn -pl module-b verify
31
32  aggregator:
33    needs: [worker-a, worker-b]
34    if: ${{ always() && github.event_name != 'pull_request' && github.event_name != 'pull_request_target' }}
35    runs-on: ubuntu-latest
36    permissions:
37      contents: read
38    steps:
39      - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd
40      - uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654
41        with:
42          distribution: temurin
43          java-version: '21'
44      - uses: buildish-tooling/buildish-mammoth-cache/actions/github/maven@<commit-sha>
45        with:
46          job-mode: distributed-aggregator
47          dependent-jobs: worker-a, worker-b

Requirements and constraints

Upgrade all workers, snapshot writers, aggregators and cache readers together when changing incompatible Action versions. The native v2 and worker TAR-v2 profiles use a new family, so older base objects produce cold misses. Start each worker and aggregator with an empty managed cache area and run Mammoth before other restores or build-tool invocations. Keep the roots exclusive through prepare, build and post. Read-only jobs restore base files under the same admission rules. See restore admission failures.

Job names must be unique within the workflow run. Delta artifacts are identified by job name. Two worker jobs with the same name would produce the same artifact name and the aggregator would only see one of them.

The aggregator must declare needs for every consumed worker and optional metadata writer. This ensures all deltas are uploaded before the aggregator starts and consumes them during prepare.

Choose whether aggregation should run after worker failures. Use if: always() together with trusted event/ref conditions when you want to consume valid envelopes from failed builds or report missing envelopes. A skipped worker or a missing post upload, including after cancellation, leaves a required envelope absent. Pull-request aggregators may be omitted entirely; if they do run, their read-only result is the explicit no-op described above.

Workers do not need the aggregator in their needs. Workers are independent of each other; only the aggregator depends on all workers.

Re-runs reuse retained worker envelopes. For each worker, the aggregator selects the highest available producer attempt that is not newer than its own attempt. A full rerun uses every new worker envelope; a failed-job rerun safely mixes rerun workers with retained earlier attempts; and an aggregator-only rerun reuses retained worker envelopes. Selection never crosses workflow run IDs.

Mammoth retains worker envelopes after failed and successful aggregation, including when no new cache generation is needed. Workers request seven days of artifact retention by default. Set worker-artifact-retention-days on either action to request a shorter period, for example one day to reduce artifact residence:

1- uses: buildish-tooling/buildish-mammoth-cache/actions/github/gradle@<commit-sha>
2  with:
3    job-mode: distributed-worker
4    worker-artifact-retention-days: 1

Shorter retention narrows the selective or aggregator-only retry window and can require rerunning expired workers. Base cache expiry and timestamp GC are independent. Retry availability ends when the backend expires or removes the artifacts, the workflow run is deleted, or a user or administrator removes them. Repository, organization, and backend limits can shorten the requested period; availability for the full period is not guaranteed. Retention uses temporary artifact storage until removal. Normal completion retires known operation-local staging. Failed or unconfirmed worker uploads can leave an immutable package at a reported path.

If a required envelope is missing or expired, aggregation fails with the worker name even if a base cache was restored. Rerun the affected worker and then the aggregator within the same workflow run. Use fresh dedicated roots for the affected invocations. Retained remote artifacts remain eligible for selection under the workflow/run and cache family rules. Mammoth does not resume local staging or attempt to salvage interrupted uploads.

A successful writable worker always uploads one envelope. This includes workers that changed no managed cache files: their explicit empty envelope is proof of participation and is distinct from a missing or failed worker.

Sharing across Java versions, operating systems and architectures

Gradle and Maven workers can share with an aggregator using another Java version, OS or architecture when their family inputs match. Platform information remains in producer diagnostics. Same-path different contents still conflict under strict policies, including inconsistent Maven directories. Equal ordinary worker files can coalesce across permission differences. Native base permissions depend on the restoring host and SDK. Windows cannot reconstruct Unix execute bits that were lost.

Use a workflow input such as cache-key-prefix: project-${{ runner.os }}-${{ runner.arch }}- to isolate platforms, or include ${{ matrix.java }} for explicit Java separation. All jobs in a sharing group must resolve the same prefix. Gradle task inputs must account for platform-sensitive outputs; Maven platform artifacts and environment-dependent local installs need distinct coordinates/classifiers or explicit namespace isolation.

Using a config file

Repeat inputs can be moved to a shared config file. The file format is identical for both build tools — use the config keys matching your tool’s action.

1# .github/buildish-mammoth-gradle.yml  (Gradle example)
2cache-key-prefix: my-project-gradle-
3wrapper-properties-files: gradle/wrapper/gradle-wrapper.properties
4cache-partitions:
5  - id: dependency-metadata
6    writer: worker-a
 1# Each worker job
 2- uses: buildish-tooling/buildish-mammoth-cache/actions/github/gradle@<commit-sha>
 3  with:
 4    job-mode: distributed-worker
 5    config-file: .github/buildish-mammoth-gradle.yml
 6
 7# Aggregator job
 8- uses: buildish-tooling/buildish-mammoth-cache/actions/github/gradle@<commit-sha>
 9  with:
10    job-mode: distributed-aggregator
11    dependent-jobs: worker-a, worker-b
12    config-file: .github/buildish-mammoth-gradle.yml

Gradle dependency layer

Set dependency-cache-layer: true on all cooperating Gradle jobs. A writable distributed worker can then consume an invocation-owned read-only dependency seed while keeping misses in its ordinary writable user home. Standalone and aggregator jobs publish the same retained family; they do not activate a seed. The designated dependency-metadata writer also uses an ordinary writable installation. This is an opt-in configuration of the existing Action, with no new launcher.

1- uses: buildish-tooling/buildish-mammoth-cache/actions/github/gradle@<commit-sha>
2  id: mammoth
3  with:
4    job-mode: distributed-worker
5    dependency-cache-layer: true
6    wrapper-properties-files: gradle/wrapper/gradle-wrapper.properties
7- run: ./gradlew build

Use a selected Gradle wrapper that supports the read-only dependency cache. Mammoth restores modules-2/files-2.1 and the restored modules-2/metadata-* directories into the seed. Gradle chooses its own metadata format and writes misses to the writable cache. The seed output reports that the layer is available. Offline builds still need usable metadata and artifacts for their dependencies.

Activation requires both restored dependency partitions in the supported layouts, writable worker mode, restore-cleanup-mode: none, and no selected single-writer partition owned by that worker. Missing partitions, unsupported layouts, jobs without a selected wrapper, read-only jobs, and other roles use the complete ordinary installation before any seed roots are detached.

The default exports GRADLE_RO_DEP_CACHE for subsequent steps and emits the same absolute path as steps.mammoth.outputs.gradle-ro-dep-cache. With export-layer-environment: false, set that variable yourself for every consumer. An existing nonempty value conflicts with eligible prepare. Keep all consumers inside the prepare/post lifetime; do not save the path for another job. Post settles registered Gradle processes, captures only writable state, then retires the owned seed. Once advertisement is attempted, even a failed or interrupted prepare retains the path until its owning post, covering later always() or continue-on-error consumers. Incomplete activation runs cleanup only and publishes no worker envelope. Uncertain ownership or quiescence retains reported diagnostic state rather than deleting a potentially live or replaced path.

Inherited dependency bytes still download and extract once into the seed. They avoid writable capture and worker-delta upload; new dependencies use the writable head. Publishers retain marked aggregate dependency partitions instead of timestamp eviction in this family. This can grow backend storage; seed sweeps and capacity eviction are deferred. Metadata retains its complete snapshot and designated-writer semantics. The family and protocol transition starts cold, including ordinary Maven generations; Maven layering is not exposed.