Code Monkey home page Code Monkey logo

gradle-gcs-build-cache's Introduction

Gradle - GCSBuildCache

This Gradle plugin provides a build cache implementation that uses Google Cloud Storage to store build artifacts.

Options

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)

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)

Usage

There are multiple ways to use the Google Cloud Storage based build cache inside your projects.

As plugin in settings.gradle

settings.gradle
buildscript {
    repositories {
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
    }

    dependencies {
        classpath 'net.idlestate:gradle-gcs-build-cache:1.2.2'
    }
}

apply plugin: 'net.idlestate.gradle-gcs-build-cache'

buildCache {
    local {
        enabled = false
    }

    remote( GCSBuildCache.class ) {
        credentials = 'my-key.json' // (optional)
        bucket = 'my-bucket'
        refreshAfterSeconds = 86400 // 24h (optional)
        writeThreshold = 8 * 1024 * 1024 // 8 MiB
        enabled = true
        push = true
    }
}

Manual registration in settings.gradle

settings.gradle
buildscript {
    repositories {
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
    }

    dependencies {
        classpath 'net.idlestate:gradle-gcs-build-cache:1.2.2'
    }
}

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'
        refreshAfterSeconds = 86400 // 24h (optional)
        writeThreshold = 8 * 1024 * 1024 // 8 MiB
        enabled = true
        push = true
    }
}

Using an init script

init-build-cache.gradle
initscript {
    repositories {
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
    }

    dependencies {
        classpath 'net.idlestate:gradle-gcs-build-cache:1.2.2'
    }
}

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'
            refreshAfterSeconds = 86400 // 24h (optional)
            writeThreshold = 8 * 1024 * 1024 // 8 MiB
            enabled = true
            push = true
        }
    }
}
% ./gradlew --build-cache --init-script init-build-cache.gradle <task>

Preparation in Google Cloud Storage

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.

rules.json
{
  "rule":
  [
{
  "action": {"type": "Delete"},
  "condition": {"age": 14}
}
  ]
}
% gsutil lifecycle set rules.json gs://$BUCKET
[...]

License

gradle-gcs-build-cache's People

Contributors

kengotoda avatar nhoughto avatar tehlers avatar trevjonez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.