Github Action setup-gradle Saving Configuration-Cache State

In case you missed it, Gradle configuration caching is no longer enabled by default in certain scenarios. Here’s what you need to know to ensure your builds leverage this feature effectively.

Important Note: To avoid potentially leaking secrets in the configuration-cache entry, the action will only save or restore configuration-cache data if the cache-encryption-key parameter is set.

What is Gradle Configuration Cache?

Gradle configuration cache is typically enabled using either:

  • The CLI parameter: gradle --configuration-cache
  • The gradle.properties file: org.gradle.configuration-cache=true

Configuration caching can significantly reduce build times by reusing the results of the configuration phase between builds. However, recent changes require additional steps to ensure its functionality in environments like GitHub Actions.

The Issue with Default Caching in GitHub Actions

While optimizing my CI/CD pipeline for faster builds, I came across the following log message, which indicates that gradle’s configuration cache state is not saved because no encryption key was provided:

“Not saving configuration-cache state, as no encryption key was provided.”

github action gradle log
Caching Gradle state Not saving configuration-cache state, as no encryption key was provided

Minimal Example: What Not to Do

Below is a minimal example of a GitHub Actions configuration. While functional, it lacks proper setup for Gradle configuration caching.

The Solution: Enable Secure Configuration Caching

To resolve this issue, follow these steps to Generate a Valid Encryption Key:

Create a secure encryption key to protect your Gradle configuration cache:

Example output:

Save this key as a GitHub Actions secret:

    • Navigate to Repository Settings > Secrets and variables > Actions.
    • Add a new secret with the name GRADLE_CACHE_ENCRYPTION_KEY and paste the generated key.

Avoid Improper Caching Configurations

These methods are less secure and conflict with configuration caching best practices.

Use the Correct Configuration

Here’s the updated configuration:

Results: Faster Builds with Secure Caching

By implementing the above configuration, my build times improved significantly—from 13 minutes to 5,5 minutes.

Bonus: Dependency Graph Insights

While setting up your pipeline, you can also enable the dependency-graph feature. This will generate and submit a dependency graph to your GitHub project view, providing valuable insights into your project’s dependencies.

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Spelling error report

The following text will be sent to our editors: