This Gradle plugin provides a build cache implementation that uses Google Cloud Storage to store build artifacts.
The build cache takes the following options:
Option | Description |
---|---|
credentials |
JSON key file of the service account to use, otherwise GCP Application Default Credentials are used. (optional) |
bucket |
Name of the Google Cloud Storage bucket (required) |
prefix |
Path prefix for objects written to Google Cloud Storage (optional) |
refreshAfterSeconds |
Amount of time to wait before the timestamp of a cached artifact that is still in use will be renewed (optional) |
writeThreshold |
Number of bytes at which the plugin starts using a file as a buffer for writing cache entries - default 8 MiB (optional) |
There are multiple ways to use the Google Cloud Storage based build cache inside your projects.
buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'net.idlestate:gradle-gcs-build-cache:1.3.0'
}
}
apply plugin: 'net.idlestate.gradle-gcs-build-cache'
buildCache {
local {
enabled = false
}
remote( GCSBuildCache.class ) {
credentials = 'my-key.json' // (optional)
bucket = 'my-bucket'
prefix = 'app-cache' // (optional)
refreshAfterSeconds = 86400 // 24h (optional)
writeThreshold = 8 * 1024 * 1024 // 8 MiB
enabled = true
push = true
}
}
buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'net.idlestate:gradle-gcs-build-cache:1.3.0'
}
}
import net.idlestate.gradle.caching.GCSBuildCache
import net.idlestate.gradle.caching.GCSBuildCacheServiceFactory
buildCache {
local {
enabled = false
}
registerBuildCacheService( GCSBuildCache.class, GCSBuildCacheServiceFactory.class )
remote( GCSBuildCache.class ) {
credentials = 'my-key.json' // (optional)
bucket = 'my-bucket'
prefix = 'app-cache' // (optional)
refreshAfterSeconds = 86400 // 24h (optional)
writeThreshold = 8 * 1024 * 1024 // 8 MiB
enabled = true
push = true
}
}
initscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'net.idlestate:gradle-gcs-build-cache:1.3.0'
}
}
import net.idlestate.gradle.caching.GCSBuildCache
import net.idlestate.gradle.caching.GCSBuildCacheServiceFactory
gradle.settingsEvaluated { settings ->
settings.buildCache {
local {
enabled = false
}
registerBuildCacheService( GCSBuildCache.class, GCSBuildCacheServiceFactory.class )
remote( GCSBuildCache.class ) {
credentials = 'my-key.json' // (optional)
bucket = 'my-bucket'
prefix = 'app-cache' // (optional)
refreshAfterSeconds = 86400 // 24h (optional)
writeThreshold = 8 * 1024 * 1024 // 8 MiB
enabled = true
push = true
}
}
}
% ./gradlew --build-cache --init-script init-build-cache.gradle <task>
The following steps will create a Google Cloud Storage bucket named $BUCKET
as part of the GCP project $PROJECT
in your $REGION
with private access. gsutil
is available as part of the Google Cloud SDK.
% gsutil mb -p $PROJECT -c regional -l $REGION gs://$BUCKET [...] % gsutil acl set private gs://$BUCKET [...]
To create a service account $ACCOUNT
with access to the bucket the following commands have to be executed.
% gcloud iam service-accounts create $ACCOUNT --display-name "Service account to access the Gradle build cache in Google Cloud Storage" [...] % gsutil acl ch -u $ACCOUNT@$PROJECT.iam.gserviceaccount.com:WRITE gs://$BUCKET [...]
A JSON key file $KEY_FILE
that could be used by the Gradle plugin to access the bucket is created with the following statement.
% gcloud iam service-accounts keys create $KEY_FILE --iam-account $ACCOUNT@$PROJECT.iam.gserviceaccount.com [...]
If cached artifacts should be deleted after two weeks lifecycle rules like the following should be applied to the bucket.
{
"rule":
[
{
"action": {"type": "Delete"},
"condition": {"age": 14}
}
]
}
% gsutil lifecycle set rules.json gs://$BUCKET [...]