4

How do I get aws codebuild to give me the same cache behavior for gradle as when I run the build locally on my machine?

If I am executing cachable tasks on my own machine, this command:

gradle clean buildFunction buildLayer

Will take 2 seconds. However on the codebuild servers this same task takes almost 3 minutes, even when I have run the build before with the same commit/code, I was expecting the same behavior to have ultrafast builds. I tried adding all sorts of directories to the cache but to no avail.

My buildspec.yml is as follows:

version: 0.1

phases:
  install:
   commands:
     - echo Entering install phase...
     - echo Nothing to do now
  build:
    commands:
     - echo Build started on `date`
     - gradle buildLayer buildFunction
cache:
  paths:
    - '.gradle/**/*'
    - 'build/**/*'
    - '/root/.gradle/caches/**/*'
    - '/root/.gradle/wrapper/**/*'
    - '/root/.m2/**/*'
artifacts:
  type: zip
  files:
    - 'build/distributions/*.zip'

Derrops
  • 7,651
  • 5
  • 30
  • 60
  • Were you ever able to resolve this? I'm running into this issue myself with 3+ minute long build times. – Matthew Tschiegg Nov 03 '21 at 18:50
  • 1
    No luck ) : I haven't touched this in a while. We've been using Azure Devops and on their agents I only needed to cache `.gradle/` for this to work. I suspect something else is invalidating the cache, maybe with debug flags could figure this out but don't know. – Derrops Nov 04 '21 at 03:16
  • I'm having the same problem. I did post an answer, but I deleted it, because it's wrong. I thought that changing the cache path from `/root/.gradle/**/*` to `/root/.gradle/` would help, and it did! My build went from 7 to 4 minutes. However that's because the cache was completely disabled. So for me, the cache is slower than just running a build. Note that this is with an S3 cache. A LOCAL cache is much faster, but it's *very* short lived - less than 10 minutes. – aSemy May 13 '22 at 11:30
  • Does this answer your question? [Caching the Gradle wrapper in AWS CodeBuild](https://stackoverflow.com/questions/50126601/caching-the-gradle-wrapper-in-aws-codebuild) basically there are no wildcard in the cache path, just use /root/.gradle and that's it – Ariel May 16 '23 at 20:36

0 Answers0