Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import com.vanniktech.maven.publish.SonatypeHost
import helpers.configureMavenCentralMetadata
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi

val kotlinJvmTarget: String by project
Expand Down Expand Up @@ -72,30 +71,32 @@ subprojects {
}
}

@OptIn(ExperimentalEncodingApi::class)
private fun MavenPublishBaseExtension.signIfKeyPresent(project: Project) {
val keyId = System.getenv("KEY_ID")
val keyBytes = runCatching {
Base64.decode(System.getenv("SECRING").toByteArray()).decodeToString()
}.getOrNull()
val keyPassword = System.getenv("PASSWORD")
fun MavenPublishBaseExtension.signIfKeyPresent(project: Project) {
val keyId = System.getenv("SIGNING_KEY_ID")
val signingKey = System.getenv("SIGNING_KEY")
val signingKeyPassphrase = System.getenv("SIGNING_KEY_PASSPHRASE")

if (keyBytes != null && keyPassword != null) {
project.logger.info("Signing artifacts with in-memory PGP key (.gpg)")
if (!signingKey.isNullOrBlank()) {
project.logger.info("Signing artifacts with in-memory PGP key for ${project.path}")
project.extensions.configure<SigningExtension>("signing") {
// For binary .gpg keys
if (keyId == null) {
useInMemoryPgpKeys(keyBytes, keyPassword)
} else {
useInMemoryPgpKeys(keyId, keyBytes, keyPassword)
}
useInMemoryPgpKeys(keyId, preprocessPrivateGpgKey(signingKey), signingKeyPassphrase)
signAllPublications()
}
} else {
project.logger.info("Skipping signing of artifacts: PGP key or password not found in environment variables")
project.logger.warn("Skipping signing of artifacts: PGP key or password not found in environment variables for ${project.path}")
}
}

private fun preprocessPrivateGpgKey(key: String): String {
val prefix = "-----BEGIN PGP PRIVATE KEY BLOCK-----"
val suffix = "-----END PGP PRIVATE KEY BLOCK-----"
val delimiter = "\r\n"
return prefix + delimiter + key
.replace(prefix, "")
.replace(suffix, "")
.replace(" ", "\r\n") + delimiter + suffix
}

tasks.register("printVersion") {
doLast {
println(projectVersion)
Expand Down
Loading