Sibelius Seraphini
Sibelius Seraphini

Reputation: 5593

How do I handle Gradle cache on CircleCI 2.0?

We are trying to cache all Gradle dependencies for our Android build job.

This is the current approach that is failing:

- restore_cache:
    key: android-build-{{ checksum "android/build.gradle" }}-{{ checksum  "android/app/build.gradle" }}
- save_cache:
    paths:
      - ~/.gradle
    key: android-build-{{ checksum "android/build.gradle" }}-{{ checksum  "android/app/build.gradle" }}

Upvotes: 5

Views: 3571

Answers (2)

gavenkoa
gavenkoa

Reputation: 48713

Place build steps between restore_cache and save_cache.

If your project is multi-module/-level hash all build scripts and use it as key to proper capture dependencies:

  - run:
      name: Hash dependency info
      command: |
        mkdir -p build
        md5sum gradle.properties settings.gradle build.gradle **/build.gradle >build/deps.md5
  - restore_cache:
      key: gradle-{{ checksum "build/deps.md5" }}
  - run:
      name: Build and deploy
      command: >
        bash ./gradlew
        build artifactoryPublish
  - save_cache:
      key: gradle-{{ checksum "build/deps.md5" }}
      paths:
        - ~/.gradle/caches
        - ~/.gradle/wrapper

Upvotes: 1

Jake Lee
Jake Lee

Reputation: 7979

There's a sample Android configuration by Circle CI themselves here as well as a step-by-step walkthrough of the attributes.

version: 2
jobs:
  build:
    working_directory: ~/code
    docker:
      - image: circleci/android:api-25-alpha
    environment:
      JVM_OPTS: -Xmx3200m
    steps:
      - checkout
      - restore_cache:
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}
#      - run:
#         name: Chmod permissions #if permission for Gradlew Dependencies fail, use this. 
#         command: sudo chmod +x ./gradlew
      - run:
          name: Download Dependencies
          command: ./gradlew androidDependencies
      - save_cache:
          paths:
            - ~/.gradle
          key: jars-{{ checksum "build.gradle" }}-{{ checksum  "app/build.gradle" }}
      - run:
          name: Run Tests
          command: ./gradlew lint test
      - store_artifacts:
          path: app/build/reports
          destination: reports
      - store_test_results:
          path: app/build/test-results

It's worth noting that we experienced some issues when utilising the cache due to submodules, but the above should work for simpler repositories.

Upvotes: 2

Related Questions