Skip to content

Commit

Permalink
feat: reuse configuration cache in case S3_BUILD_CACHE_... environmen…
Browse files Browse the repository at this point in the history
…t variables change

The following variables are accessed lazily:
* S3_BUILD_CACHE_ACCESS_KEY_ID
* S3_BUILD_CACHE_SECRET_KEY
* S3_BUILD_CACHE_SESSION_TOKEN
* S3_BUILD_CACHE_PROFILE

Fixes #74
  • Loading branch information
vlsi committed Oct 22, 2024
1 parent 85dc760 commit b603fdf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/main/kotlin/com/github/burrunan/s3cache/AwsS3BuildCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ open class AwsS3BuildCache : AbstractBuildCache() {
var endpoint: String? = null
var forcePathStyle: Boolean = false
var headers: Map<String?, String?>? = null
var awsAccessKeyId: String? = System.getenv("S3_BUILD_CACHE_ACCESS_KEY_ID")
var awsSecretKey: String? = System.getenv("S3_BUILD_CACHE_SECRET_KEY")
var sessionToken: String? = System.getenv("S3_BUILD_CACHE_SESSION_TOKEN")
var awsProfile: String? = System.getenv("S3_BUILD_CACHE_PROFILE")
var awsAccessKeyId: String? = null
get() = field ?: System.getenv("S3_BUILD_CACHE_ACCESS_KEY_ID")
var awsSecretKey: String? = null
get() = field ?: System.getenv("S3_BUILD_CACHE_SECRET_KEY")
var sessionToken: String? = null
get() = field ?: System.getenv("S3_BUILD_CACHE_SESSION_TOKEN")
var awsProfile: String? = null
get() = field ?: System.getenv("S3_BUILD_CACHE_PROFILE")
var lookupDefaultAwsCredentials: Boolean = false
var credentialsProvider: AwsCredentialsProvider? = null
var showStatistics: Boolean = true
Expand Down
37 changes: 31 additions & 6 deletions src/test/kotlin/com/github/burrunan/s3cache/RemoteCacheTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import org.gradle.api.JavaVersion
import org.gradle.testkit.runner.TaskOutcome
import org.gradle.util.GradleVersion
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.fail
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.extension.RegisterExtension
import org.junit.jupiter.api.fail
import org.junit.jupiter.api.parallel.Execution
import org.junit.jupiter.api.parallel.ExecutionMode
import org.junit.jupiter.params.ParameterizedTest
Expand Down Expand Up @@ -65,7 +65,7 @@ class RemoteCacheTest : BaseGradleTest() {
private fun gradleVersionAndSettings(): Iterable<Arguments> {
if (!isCI) {
// Use only the minimum supported Gradle version to make the test faster
return listOf(arguments("4.1", ConfigurationCache.OFF))
return listOf(arguments("8.10.2", ConfigurationCache.ON))
}
return mutableListOf<Arguments>().apply {
if (JavaVersion.current() <= JavaVersion.VERSION_1_8) {
Expand All @@ -87,6 +87,9 @@ class RemoteCacheTest : BaseGradleTest() {
add(arguments("7.4.2", ConfigurationCache.OFF))
// Configuration cache supports custom caches since 7.5 only: https://github.com/gradle/gradle/issues/14874
add(arguments("7.5", ConfigurationCache.ON))
add(arguments("7.6.3", ConfigurationCache.ON))
add(arguments("8.10.2", ConfigurationCache.ON))
add(arguments("8.0.2", ConfigurationCache.ON))
}
}
}
Expand Down Expand Up @@ -160,7 +163,7 @@ class RemoteCacheTest : BaseGradleTest() {
}
""".trimIndent()
)
val result = prepare(gradleVersion, "props", "-i").build()
val result = prepare(gradleVersion, "props", "-i", "-s").build()
if (isCI) {
println(result.output)
}
Expand All @@ -169,7 +172,16 @@ class RemoteCacheTest : BaseGradleTest() {
}
// Delete output to force task re-execution
projectDir.resolve(outputFile).toFile().delete()
val result2 = prepare(gradleVersion, "props", "props2", "-i").build()
val result2 = prepare(gradleVersion, "props", "props2", "-i")
.withEnvironment(
mapOf(
"S3_BUILD_CACHE_ACCESS_KEY_ID" to "access_key 1",
"S3_BUILD_CACHE_SECRET_KEY" to "secret_key 1",
"S3_BUILD_CACHE_SESSION_TOKEN" to "session_token 1",
"S3_BUILD_CACHE_PROFILE" to "cache_profile 1",
)
)
.build()
if (isCI) {
println(result2.output)
}
Expand All @@ -180,10 +192,23 @@ class RemoteCacheTest : BaseGradleTest() {
if (configurationCache == ConfigurationCache.ON) {
// Delete output to force task re-execution
projectDir.resolve(outputFile).toFile().delete()
val result3 = prepare(gradleVersion, "props", "props2", "-i").build()
// The configuration cache should be reused even though the environment variables change
val result3 = prepare(gradleVersion, "props", "props2", "-i", "-s")
.withEnvironment(
mapOf(
"S3_BUILD_CACHE_ACCESS_KEY_ID" to "access_key 2",
"S3_BUILD_CACHE_SECRET_KEY" to "secret_key 2",
"S3_BUILD_CACHE_SESSION_TOKEN" to "session_token 2",
"S3_BUILD_CACHE_PROFILE" to "cache_profile 2",
)
)
.build()
if (isCI) {
println(result3.output)
}
if (!result3.output.contains("Reusing configuration cache.")) {
fail("Configuration cache was not reused")
}
assertEquals(TaskOutcome.FROM_CACHE, result3.task(":props")?.outcome) {
"second execution => task should be resolved from cache"
}
Expand All @@ -198,7 +223,7 @@ class RemoteCacheTest : BaseGradleTest() {
return
}
if (GradleVersion.version(gradleVersion) < GradleVersion.version("7.0")) {
fail<Unit>("Gradle version $gradleVersion does not support configuration cache")
fail("Gradle version $gradleVersion does not support configuration cache")
}
// Gradle 6.5 expects values ON, OFF, WARN, so we add the option for 7.0 only
projectDir.resolve("gradle.properties").toFile().appendText(
Expand Down

0 comments on commit b603fdf

Please sign in to comment.