:wave: Hello, team! I've been tasked with decreas...
# caching
l
👋 Hello, team! I've been tasked with decreasing the time taken to run our build pipelines and decided on using gradle caching via: variables: GRADLE_USER_HOME: $(Pipeline.Workspace)/.gradle steps: - task: Cache@2 inputs: key: 'gradle | "$(Agent.OS)" | **/build.gradle.kts' # Swap build.gradle.kts for build.gradle when using Groovy restoreKeys: | gradle | "$(Agent.OS)" gradle path: $(GRADLE_USER_HOME) displayName: Configure gradle caching - task: Gradle@2 inputs: gradleWrapperFile: 'gradlew' tasks: 'build' options: '--build-cache' displayName: Build - script: | # stop the Gradle daemon to ensure no files are left open (impacting the save cache operation later) ./gradlew --stop displayName: Gradlew stop The first run built the cache as expected and then the second build utilised the cache for 4 of the 7 build tasks, however, any subsequent builds then ran all of the tasks again from scratch. After a week of scratching my head and scouring Google my ADO build history had disappeared so I couldn't see the successful and unsuccessful runs. I pushed a dummy commit to instigate the building of the cache, I then pushed a 2nd dummy commit to utilise the cache (which again worked), however, the 3rd dummy commit actually utilised the cache and so did a 4th. Nothing had changed in the code so I don't understand why it suddenly started working? Because of the time elapsed and the setup of our repos, I deleted my initial branch and created it again from an up-to-date version of master. I then repeated the process and now none of my builds are utilising cache ðŸĪŠ The builds that did use cache have the same cache hit on the same artifact ID/key as the ones that didn't use the cache. Is the gradle caching always inconsistent in your experience? I will post the details of the successful and unsuccessful ADO pipeline runs in a second.
a
Hi 👋 There can be a lot of things that might cause cache misses. Before figuring out why caching isn't working on CI, did you check that caching works as expected on a local machine?
The most useful way to check build cache misses is to enable Gradle Build Scans. If you re-run the build, then it's really easy to compare the scans from both builds and you can compare and contrast.
l
Thanks Adam, I'll try enabling Gradle Build Scans.
Although in my case the builds both generated a cache hit, only one build used the cache and the other didn't. Won't Build Scans only help when there is a cache miss?
a
it's good to have a successful scan to compare against a failed scan, then we can see what's different
for example, here's a comparison of two Dokka builds. One took much longer than the other, but if you click around, it's easy to find out why :) https://ge.jetbrains.com/c/4n7i2hglpahiw/q7ehskzxkm6n2/task-inputs