Skip to content

Commit

Permalink
Update build cache authorisation (#487)
Browse files Browse the repository at this point in the history
* Change the way of remote build cache authorization.

* Fixed build scan enabled flag
  • Loading branch information
dkrasnoff authored Nov 26, 2024
1 parent b94c6bd commit ab539b1
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 106 deletions.
100 changes: 99 additions & 1 deletion build-settings-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,103 @@ dependencyResolutionManagement {
}
}

apply(from = "src/main/kotlin/atomicfu-gradle-build-cache.settings.gradle.kts")
apply(from = "src/main/kotlin/atomicfu-cache-redirector.settings.gradle.kts")

/*
We have to make sure the build-cache config is consistent in the settings.gradle.kts
files of all projects. The natural way to share logic is with a convention plugin,
but we can't apply a convention plugin in build-settings-logic to itself, so we just copy it.
We could publish build-settings-logic to a Maven repo? But this is quicker and easier.
The following content should be kept in sync with the content of:
`src/main/kotlin/atomicfu-develocity.settings.gradle.kts`
The only difference with the script above is explicitly specified versions
*/

// version should be kept in sync with `gradle/libs.versions.toml`
plugins {
id("com.gradle.develocity") version "3.17.6"
}

val buildingOnTeamCity: Boolean = System.getenv("TEAMCITY_VERSION") != null
val buildingOnGitHub: Boolean = System.getenv("GITHUB_ACTION") != null
val buildingOnCi: Boolean = System.getenv("CI") != null || buildingOnTeamCity || buildingOnGitHub

// NOTE: build scan properties are documented in README.md
val Settings.buildScanEnabled: Provider<Boolean>
get() =
atomicfuProperty("build.scan.enabled", String::toBoolean)
.orElse(buildingOnCi)

val DEFAULT_ATOMICFU_USER_NAME = "<default>"

/**
* Optionally override the default name attached to a Build Scan.
*/
val Settings.buildScanUsername: Provider<String>
get() =
atomicfuProperty("build.scan.username")
.orElse(DEFAULT_ATOMICFU_USER_NAME)
.map(String::trim)

/**
* Disable Local Cache on CI, because CI machines are short-lived, so local caching doesn't help a lot.
* Also, to force CI machines to update the remote cache.
*/
val Settings.buildCacheLocalEnabled: Provider<Boolean>
get() = atomicfuProperty("build.cache.local.enabled", String::toBoolean)
.orElse(!buildingOnCi)

val Settings.buildCacheLocalDirectory: Provider<String>
get() = atomicfuProperty("build.cache.local.directory")

val Settings.buildCachePushEnabled: Provider<Boolean>
get() = atomicfuProperty("build.cache.push", String::toBoolean)
.orElse(buildingOnTeamCity)

internal fun Settings.atomicfuProperty(name: String): Provider<String> =
providers.gradleProperty("org.jetbrains.atomicfu.$name")

internal fun <T : Any> Settings.atomicfuProperty(name: String, convert: (String) -> T): Provider<T> =
atomicfuProperty(name).map(convert)

develocity {
val buildScanEnabled = buildScanEnabled.get()
if (buildScanEnabled) {
val overriddenName = buildScanUsername.orNull
server = "https://ge.jetbrains.com/"
buildScan {
publishing.onlyIf { buildScanEnabled }
capture {
fileFingerprints = buildScanEnabled
buildLogging = buildScanEnabled
uploadInBackground = buildScanEnabled
}
obfuscation {
ipAddresses { _ -> listOf("0.0.0.0") }
hostname { _ -> "concealed" }
username { originalUsername ->
when {
buildingOnTeamCity -> "TeamCity"
buildingOnGitHub -> "GitHub"
buildingOnCi -> "CI"
!overriddenName.isNullOrBlank() && overriddenName != DEFAULT_ATOMICFU_USER_NAME -> overriddenName
overriddenName == DEFAULT_ATOMICFU_USER_NAME -> originalUsername
else -> "unknown"
}
}
}
}
}
}

buildCache {
local {
isEnabled = buildCacheLocalEnabled.get()
if (buildCacheLocalDirectory.orNull != null) {
directory = buildCacheLocalDirectory.get()
}
}
remote(develocity.buildCache) {
isPush = buildCachePushEnabled.get()
}
}
19 changes: 17 additions & 2 deletions build-settings-logic/src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,32 @@ val Settings.buildScanEnabled: Provider<Boolean>
atomicfuProperty("build.scan.enabled", String::toBoolean)
.orElse(buildingOnCi)

internal const val DEFAULT_ATOMICFU_USER_NAME = "<default>"
const val DEFAULT_ATOMICFU_USER_NAME = "<default>"

/**
* Optionaly override the default name attached to a Build Scan.
* Optionally override the default name attached to a Build Scan.
*/
val Settings.buildScanUsername: Provider<String>
get() =
atomicfuProperty("build.scan.username")
.orElse(DEFAULT_ATOMICFU_USER_NAME)
.map(String::trim)

/**
* Disable Local Cache on CI, because CI machines are short-lived, so local caching doesn't help a lot.
* Also, to force CI machines to update the remote cache.
*/
val Settings.buildCacheLocalEnabled: Provider<Boolean>
get() = atomicfuProperty("build.cache.local.enabled", String::toBoolean)
.orElse(!buildingOnCi)

val Settings.buildCacheLocalDirectory: Provider<String>
get() = atomicfuProperty("build.cache.local.directory")

val Settings.buildCachePushEnabled: Provider<Boolean>
get() = atomicfuProperty("build.cache.push", String::toBoolean)
.orElse(buildingOnTeamCity)

internal fun Settings.atomicfuProperty(name: String): Provider<String> =
providers.gradleProperty("org.jetbrains.atomicfu.$name")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// this is a settings convention plugin for Gradle Develocity

// Based on https://github.com/JetBrains/kotlin/blob/c20f644ee4cd8d28b39b12ea5304b68c5639e531/repo/gradle-settings-conventions/develocity/src/main/kotlin/develocity.settings.gradle.kts
// Because Atomicfu uses Composite Builds, Build Cache must be configured consistently on:
// - the root settings.gradle.kts,
// - and the settings.gradle.kts of any projects added with `pluginManagement { includedBuild("...") }`
// The Content of this file should be kept in sync with the content at the end of:
// `build-settings-logic/settings.gradle.kts`
// useful links:
// - develocity: https://docs.gradle.com/develocity/gradle-plugin/
// - build cache: https://docs.gradle.org/8.4/userguide/build_cache.html#sec:build_cache_composite
plugins {
id("com.gradle.develocity")
}

develocity {
val buildScanEnabled = buildScanEnabled.get()
if (buildScanEnabled) {
val overriddenName = buildScanUsername.orNull
server = "https://ge.jetbrains.com/"
buildScan {
publishing.onlyIf { buildScanEnabled }
capture {
fileFingerprints = buildScanEnabled
buildLogging = buildScanEnabled
uploadInBackground = buildScanEnabled
}
obfuscation {
ipAddresses { _ -> listOf("0.0.0.0") }
hostname { _ -> "concealed" }
username { originalUsername ->
when {
buildingOnTeamCity -> "TeamCity"
buildingOnGitHub -> "GitHub"
buildingOnCi -> "CI"
!overriddenName.isNullOrBlank() && overriddenName != DEFAULT_ATOMICFU_USER_NAME -> overriddenName
overriddenName == DEFAULT_ATOMICFU_USER_NAME -> originalUsername
else -> "unknown"
}
}
}
}
}
}


buildCache {
local {
isEnabled = buildCacheLocalEnabled.get()
if (buildCacheLocalDirectory.orNull != null) {
directory = buildCacheLocalDirectory.get()
}
}
remote(develocity.buildCache) {
isPush = buildCachePushEnabled.get()
}
}

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ maven-pluginTools = "3.5"
node-gradle = "3.1.1"
rhino = "1.7.10"
gradle-plugin-publish = "1.2.1"
gradle-develocity = "3.17.5"
gradle-develocity = "3.17.6"

[libraries]

Expand Down
3 changes: 1 addition & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ pluginManagement {
plugins {
id("atomicfu-dependency-resolution-management")
id("org.gradle.toolchains.foojay-resolver-convention") version("0.8.0")
id("atomicfu-gradle-build-scan")
id("atomicfu-gradle-build-cache")
id("atomicfu-develocity")
id("atomicfu-cache-redirector")
}

Expand Down

0 comments on commit ab539b1

Please sign in to comment.