Skip to content

Commit a74466d

Browse files
authored
Merge pull request #6 from EmanueleIannuzzi/fix/publishing
Fix PGP key handling and refactor Base64 API usage
2 parents b33b28f + 8c618a6 commit a74466d

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

build.gradle.kts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import com.vanniktech.maven.publish.SonatypeHost
33
import helpers.configureMavenCentralMetadata
44
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
55
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
6-
import kotlin.io.encoding.Base64
76
import kotlin.io.encoding.ExperimentalEncodingApi
87

98
val kotlinJvmTarget: String by project
@@ -72,30 +71,32 @@ subprojects {
7271
}
7372
}
7473

75-
@OptIn(ExperimentalEncodingApi::class)
76-
private fun MavenPublishBaseExtension.signIfKeyPresent(project: Project) {
77-
val keyId = System.getenv("KEY_ID")
78-
val keyBytes = runCatching {
79-
Base64.decode(System.getenv("SECRING").toByteArray()).decodeToString()
80-
}.getOrNull()
81-
val keyPassword = System.getenv("PASSWORD")
74+
fun MavenPublishBaseExtension.signIfKeyPresent(project: Project) {
75+
val keyId = System.getenv("SIGNING_KEY_ID")
76+
val signingKey = System.getenv("SIGNING_KEY")
77+
val signingKeyPassphrase = System.getenv("SIGNING_KEY_PASSPHRASE")
8278

83-
if (keyBytes != null && keyPassword != null) {
84-
project.logger.info("Signing artifacts with in-memory PGP key (.gpg)")
79+
if (!signingKey.isNullOrBlank()) {
80+
project.logger.info("Signing artifacts with in-memory PGP key for ${project.path}")
8581
project.extensions.configure<SigningExtension>("signing") {
86-
// For binary .gpg keys
87-
if (keyId == null) {
88-
useInMemoryPgpKeys(keyBytes, keyPassword)
89-
} else {
90-
useInMemoryPgpKeys(keyId, keyBytes, keyPassword)
91-
}
82+
useInMemoryPgpKeys(keyId, preprocessPrivateGpgKey(signingKey), signingKeyPassphrase)
9283
signAllPublications()
9384
}
9485
} else {
95-
project.logger.info("Skipping signing of artifacts: PGP key or password not found in environment variables")
86+
project.logger.warn("Skipping signing of artifacts: PGP key or password not found in environment variables for ${project.path}")
9687
}
9788
}
9889

90+
private fun preprocessPrivateGpgKey(key: String): String {
91+
val prefix = "-----BEGIN PGP PRIVATE KEY BLOCK-----"
92+
val suffix = "-----END PGP PRIVATE KEY BLOCK-----"
93+
val delimiter = "\r\n"
94+
return prefix + delimiter + key
95+
.replace(prefix, "")
96+
.replace(suffix, "")
97+
.replace(" ", "\r\n") + delimiter + suffix
98+
}
99+
99100
tasks.register("printVersion") {
100101
doLast {
101102
println(projectVersion)

0 commit comments

Comments
 (0)