Skip to content

Instantly share code, notes, and snippets.

@gabrielfeo
Last active January 26, 2026 19:44
Show Gist options
  • Select an option

  • Save gabrielfeo/4dea066a924a02bd58a2c4c47d76f21c to your computer and use it in GitHub Desktop.

Select an option

Save gabrielfeo/4dea066a924a02bd58a2c4c47d76f21c to your computer and use it in GitHub Desktop.
A comprehensive example of how to require that developers provision a Develocity access key. Based on a snippet from @ribafish.

A comprehensive example of how to require that developers provision a Develocity access key. This sample throws a build failure if a usable key cannot be found and the current build is not to provision a key in the first place.

📖 Develocity Gradle plugin authentication docs

Firmly based on a snippet from @ribafish.

import java.io.FileInputStream
import java.net.HttpURLConnection
import java.net.URI
import java.util.Properties
plugins {
id("com.gradle.develocity") version "4.3.1"
id("com.gradle.common-custom-user-data-gradle-plugin") version "2.4.0"
}
develocity {
server = "https://develocity.example.com"
// ...
}
fun getAccessKeyForHost(filePath: String, host: String): String? {
return try {
val properties = Properties()
FileInputStream(filePath).use {
properties.load(it)
}
properties.getProperty(host)
} catch (e: Exception) {
throw IllegalStateException(
"Error reading file or parsing access key for server $host. " +
"Please, run `./gradlew provisionDevelocityAccessKey` and try again.", e)
}
}
fun validateAccessKey() {
val server = develocity.server.get()
val hostname = URI(server).host.replaceFirst("www.", "")
val accessKey = System.getenv("DEVELOCITY_ACCESS_KEY") ?: getAccessKeyForHost(
System.getProperty("user.home") + "/.gradle/develocity/keys.properties",
hostname
) ?: throw IllegalStateException(
"No Develocity access key found for server $hostname. " +
"Please, run `./gradlew provisionDevelocityAccessKey` and try again."
)
val connection = URI("$server/api/version").toURL().openConnection() as HttpURLConnection
try {
connection.requestMethod = "GET"
connection.setRequestProperty("Authorization", "Bearer $accessKey")
if (connection.responseCode == HttpURLConnection.HTTP_FORBIDDEN ||
connection.responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
throw IllegalStateException(
"Develocity Unauthorized error, response code ${connection.responseCode}. " +
"Please, run `./gradlew provisionDevelocityAccessKey` and try again."
)
}
} finally {
connection.disconnect()
}
}
if (gradle.startParameter.taskNames.none { it == "pDAK" || it == "provisionDevelocityAccessKey" }) {
validateAccessKey()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment