From ab539b13ac60035ad76c38e3082df6b6c7904e72 Mon Sep 17 00:00:00 2001 From: Dmitrii Krasnov <72867328+dkrasnoff@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:31:32 +0100 Subject: [PATCH] Update build cache authorisation (#487) * Change the way of remote build cache authorization. * Fixed build scan enabled flag --- build-settings-logic/settings.gradle.kts | 100 +++++++++++++++++- build-settings-logic/src/main/kotlin/Utils.kt | 19 +++- .../atomicfu-develocity.settings.gradle.kts | 57 ++++++++++ ...cfu-gradle-build-cache.settings.gradle.kts | 66 ------------ ...icfu-gradle-build-scan.settings.gradle.kts | 34 ------ gradle/libs.versions.toml | 2 +- settings.gradle.kts | 3 +- 7 files changed, 175 insertions(+), 106 deletions(-) create mode 100644 build-settings-logic/src/main/kotlin/atomicfu-develocity.settings.gradle.kts delete mode 100644 build-settings-logic/src/main/kotlin/atomicfu-gradle-build-cache.settings.gradle.kts delete mode 100644 build-settings-logic/src/main/kotlin/atomicfu-gradle-build-scan.settings.gradle.kts diff --git a/build-settings-logic/settings.gradle.kts b/build-settings-logic/settings.gradle.kts index 16c0df26..8ffc2573 100644 --- a/build-settings-logic/settings.gradle.kts +++ b/build-settings-logic/settings.gradle.kts @@ -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 + get() = + atomicfuProperty("build.scan.enabled", String::toBoolean) + .orElse(buildingOnCi) + +val DEFAULT_ATOMICFU_USER_NAME = "" + +/** + * Optionally override the default name attached to a Build Scan. + */ +val Settings.buildScanUsername: Provider + 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 + get() = atomicfuProperty("build.cache.local.enabled", String::toBoolean) + .orElse(!buildingOnCi) + +val Settings.buildCacheLocalDirectory: Provider + get() = atomicfuProperty("build.cache.local.directory") + +val Settings.buildCachePushEnabled: Provider + get() = atomicfuProperty("build.cache.push", String::toBoolean) + .orElse(buildingOnTeamCity) + +internal fun Settings.atomicfuProperty(name: String): Provider = + providers.gradleProperty("org.jetbrains.atomicfu.$name") + +internal fun Settings.atomicfuProperty(name: String, convert: (String) -> T): Provider = + 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() + } +} \ No newline at end of file diff --git a/build-settings-logic/src/main/kotlin/Utils.kt b/build-settings-logic/src/main/kotlin/Utils.kt index 80214dc3..e490c8a7 100644 --- a/build-settings-logic/src/main/kotlin/Utils.kt +++ b/build-settings-logic/src/main/kotlin/Utils.kt @@ -15,10 +15,10 @@ val Settings.buildScanEnabled: Provider atomicfuProperty("build.scan.enabled", String::toBoolean) .orElse(buildingOnCi) -internal const val DEFAULT_ATOMICFU_USER_NAME = "" +const val DEFAULT_ATOMICFU_USER_NAME = "" /** - * Optionaly override the default name attached to a Build Scan. + * Optionally override the default name attached to a Build Scan. */ val Settings.buildScanUsername: Provider get() = @@ -26,6 +26,21 @@ val Settings.buildScanUsername: Provider .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 + get() = atomicfuProperty("build.cache.local.enabled", String::toBoolean) + .orElse(!buildingOnCi) + +val Settings.buildCacheLocalDirectory: Provider + get() = atomicfuProperty("build.cache.local.directory") + +val Settings.buildCachePushEnabled: Provider + get() = atomicfuProperty("build.cache.push", String::toBoolean) + .orElse(buildingOnTeamCity) + internal fun Settings.atomicfuProperty(name: String): Provider = providers.gradleProperty("org.jetbrains.atomicfu.$name") diff --git a/build-settings-logic/src/main/kotlin/atomicfu-develocity.settings.gradle.kts b/build-settings-logic/src/main/kotlin/atomicfu-develocity.settings.gradle.kts new file mode 100644 index 00000000..14898643 --- /dev/null +++ b/build-settings-logic/src/main/kotlin/atomicfu-develocity.settings.gradle.kts @@ -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() + } +} \ No newline at end of file diff --git a/build-settings-logic/src/main/kotlin/atomicfu-gradle-build-cache.settings.gradle.kts b/build-settings-logic/src/main/kotlin/atomicfu-gradle-build-cache.settings.gradle.kts deleted file mode 100644 index 81a621db..00000000 --- a/build-settings-logic/src/main/kotlin/atomicfu-gradle-build-cache.settings.gradle.kts +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Gradle Build Cache conventions. - * - * 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("...") }` - * - * See https://docs.gradle.org/8.8/userguide/build_cache.html#sec:build_cache_composite - * - * ⚠️ This file _must_ be applicable as a script plugin and so _must not_ depend on other source files. - * - * Based on https://github.com/JetBrains/kotlin/blob/2675531624d42851af502a993bbefd65ee3e38ef/repo/gradle-settings-conventions/build-cache/src/main/kotlin/build-cache.settings.gradle.kts - */ - -// Don't remove or move to *.kt files these functions and variables -// they are required for applying this plugin as convention plugin to `build-settings-logic` settings file -internal fun Settings.atomicfuProperty(name: String): Provider = - providers.gradleProperty("org.jetbrains.atomicfu.$name") - -internal fun Settings.atomicfuProperty(name: String, convert: (String) -> T): Provider = - atomicfuProperty(name).map(convert) - -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 - -/** - * 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 buildCacheLocalEnabled: Provider = - atomicfuProperty("build.cache.local.enabled", String::toBoolean) - .orElse(!buildingOnCi) - -val buildCacheLocalDirectory: Provider = - atomicfuProperty("build.cache.local.directory") - -val buildCachePushEnabled: Provider = - atomicfuProperty("build.cache.push", String::toBoolean) - .orElse(buildingOnTeamCity) - -val buildCacheUser: Provider = - providers.gradleProperty("build.cache.user") - -val buildCachePassword: Provider = - providers.gradleProperty("build.cache.password") - -buildCache { - local { - isEnabled = buildCacheLocalEnabled.get() - if (buildCacheLocalDirectory.orNull != null) { - directory = buildCacheLocalDirectory.get() - } - } - remote { - url = uri("https://ge.jetbrains.com/cache/") - isPush = buildCachePushEnabled.get() - - if (buildCacheUser.isPresent && - buildCachePassword.isPresent - ) { - credentials.username = buildCacheUser.get() - credentials.password = buildCachePassword.get() - } - } -} \ No newline at end of file diff --git a/build-settings-logic/src/main/kotlin/atomicfu-gradle-build-scan.settings.gradle.kts b/build-settings-logic/src/main/kotlin/atomicfu-gradle-build-scan.settings.gradle.kts deleted file mode 100644 index ee1664d6..00000000 --- a/build-settings-logic/src/main/kotlin/atomicfu-gradle-build-scan.settings.gradle.kts +++ /dev/null @@ -1,34 +0,0 @@ -// this is a settings convention plugin for Gradle Develocity - -plugins { - id("com.gradle.develocity") -} - -develocity { - if (buildScanEnabled.get()) { - val overriddenName = buildScanUsername.orNull - server = "https://ge.jetbrains.com/" - buildScan { - publishing.onlyIf { true } - capture { - fileFingerprints = true - buildLogging = true - uploadInBackground = true - } - 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" - } - } - } - } - } -} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 87d5201a..8b702517 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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] diff --git a/settings.gradle.kts b/settings.gradle.kts index 9babb50d..fb24a9d4 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -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") }