We use cookies to understand how people use Depot.
Cache

Gradle

Gradle is the build tool of choice for Java, Android, and Kotlin. It is used in many large projects, including Android itself, and is optimized for incremental builds, advanced local and remote caching, and parallel execution. Gradle supports many different languages and platforms, and is highly configurable, scaling to codebases of any size.

Depot Cache provides a remote cache service that can be used with Gradle, allowing you to incrementally cache and reuse parts of your builds. This cache is accessible from anywhere, both on your local machine and on CI/CD systems.

Configuring Gradle to use Depot Cache

Depot Cache can be used with Gradle from Depot's managed GitHub Actions runners, from your local machine, from any CI/CD system, or within containerized builds using Dockerfiles or Bake files.

From Depot-managed Actions runners

Depot GitHub Actions runners are pre-configured to use Depot Cache with Gradle - each runner is launched with an init.gradle file that is pre-populated with the connection details for Depot Cache. You will need to verify that caching is enabled in your gradle.properties file.

org.gradle.caching=true

If you don't want Depot to override the init.gradle file on each runner, disable Allow Actions jobs to automatically connect to Depot Cache in your organization settings page. You can manually configure Gradle to use Depot Cache as described in the "Using Depot Cache from your local machine or any CI/CD system" section.

Using Depot Cache with Gradle in depot/build-push-action

When using depot/build-push-action to build Docker images that contain Gradle projects, your build needs access to Gradle's remote cache credentials to benefit from caching.

These credentials are not automatically available inside your Docker build environment. Unlike builds running directly on Depot-managed GitHub Actions runners (which have automatic access to Depot Cache environment variables), containerized builds execute in isolated VMs that require explicit configuration.

Follow these steps to securely pass your Gradle credentials into your Docker build:

  1. Verify that caching is enabled in your gradle.properties file:
org.gradle.caching=true
  1. Store the Depot token in a GitHub Secret named DEPOT_TOKEN.

  2. Update your settings.gradle to read the Depot token from an environment variable:

buildCache {
    remote(HttpBuildCache) {
        url = 'https://cache.depot.dev'
        enabled = true
        push = true
        credentials {
            username = ''
            password = System.getenv('DEPOT_TOKEN')
        }
    }
}
  1. Configure your GitHub Action to pass secrets to the container build:
- name: Build and push
  uses: depot/build-push-action@v1
  with:
    context: .
    file: ./Dockerfile
    push: true
    tags: your-image:tag
    secrets: |
      "DEPOT_TOKEN=${{ secrets.DEPOT_TOKEN }}"
  1. Update your Dockerfile to mount the secret and run the build:
# syntax=docker/dockerfile:1

# ... other Dockerfile instructions

# Copy Gradle configuration and run build with mounted secret
COPY gradle.properties settings.gradle ./
RUN --mount=type=secret,id=DEPOT_TOKEN,env=DEPOT_TOKEN \
    ./gradlew build

Adding # syntax=docker/dockerfile:1 as the first line of your Dockerfile enables mounting secrets as environment variables.

Using Depot Cache from your local machine or any CI/CD system

To manually configure Gradle to use Depot Cache, you will need to configure remote caching in your settings.gradle file. First, verify that caching is enabled in your gradle.properties file:

org.gradle.caching=true

Then, configure Gradle to use the Depot Cache service endpoints and set your API token as the password credential:

settings.gradle:

buildCache {
    remote(HttpBuildCache) {
        url = 'https://cache.depot.dev'
        enabled = true
        push = true
        credentials {
            username = ''
            password = 'DEPOT_TOKEN'
        }
    }
}

If you are a member of multiple organizations, and you are authenticating with a user token, you must additionally specify which organization ID to use for cache storage in the username:

buildCache {
    remote(HttpBuildCache) {
        url = 'https://cache.depot.dev'
        enabled = true
        push = true
        credentials {
            username = 'DEPOT_ORG_ID'
            password = 'DEPOT_TOKEN'
        }
    }
}

After Gradle is configured to use Depot Cache, you can then run your builds as you normally would. Gradle will automatically communicate with Depot Cache to fetch and reuse any stored build artifacts from your previous builds.

Using Depot Cache with Gradle in Depot CLI

When building directly with Depot CLI, follow these steps:

  1. Verify that caching is enabled in your gradle.properties file:
org.gradle.caching=true
  1. Update your settings.gradle to read the Depot token from an environment variable:
buildCache {
    remote(HttpBuildCache) {
        url = 'https://cache.depot.dev'
        enabled = true
        push = true
        credentials {
            username = ''
            password = System.getenv('DEPOT_TOKEN')
        }
    }
}
  1. Update your Dockerfile to copy Gradle configuration files and mount the secret:
# syntax=docker/dockerfile:1

# ... other Dockerfile instructions

# Copy Gradle configuration and run build with mounted secret
COPY gradle.properties settings.gradle ./
RUN --mount=type=secret,id=DEPOT_TOKEN,env=DEPOT_TOKEN \
    ./gradlew build

Adding # syntax=docker/dockerfile:1 as the first line of your Dockerfile enables mounting secrets as environment variables.

  1. Build with Depot CLI:
depot build --secret id=DEPOT_TOKEN,env=DEPOT_TOKEN -t your-image:tag .

Or with Docker Buildx:

docker buildx build --secret id=DEPOT_TOKEN,env=DEPOT_TOKEN -t your-image:tag .

Using Depot Cache with Gradle in Bake files

When using Bake files to build Docker images containing Gradle projects, you can pass secrets through the target.secret attribute:

  1. Verify that caching is enabled in your gradle.properties file:
org.gradle.caching=true
  1. Update your settings.gradle to read the Depot token from an environment variable:
buildCache {
    remote(HttpBuildCache) {
        url = 'https://cache.depot.dev'
        enabled = true
        push = true
        credentials {
            username = ''
            password = System.getenv('DEPOT_TOKEN')
        }
    }
}
  1. Define the secret in your docker-bake.hcl file:
target "default" {
  context    = "."
  dockerfile = "Dockerfile"
  tags       = ["your-image:tag"]
  secret = [
    {
      type = "env"
      id   = "DEPOT_TOKEN"
    }
  ]
}
  1. Update your Dockerfile to copy Gradle configuration files and mount the secret:
# syntax=docker/dockerfile:1

# ... other Dockerfile instructions

# Copy Gradle configuration and run build with mounted secret
COPY gradle.properties settings.gradle ./
RUN --mount=type=secret,id=DEPOT_TOKEN,env=DEPOT_TOKEN \
    ./gradlew build

Adding # syntax=docker/dockerfile:1 as the first line of your Dockerfile enables mounting secrets as environment variables.

  1. Run the build with depot bake:
DEPOT_TOKEN=your_token depot bake