1
+ import okhttp3.MediaType.Companion.toMediaType
2
+ import okhttp3.OkHttpClient
3
+ import okhttp3.RequestBody.Companion.asRequestBody
1
4
import java.time.Duration
5
+ import java.util.Base64
2
6
3
7
plugins {
4
8
id(" idea" )
@@ -16,6 +20,12 @@ plugins {
16
20
id(" org.graalvm.buildtools.native" ) apply false
17
21
}
18
22
23
+ buildscript {
24
+ dependencies {
25
+ classpath(" com.squareup.okhttp3:okhttp:4.12.0" )
26
+ }
27
+ }
28
+
19
29
apply (from = " version.gradle.kts" )
20
30
21
31
nexusPublishing {
@@ -111,6 +121,8 @@ if (gradle.startParameter.taskNames.contains("listTestsInPartition")) {
111
121
}
112
122
113
123
tasks {
124
+ val stableVersion = version.toString().replace(" -alpha" , " " )
125
+
114
126
val generateFossaConfiguration by registering {
115
127
group = " Help"
116
128
description = " Generate .fossa.yml configuration file"
@@ -141,4 +153,48 @@ tasks {
141
153
}
142
154
}
143
155
}
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
+ }
144
200
}
0 commit comments