Skip to content

Commit

Permalink
Add gradle caching function (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
idelvall authored Dec 11, 2023
1 parent fbf4293 commit b4d24ae
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
21 changes: 21 additions & 0 deletions gradle/Earthfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
VERSION --global-cache 0.7


# GRADLE_GET_MOUNT_CACHE sets the following entries in the calling environment:
# EARTHLY_GRADLE_USER_HOME_CACHE: Code of the mount cache for the gradle home.
# EARTHLY_GRADLE_PROJECT_CACHE: Code of the mount cache for the .gradle folder.
# Example:
# DO gradle+GRADLE_GET_MOUNT_CACHE
# RUN --mount=$EARTHLY_GRADLE_USER_HOME_CACHE --mount=$EARTHLY_GRADLE_PROJECT_CACHE gradle --no-daemon build
# Arguments:
# - cache_prefix: To be used in both caches. By default: ${EARTHLY_TARGET_PROJECT_NO_TAG}#${OS_RELEASE}#earthly-gradle-cache
GRADLE_GET_MOUNT_CACHE:
COMMAND
ARG EARTHLY_TARGET_PROJECT_NO_TAG
ARG cache_prefix = "${EARTHLY_TARGET_PROJECT_NO_TAG}#earthly-gradle-cache"
IF [ "$GRADLE_USER_HOME" = "" ]
ENV GRADLE_USER_HOME="$HOME/.gradle"
END
ENV EARTHLY_GRADLE_CACHE_PREFIX=$cache_prefix
ENV EARTHLY_GRADLE_USER_HOME_CACHE="type=cache,mode=0777,id=$cache_prefix,sharing=shared,target=$GRADLE_USER_HOME"
ENV EARTHLY_GRADLE_PROJECT_CACHE="type=cache,mode=0777,id=${cache_prefix}#project,sharing=shared,target=.gradle"
33 changes: 33 additions & 0 deletions gradle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# lib/gradle

Earthly's official collection of Gradle [functions](https://docs.earthly.dev/docs/guides/functions).

First, import the library up in your Earthfile:
```earthfile
VERSION --global-cache 0.7
IMPORT github.com/earthly/lib/gradle:<version/commit> AS gradle
```
> :warning: Due to [this issue](https://github.com/earthly/earthly/issues/3490), make sure to enable `--global-cache` in the calling Earthfile, as shown above.
## +GRADLE_GET_MOUNT_CACHE

This function sets the following entries in the calling environment, so they can be used later to parametrize two global mount caches in `RUN` commands:
- `$EARTHLY_GRADLE_USER_HOME_CACHE`: Code of the mount cache for the [gradle user home](https://docs.gradle.org/current/userguide/directory_layout.html#dir:gradle_user_home) directory.
- `$EARTHLY_GRADLE_PROJECT_CACHE`: Code of the mount cache for the [gradle project root](https://docs.gradle.org/current/userguide/directory_layout.html#dir:project_root) directory.

### Arguments
- `cache_prefix`: To be used in both caches. By default: `${EARTHLY_TARGET_PROJECT_NO_TAG}#${OS_RELEASE}#earthly-gradle-cache`

### Example
```earthly
DO gradle+GRADLE_GET_MOUNT_CACHE
RUN --mount=$EARTHLY_GRADLE_USER_HOME_CACHE --mount=$EARTHLY_GRADLE_PROJECT_CACHE gradle --no-daemon build
```

### Scoping

If `cache_prefix` is not set, the two global caches have an Earthfile-scope, that is, they are not shared with other Earthfiles.
Otherwise, all Earthfiles using the same `cache_prefix` will share the cache mounts.

## Example
See [earthly-intellij-plugin](https://github.com/earthly/earthly-intellij-plugin/blob/main/Earthfile) for a complete example.

0 comments on commit b4d24ae

Please sign in to comment.