Skip to content

Commit 1be1488

Browse files
authored
Added build cache and develocity plugin (#192)
1 parent f633ff4 commit 1be1488

File tree

12 files changed

+209
-0
lines changed

12 files changed

+209
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ detekt/reports/**
3232

3333
# Ignore kover reports
3434
/kover
35+
36+
scan-journal.log

compiler-plugin/settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pluginManagement {
1313

1414
plugins {
1515
id("settings-conventions")
16+
id("conventions-develocity")
1617
}
1718

1819
includeRootAsPublic()

docs/develocity.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Develocity usage guide
2+
3+
> [!NOTE]
4+
> Build scans and remote build cache are for JetBrains developers only.
5+
>
6+
> To disable publishing attempts, add `kotlinx.rpc.develocity.skipBuildScans=true` property
7+
to your `~/.gradle/gradle.properties` file
8+
9+
Develocity is configured for this project.
10+
That means that you can use both [build scans](https://docs.gradle.org/current/userguide/build_scans.html)
11+
and remote [build cache](https://docs.gradle.org/current/userguide/build_cache.html)
12+
features.
13+
14+
To use build scans, first you need to log in here: https://ge.jetbrains.com.
15+
You can do that directly in Gradle:
16+
17+
```Bash
18+
./gradlew :provisionDevelocityAccessKey
19+
```
20+
21+
Sing in with your Google work account, and that is it.
22+
Now your scans will automatically be published after each build, and
23+
Gradle will read the remote build caches so that your local build may be faster.
24+
25+
This also allows you to check various metrics about your scans (https://ge.jetbrains.com).
26+
27+
Build scan logs are collected locally in the `scan-journal.log` file.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
plugins {
6+
alias(libs.plugins.gradle.kotlin.dsl)
7+
}
8+
9+
configurations.configureEach {
10+
resolutionStrategy {
11+
force(libs.kotlin.reflect)
12+
force(libs.kotlin.stdlib)
13+
force(libs.kotlin.stdlib.jdk7)
14+
force(libs.kotlin.stdlib.jdk8)
15+
}
16+
}
17+
18+
repositories {
19+
mavenCentral()
20+
gradlePluginPortal()
21+
}
22+
23+
dependencies {
24+
implementation("com.gradle:develocity-gradle-plugin:3.17")
25+
implementation("com.gradle:common-custom-user-data-gradle-plugin:2.0.1")
26+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import java.util.*
6+
7+
plugins {
8+
id("com.gradle.develocity")
9+
id("com.gradle.common-custom-user-data-gradle-plugin")
10+
}
11+
12+
develocity {
13+
val startParameter = gradle.startParameter
14+
val scanJournal = File(settingsDir, "scan-journal.log")
15+
16+
server = DEVELOCITY_SERVER
17+
18+
buildScan {
19+
uploadInBackground = !isCIRun
20+
21+
// obfuscate NIC since we don't want to expose user real IP (will be relevant without VPN)
22+
obfuscation {
23+
ipAddresses { addresses -> addresses.map { _ -> "0.0.0.0" } }
24+
}
25+
26+
capture {
27+
fileFingerprints = true
28+
}
29+
30+
buildScanPublished {
31+
scanJournal.appendText("${Date()}$buildScanUri$startParameter\n")
32+
}
33+
34+
val skipBuildScans = settings.providers.gradleProperty("kotlinx.rpc.develocity.skipBuildScans")
35+
.getOrElse("false")
36+
.toBooleanStrict()
37+
38+
publishing.onlyIf { !skipBuildScans }
39+
}
40+
}
41+
42+
buildCache {
43+
if (isCIRun) {
44+
local {
45+
isEnabled = false
46+
}
47+
}
48+
49+
remote(develocity.buildCache) {
50+
isPush = isCIRun
51+
isEnabled = true
52+
}
53+
}
54+
55+
enrichTeamCityData()
56+
enrichGitData()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import com.gradle.develocity.agent.gradle.DevelocityConfiguration
6+
import org.gradle.api.initialization.Settings
7+
8+
fun Settings.enrichTeamCityData() {
9+
val ge = extensions.getByType(DevelocityConfiguration::class.java)
10+
11+
gradle.projectsEvaluated {
12+
if (isCIRun) {
13+
val buildTypeId = "teamcity.buildType.id"
14+
val buildId = "teamcity.build.id"
15+
16+
if (gradle.rootProject.hasProperty(buildId) && gradle.rootProject.hasProperty(buildTypeId)) {
17+
val buildIdValue = gradle.rootProject.property(buildId).toString()
18+
val teamCityBuildNumber = java.net.URLEncoder.encode(buildIdValue, Charsets.UTF_8)
19+
val teamCityBuildTypeId = gradle.rootProject.property(buildTypeId)
20+
21+
ge.buildScan.link(
22+
"kotlinx.rpc TeamCity build",
23+
"${TEAMCITY_URL}/buildConfiguration/${teamCityBuildTypeId}/${teamCityBuildNumber}"
24+
)
25+
}
26+
27+
if (gradle.rootProject.hasProperty(buildId)) {
28+
ge.buildScan.value("CI build id", gradle.rootProject.property(buildId) as String)
29+
}
30+
}
31+
}
32+
}
33+
34+
fun Settings.enrichGitData() {
35+
val ge = extensions.getByType(DevelocityConfiguration::class.java)
36+
37+
gradle.projectsEvaluated {
38+
val skipGitTags = settings.providers.gradleProperty("kotlinx.rpc.develocity.skipGitTags")
39+
.getOrElse("false")
40+
.toBooleanStrict()
41+
42+
if (!isCIRun && !skipGitTags) {
43+
// Git commit id
44+
val commitId = execute("git rev-parse --verify HEAD")
45+
if (commitId.isNotEmpty()) {
46+
ge.buildScan.value("Git Commit ID", commitId)
47+
ge.buildScan.link("GitHub Commit Link", "$GITHUB_REPO/tree/$commitId")
48+
}
49+
50+
// Git branch name
51+
val branchName = execute("git rev-parse --abbrev-ref HEAD")
52+
if (branchName.isNotEmpty()) {
53+
ge.buildScan.value("Git Branch Name", branchName)
54+
ge.buildScan.link("GitHub Branch Link", "$GITHUB_REPO/tree/$branchName")
55+
}
56+
57+
// Git dirty local state
58+
val status = execute("git status --porcelain")
59+
if (status.isNotEmpty()) {
60+
ge.buildScan.value("Git Status", status)
61+
}
62+
}
63+
}
64+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import org.gradle.api.initialization.Settings
6+
7+
@Suppress("UnstableApiUsage")
8+
fun Settings.execute(cmd: String): String {
9+
return settings.providers.exec {
10+
commandLine(cmd.split(" "))
11+
}.standardOutput.asText.get().trim()
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
const val DEVELOCITY_SERVER = "https://ge.jetbrains.com"
6+
const val GITHUB_REPO = "https://github.com/Kotlin/kotlinx-rpc"
7+
const val TEAMCITY_URL = "https://krpc.teamcity.com"
8+
9+
val isCIRun = System.getenv("TEAMCITY_VERSION") != null

gradle-conventions-settings/settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ rootProject.name = "gradle-conventions-settings"
66

77
// Code below is a hack because a chicken-egg problem, I can't use myself as a settings-plugin
88
apply(from = "src/main/kotlin/settings-conventions.settings.gradle.kts")
9+
10+
include(":develocity")

gradle.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ systemProp.org.gradle.kotlin.dsl.precompiled.accessors.strict=true
3838
# Using compileOnly dependencies in these targets is not currently supported
3939
# because compileOnly dependencies must be present during the compilation of projects that depend on this project.
4040
kotlin.suppressGradlePluginWarnings=IncorrectCompileOnlyDependencyWarning
41+
42+
# Uncomment to skip attempts to publish Develocity build scans
43+
# Add this property to ~/.gradle/gradle.properties to avoid polluting git with unwanted changes
44+
#kotlinx.rpc.develocity.skipBuildScans=true
45+
46+
# Uncomment to skip adding git tags to Develocity build scan
47+
# Add this property to ~/.gradle/gradle.properties to avoid polluting git with unwanted changes
48+
#kotlinx.rpc.develocity.skipGitTags=true

0 commit comments

Comments
 (0)