Programming
VERIFIED FIXES

CI Gradle build fails with 'duplicate classes' intermittently in Docker-in-Docker runners due to shared Gradle cache corruption

Environment

Environment

Self-hosted GitHub Actions runner using Docker-in-Docker executor, OpenJDK 11, Gradle 7.5, builds run in containers spawned from a shared runner image; host mounts /home/gradle/.gradle as a persistent volume across jobs.

Symptoms

Symptoms

Random CI builds fail with Java compile/link errors reporting duplicate classes (same FQCN found in multiple jars) during Gradle compileKotlin/Java. Failures are non-deterministic and often succeed after re-run.

Observations

Observations

Cleaning project (gradle clean) sometimes doesn't help. Deleting the runner's shared ~/.gradle cache directory and rerunning fixes builds for several job executions. Multiple concurrent jobs share the same host-mounted Gradle cache volume; logs show simultaneous downloads and cache writes before a failure.

Already attempted

Already attempted

Clean build, removed ~/.gradle cache on host (fixes temporarily), switched to ephemeral container cache (works but increases network downloads), tried locking cache via external script (partial improvement).

Question

Question

What's causing intermittent duplicate-class build failures in CI when using a shared Gradle cache across concurrent Docker-in-Docker jobs, and how can I fix it reliably without removing cache persistence entirely?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
nordicbyte Answer
user
Posted 1mo ago • 0 confirmations

The intermittent 'duplicate classes' in D-in-D CI almost always means your shared Gradle cache is being written concurrently by multiple builds and getting corrupted. Make each job use an isolated cache: set GRADLE_USER_HOME=$GITHUB_WORKSPACE/.gradle (or another per-job path) and disable the daemon in CI (org.gradle.daemon=false or pass -Dorg.gradle.daemon=false). If you still want network caching, use the CI's cache action keyed per workflow/job rather than a shared host volume. Verify by running parallel jobs; if errors vanish you're good. Rollback: revert GRADLE_USER_HOME and daemon settings.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Concurrent Gradle cache writes from multiple containerized jobs on the same host volume corrupt cache entries, leading to duplicate artifact contents exposed to the build 95%
Gradle daemon left running between jobs causing classpath caching inconsistencies when combined with shared volumes 40%
Incorrect CI image layering that mixes different dependency versions into the same cache path 20%

Summary

A shared Gradle cache that is writable by concurrent, isolated container processes can become corrupted or partially-populated when multiple jobs write the same cache entries concurrently. This can lead to inconsistent/duplicate artifacts being seen by the build tooling, manifesting as duplicate classes. Temporary fixes (delete cache) work because they remove corrupted entries. Proper fixes are to avoid concurrent writers to the same cache, provide per-job caches, or configure Gradle to run without the daemon and use stable, serialized cache mechanisms.

Suggested Fixes

Diagnostic step 1
Immediate safe fix: configure each CI job to use an isolated Gradle user home by setting GRADLE_USER_HOME to a workspace-local path (e.g. $GITHUB_WORKSPACE/.gradle) to avoid shared concurrent writes.
Diagnostic step 2
Disable the Gradle daemon in CI builds: add org.gradle.daemon=false to gradle.properties in CI environment or pass -Dorg.gradle.daemon=false so no persistent daemons will race.
Diagnostic step 3
If you need caching, use the CI cache mechanism keyed per-branch/job (actions/cache or your runner cache) or serialize access to the shared cache with a cache lock plugin.

Continue troubleshooting

Matched from the title, description and tags