Skip to content

Commit bbb1028

Browse files
authored
Rework how we release to central (#14146)
1 parent 66ab786 commit bbb1028

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ jobs:
100100
SONATYPE_KEY: ${{ secrets.SONATYPE_KEY }}
101101
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
102102
GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }}
103-
run: ./gradlew assemble spdxSbom publishToSonatype closeAndReleaseSonatypeStagingRepository
103+
# run: ./gradlew assemble spdxSbom publishToSonatype closeAndReleaseSonatypeStagingRepository
104+
run: ./gradlew assemble spdxSbom publishAllPublicationToReleaseRepoRepository generateReleaseBundle uploadReleaseBundle
104105

105106
- name: Build and publish gradle plugins
106107
env:

build.gradle.kts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import okhttp3.MediaType.Companion.toMediaType
2+
import okhttp3.OkHttpClient
3+
import okhttp3.RequestBody.Companion.asRequestBody
14
import java.time.Duration
5+
import java.util.Base64
26

37
plugins {
48
id("idea")
@@ -16,6 +20,12 @@ plugins {
1620
id("org.graalvm.buildtools.native") apply false
1721
}
1822

23+
buildscript {
24+
dependencies {
25+
classpath("com.squareup.okhttp3:okhttp:4.12.0")
26+
}
27+
}
28+
1929
apply(from = "version.gradle.kts")
2030

2131
nexusPublishing {
@@ -111,6 +121,8 @@ if (gradle.startParameter.taskNames.contains("listTestsInPartition")) {
111121
}
112122

113123
tasks {
124+
val stableVersion = version.toString().replace("-alpha", "")
125+
114126
val generateFossaConfiguration by registering {
115127
group = "Help"
116128
description = "Generate .fossa.yml configuration file"
@@ -141,4 +153,48 @@ tasks {
141153
}
142154
}
143155
}
156+
157+
val generateReleaseBundle by registering(Zip::class) {
158+
dependsOn(getTasksByName("publishAllPublicationToReleaseRepoRepository", true))
159+
from("releaseRepo")
160+
161+
exclude("**/maven-metadata.*")
162+
duplicatesStrategy = DuplicatesStrategy.FAIL
163+
includeEmptyDirs = false
164+
archiveFileName.set("release-bundle-$stableVersion.zip")
165+
}
166+
167+
val uploadReleaseBundle by registering {
168+
dependsOn(generateReleaseBundle)
169+
170+
val bundle = generateReleaseBundle.get().outputs.files.singleFile
171+
val httpClient = OkHttpClient()
172+
val username = System.getenv("SONATYPE_USER") ?: throw GradleException("Sonatype user not set")
173+
val password = System.getenv("SONATYPE_KEY") ?: throw GradleException("Sonatype key not set")
174+
val token = Base64.getEncoder().encodeToString("$username:$password".toByteArray())
175+
176+
var query = "?name=opentelemetry-java-instrumentation-$stableVersion"
177+
// uncomment to automatically publish the release
178+
// query += "&publishingType=AUTOMATIC"
179+
180+
val request = okhttp3.Request.Builder()
181+
.url("https://central.sonatype.com/api/v1/publisher/upload$query")
182+
.post(
183+
okhttp3.MultipartBody.Builder().addFormDataPart(
184+
"bundle",
185+
bundle.name,
186+
bundle.asRequestBody("application/zip".toMediaType())
187+
).build()
188+
)
189+
.header("authorization", "Bearer $token")
190+
.build()
191+
httpClient.newCall(request).execute().use { response ->
192+
response.body!!.string()
193+
}
194+
195+
httpClient.newCall(request).execute().use { response ->
196+
if (response.code != 201) throw GradleException("Unexpected response status ${response.code} while uploading the release bundle")
197+
println("Uploaded deployment ${response.body!!.string()}")
198+
}
199+
}
144200
}

conventions/src/main/kotlin/otel.publish-conventions.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ publishing {
6565
}
6666
}
6767
}
68+
69+
repositories {
70+
maven {
71+
name = "ReleaseRepo"
72+
url = File(project.rootDir.path, "releaseRepo").toURI()
73+
}
74+
}
6875
}
6976

7077
fun artifactPrefix(p: Project, archivesBaseName: String): String {

0 commit comments

Comments
 (0)