Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ testing {
}
}
}

// b/250726951 Gradle ProjectBuilder needs reflection access to java.lang.
val jvmAddOpensArgs = listOf("--add-opens=java.base/java.lang=ALL-UNNAMED")
tasks.withType<Test>() {
this.jvmArgs(jvmAddOpensArgs)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package androidx.build.gradle.core

import org.gradle.api.provider.Provider
import java.io.File
import java.io.InputStream
import java.nio.file.Files
Expand All @@ -26,7 +27,7 @@ import java.nio.file.Files
*/
class FileSystemStorageService(
override val bucketName: String,
override val isPush: Boolean,
override val isPush: Provider<Boolean>,
override val isEnabled: Boolean
) : StorageService {

Expand All @@ -50,7 +51,7 @@ class FileSystemStorageService(
return false
}

if (!isPush) {
if (!isPush.get()) {
return false
}

Expand All @@ -67,7 +68,7 @@ class FileSystemStorageService(
return false
}

if (!isPush) {
if (!isPush.get()) {
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,28 @@

package androidx.build.gradle.core

import org.gradle.api.provider.Property
import org.gradle.caching.configuration.AbstractBuildCache

/**
* Gradle Build Cache that uses a cloud storage provider as a backing to load and store Gradle cache results.
*/
abstract class RemoteGradleBuildCache : AbstractBuildCache() {

/**
* Runtime switch that determines whether the build attempts to upload entries
* to the remote build cache.
*
* Unlike [org.gradle.caching.configuration.BuildCache.isPush], this flag is **not**
* part of the Gradle Configuration Cache model’s fingerprint and is only used at execution
* time.
*
* Keeping `push = true` in `settings.gradle[.kts]` and gating uploads with this property allows you to
* toggle pushing without invalidating the configuration cache
*
*/
abstract val runtimePush: Property<Boolean>

/**
* The name of the bucket that is used to store all the gradle cache entries.
* This essentially becomes the root of all cache entries.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package androidx.build.gradle.core

import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import java.io.Closeable
import java.io.InputStream

Expand All @@ -29,7 +31,7 @@ interface StorageService : Closeable {
/**
* `true` if the underlying storage service supports writes and deletes.
*/
val isPush: Boolean
val isPush: Provider<Boolean>

/**
* If `true`, use the underlying storage service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@

package androidx.build.gradle.core

import org.gradle.api.model.ObjectFactory
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Test

class FileStorageServiceTest {

private val objectFactory: ObjectFactory = ProjectBuilder.builder().build().objects

@Test
fun testStoreBlob() {
val storageService = FileSystemStorageService(
bucketName = BUCKET_NAME,
isPush = true,
isPush = objectFactory.property(Boolean::class.java).convention(true),
isEnabled = true
)
storageService.use {
Expand All @@ -39,7 +44,7 @@ class FileStorageServiceTest {
fun testLoadBlob() {
val storageService = FileSystemStorageService(
bucketName = BUCKET_NAME,
isPush = true,
isPush = objectFactory.property(Boolean::class.java).convention(true),
isEnabled = true
)
storageService.use {
Expand All @@ -57,7 +62,7 @@ class FileStorageServiceTest {
fun testStoreBlob_noPushSupport() {
val storageService = FileSystemStorageService(
bucketName = BUCKET_NAME,
isPush = false,
isPush = objectFactory.property(Boolean::class.java).convention(false),
isEnabled = true
)
storageService.use {
Expand All @@ -72,7 +77,7 @@ class FileStorageServiceTest {
fun testStoreBlob_disabled() {
val storageService = FileSystemStorageService(
bucketName = BUCKET_NAME,
isPush = true,
isPush = objectFactory.property(Boolean::class.java).convention(true),
isEnabled = false
)
storageService.use {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package androidx.build.gradle.gcpbuildcache
import androidx.build.gradle.core.FileSystemStorageService
import androidx.build.gradle.core.blobKey
import org.gradle.api.logging.Logging
import org.gradle.api.provider.Provider
import org.gradle.caching.BuildCacheEntryReader
import org.gradle.caching.BuildCacheEntryWriter
import org.gradle.caching.BuildCacheKey
Expand All @@ -39,7 +40,7 @@ internal class GcpBuildCacheService(
private val bucketName: String,
gcpCredentials: GcpCredentials,
messageOnAuthenticationFailure: String,
isPush: Boolean,
isPush: Provider<Boolean>,
isEnabled: Boolean,
inTestMode: Boolean = false
) : BuildCacheService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class GcpBuildCacheServiceFactory : BuildCacheServiceFactory<GcpBuildCache> {
.type("GCP-backed")
.config("projectId", buildCache.projectId)
.config("bucketName", buildCache.bucketName)
.config("isPushSupported", "${buildCache.isPush}")
.config("isEnabled", "${buildCache.isEnabled}")
.config(
"usingExportedKeyCredentials",
Expand All @@ -44,7 +43,7 @@ class GcpBuildCacheServiceFactory : BuildCacheServiceFactory<GcpBuildCache> {
buildCache.bucketName,
buildCache.credentials,
buildCache.messageOnAuthenticationFailure,
buildCache.isPush,
buildCache.runtimePush.orElse(buildCache.isPush),
buildCache.isEnabled
)
service.validateConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.google.cloud.http.HttpTransportOptions
import com.google.cloud.storage.*
import org.gradle.api.GradleException
import org.gradle.api.logging.Logging
import org.gradle.api.provider.Provider
import java.io.InputStream

/**
Expand All @@ -37,13 +38,13 @@ internal class GcpStorageService(
override val bucketName: String,
gcpCredentials: GcpCredentials,
messageOnAuthenticationFailure: String,
override val isPush: Boolean,
override val isPush: Provider<Boolean>,
override val isEnabled: Boolean,
private val sizeThreshold: Long = BLOB_SIZE_THRESHOLD
) : StorageService {

private val storageOptions by lazy {
storageOptions(projectId, gcpCredentials, messageOnAuthenticationFailure, isPush)
storageOptions(projectId, gcpCredentials, messageOnAuthenticationFailure)
}

override fun load(cacheKey: String): InputStream? {
Expand All @@ -62,7 +63,7 @@ internal class GcpStorageService(
return false
}

if (!isPush) {
if (!isPush.get()) {
logger.info("No push support")
return false
}
Expand All @@ -77,7 +78,7 @@ internal class GcpStorageService(
return false
}

if (!isPush) {
if (!isPush.get()) {
return false
}
val blobId = BlobId.of(bucketName, cacheKey)
Expand Down Expand Up @@ -164,12 +165,10 @@ internal class GcpStorageService(
projectId: String,
gcpCredentials: GcpCredentials,
messageOnAuthenticationFailure: String,
isPushSupported: Boolean
): StorageOptions? {
val credentials = credentials(
gcpCredentials,
messageOnAuthenticationFailure,
isPushSupported
) ?: return null
val retrySettings = RetrySettings.newBuilder()
retrySettings.maxAttempts = 3
Expand Down Expand Up @@ -241,14 +240,10 @@ internal class GcpStorageService(
private fun credentials(
gcpCredentials: GcpCredentials,
messageOnAuthenticationFailure: String,
isPushSupported: Boolean
): GoogleCredentials? {
val scopes = mutableListOf(
STORAGE_READ_ONLY,
val scopes = listOf(
STORAGE_READ_ONLY, STORAGE_READ_WRITE, STORAGE_FULL_CONTROL
)
if (isPushSupported) {
scopes += listOf(STORAGE_READ_WRITE, STORAGE_FULL_CONTROL)
}
return when (gcpCredentials) {
is ApplicationDefaultGcpCredentials -> {
defaultApplicationGcpCredentials(scopes, messageOnAuthenticationFailure, forceClearCache = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package androidx.build.gradle.gcpbuildcache

import org.gradle.api.model.ObjectFactory
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Assume.assumeNotNull
import org.junit.Test
import java.io.File
Expand All @@ -27,6 +29,8 @@ import java.io.File
class GcpStorageServiceTest {
private val serviceAccountPath = System.getenv()["GRADLE_CACHE_SERVICE_ACCOUNT_PATH"]

private val objectFactory: ObjectFactory = ProjectBuilder.builder().build().objects

@Test
fun testStoreBlob() {
assumeNotNull(serviceAccountPath)
Expand All @@ -35,7 +39,7 @@ class GcpStorageServiceTest {
bucketName = BUCKET_NAME,
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath!!)),
messageOnAuthenticationFailure = "Please re-authenticate",
isPush = true,
isPush = objectFactory.property(Boolean::class.java).convention(true),
isEnabled = true,
sizeThreshold = 0L
)
Expand All @@ -56,7 +60,7 @@ class GcpStorageServiceTest {
bucketName = BUCKET_NAME,
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath!!)),
messageOnAuthenticationFailure = "Please re-authenticate",
isPush = true,
isPush = objectFactory.property(Boolean::class.java).convention(true),
isEnabled = true,
sizeThreshold = 0L
)
Expand All @@ -80,7 +84,7 @@ class GcpStorageServiceTest {
bucketName = BUCKET_NAME,
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath!!)),
messageOnAuthenticationFailure = "Please re-authenticate",
isPush = false,
isPush = objectFactory.property(Boolean::class.java).convention(false),
isEnabled = true,
sizeThreshold = 0L
)
Expand All @@ -100,7 +104,7 @@ class GcpStorageServiceTest {
bucketName = BUCKET_NAME,
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath!!)),
messageOnAuthenticationFailure = "Please re-authenticate",
isPush = true,
isPush = objectFactory.property(Boolean::class.java).convention(true),
isEnabled = true,
sizeThreshold = 0L
)
Expand All @@ -109,7 +113,7 @@ class GcpStorageServiceTest {
bucketName = BUCKET_NAME,
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath)),
messageOnAuthenticationFailure = "Please re-authenticate",
isPush = false,
isPush = objectFactory.property(Boolean::class.java).convention(false),
isEnabled = true,
sizeThreshold = 0L
)
Expand All @@ -135,7 +139,7 @@ class GcpStorageServiceTest {
bucketName = BUCKET_NAME,
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath!!)),
messageOnAuthenticationFailure = "Please re-authenticate",
isPush = true,
isPush = objectFactory.property(Boolean::class.java).convention(true),
isEnabled = false,
sizeThreshold = 0L
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package androidx.build.gradle.s3buildcache
import androidx.build.gradle.core.FileSystemStorageService
import androidx.build.gradle.core.blobKey
import org.gradle.api.logging.Logging
import org.gradle.api.provider.Provider
import org.gradle.caching.BuildCacheEntryReader
import org.gradle.caching.BuildCacheEntryWriter
import org.gradle.caching.BuildCacheKey
Expand All @@ -42,7 +43,7 @@ class S3BuildCacheService(
credentials: S3Credentials,
region: String,
bucketName: String,
isPush: Boolean,
isPush: Provider<Boolean>,
isEnabled: Boolean,
reducedRedundancy: Boolean,
inTestMode: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ class S3BuildCacheServiceFactory : BuildCacheServiceFactory<S3BuildCache> {
.config("region", buildCache.region)
.config("bucketName", buildCache.bucketName)
.config("reducedRedundancy", "${buildCache.reducedRedundancy}")
.config("isPushSupported", "${buildCache.isPush}")
.config("isEnabled", "${buildCache.isEnabled}")
.config("credentialsType", "${buildCache.credentials}")

val service = S3BuildCacheService(
region = buildCache.region,
bucketName = buildCache.bucketName,
isPush = buildCache.isPush,
isPush = buildCache.runtimePush.orElse(buildCache.isPush),
isEnabled = buildCache.isEnabled,
reducedRedundancy = buildCache.reducedRedundancy,
credentials = buildCache.credentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import androidx.build.gradle.core.FileHandleInputStream
import androidx.build.gradle.core.FileHandleInputStream.Companion.handleInputStream
import androidx.build.gradle.core.StorageService
import org.gradle.api.logging.Logging
import software.amazon.awssdk.core.exception.SdkClientException
import software.amazon.awssdk.core.exception.SdkException
import org.gradle.api.provider.Provider
import software.amazon.awssdk.core.exception.SdkServiceException
import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.services.s3.S3Client
Expand All @@ -37,7 +36,7 @@ import kotlin.io.path.outputStream

class S3StorageService(
override val bucketName: String,
override val isPush: Boolean,
override val isPush: Provider<Boolean>,
override val isEnabled: Boolean,
private val client: S3Client,
private val region: String,
Expand Down Expand Up @@ -65,7 +64,7 @@ class S3StorageService(
return false
}

if (!isPush) {
if (!isPush.get()) {
logger.info("No push support")
return false
}
Expand Down Expand Up @@ -94,7 +93,7 @@ class S3StorageService(
return false
}

if (!isPush) {
if (!isPush.get()) {
logger.info("No push support")
return false
}
Expand Down
Loading