Last active
July 9, 2025 15:49
-
-
Save gabrielfeo/3b95cdc7080fbfa5256edfae3a2f4085 to your computer and use it in GitHub Desktop.
Get build cache artifacts of a given build scan from a Develocity instance. One script will get only build cache artifacts of Task executions, while the other will get all cache artifacts including of 'artifact transform' operations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env kotlin | |
/* | |
This script demonstrates how to retrieve the cache artifacts that were created for Task and | |
artifact transform outputs as part of a given build, sorted by size (bytes) descending, using the | |
Develocity REST API to fetch build information. | |
(https://docs.gradle.com/develocity/api-manual/) | |
DISCLAIMER: develocity-api-kotlin is a third-party library, not an official Gradle product. | |
(https://github.com/gabrielfeo/develocity-api-kotlin) | |
To run it, install the Kotlin compiler CLI (e.g. brew install kotlin) and set variables: | |
export DEVELOCITY_API_URL="https://develocity.example.com/api/" | |
export DEVELOCITY_API_TOKEN="your_access_key_here" | |
The "API token" must be a Develocity access key from a user with API permission. It should | |
not be prefixed with the hostname, key only. | |
USAGE: | |
./all-cache-artifacts-sorted.main.kts <build-scan-id> | |
*/ | |
@file:DependsOn("com.gabrielfeo:develocity-api-kotlin:2024.3.0") | |
import com.gabrielfeo.develocity.api.* | |
import com.gabrielfeo.develocity.api.model.* | |
import com.gabrielfeo.develocity.api.extension.* | |
import kotlinx.coroutines.* | |
import kotlinx.coroutines.flow.* | |
import java.time.* | |
import kotlin.system.exitProcess | |
val api = DevelocityApi.newInstance() | |
suspend fun getBuildWithCacheableOperations(scanId: String): Build { | |
return api.buildsApi.getBuild( | |
id = scanId, | |
// https://gabrielfeo.github.io/develocity-api-kotlin/library/com.gabrielfeo.develocity.api.model/-gradle-build-cache-performance | |
models = listOf( | |
BuildModelName.gradleBuildCachePerformance, | |
BuildModelName.gradleArtifactTransformExecutions, | |
), | |
) | |
} | |
data class Operation(val name: String, val cacheEntrySize: Long?) | |
fun getArtifactSizeOfAllCacheOperations(build: Build): List<Operation> { | |
val taskExecutions = checkNotNull(build.models?.gradleBuildCachePerformance?.model) { | |
"Cache performance details not found. Problem: ${build.models?.gradleBuildCachePerformance?.problem}" | |
}.taskExecution | |
val artifactTransforms = checkNotNull(build.models?.gradleArtifactTransformExecutions?.model) { | |
"Artifact transform executions not found. Problem: ${build.models?.gradleArtifactTransformExecutions?.problem}" | |
}.artifactTransformExecutions.orEmpty() | |
return buildList { | |
taskExecutions.forEach { | |
add(Operation(it.taskPath, it.cacheArtifactSize)) | |
} | |
artifactTransforms.forEach { | |
add(Operation(it.artifactTransformExecutionName, it.cacheArtifactSize)) | |
} | |
} | |
} | |
fun printSortedCacheEntries(artifactSizeByOperation: List<Operation>) { | |
artifactSizeByOperation | |
.sortedByDescending { (_, size) -> size } | |
.forEach { (task, entrySize) -> | |
println("%-120s | %s".format(task, entrySize)) | |
} | |
} | |
fun main() = runBlocking { | |
val buildScanId = args.singleOrNull() ?: run { | |
System.err.println("USAGE: ./all-cache-artifacts-sorted.main.kts <build-scan-id>") | |
exitProcess(1) | |
} | |
val build = getBuildWithCacheableOperations(buildScanId) | |
val buildCacheDetails = getArtifactSizeOfAllCacheOperations(build) | |
printSortedCacheEntries(buildCacheDetails) | |
api.shutdown() | |
} | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env kotlin | |
/* | |
This script demonstrates how to retrieve the cache artifacts that were created for Task outputs in | |
a given build, sorted by size (bytes) descending, using the Develocity REST API to fetch build information. | |
(https://docs.gradle.com/develocity/api-manual/) | |
DISCLAIMER: develocity-api-kotlin is a third-party library, not an official Gradle product. | |
(https://github.com/gabrielfeo/develocity-api-kotlin) | |
To run it, install the Kotlin compiler CLI (e.g. brew install kotlin) and set variables: | |
export DEVELOCITY_API_URL="https://develocity.example.com/api/" | |
export DEVELOCITY_API_TOKEN="your_access_key_here" | |
The "API token" must be a Develocity access key from a user with API permission. It should | |
not be prefixed with the hostname, key only. | |
USAGE: | |
./task-cache-artifacts-sorted.main.kts <build-scan-id> | |
*/ | |
@file:DependsOn("com.gabrielfeo:develocity-api-kotlin:2024.3.0") | |
import com.gabrielfeo.develocity.api.* | |
import com.gabrielfeo.develocity.api.model.* | |
import com.gabrielfeo.develocity.api.extension.* | |
import kotlinx.coroutines.* | |
import kotlinx.coroutines.flow.* | |
import java.time.* | |
import kotlin.system.exitProcess | |
val api = DevelocityApi.newInstance() | |
suspend fun getCachePerformanceDetails(scanId: String): GradleBuildCachePerformance { | |
val build: Build = api.buildsApi.getBuild( | |
id = scanId, | |
// https://gabrielfeo.github.io/develocity-api-kotlin/library/com.gabrielfeo.develocity.api.model/-gradle-build-cache-performance | |
models = listOf(BuildModelName.gradleBuildCachePerformance), | |
) | |
return checkNotNull(build.models?.gradleBuildCachePerformance?.model) { | |
"Cache performance details not found. Problem: ${build.models?.gradleBuildCachePerformance?.problem}" | |
} | |
} | |
fun printSortedCacheEntries(buildCacheDetails: GradleBuildCachePerformance) { | |
buildCacheDetails.taskExecution | |
.map { execution -> execution.taskPath to execution.cacheArtifactSize } | |
.sortedByDescending { (_, entrySize) -> entrySize } | |
.forEach { (task, entrySize) -> | |
println("%-120s | %s".format(task, entrySize)) | |
} | |
} | |
fun main() = runBlocking { | |
val buildScanId = args.singleOrNull() ?: run { | |
System.err.println("USAGE: ./task-cache-artifacts-sorted.main.kts <build-scan-id>") | |
exitProcess(1) | |
} | |
val buildCacheDetails = getCachePerformanceDetails(buildScanId) | |
printSortedCacheEntries(buildCacheDetails) | |
api.shutdown() | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment