Skip to content

Commit f68c45b

Browse files
authored
Merge pull request #86 from commercetools/maven_api_sync
add bintray maven sync workflow
2 parents c880843 + 6afe1e1 commit f68c45b

File tree

2 files changed

+122
-5
lines changed

2 files changed

+122
-5
lines changed

.github/workflows/mavensync.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
on:
2+
workflow_run:
3+
workflows:
4+
- "AutoRelease"
5+
types:
6+
- completed
7+
8+
jobs:
9+
mavensync:
10+
name: Sync to Maven
11+
12+
runs-on: ubuntu-latest
13+
14+
if: startsWith( github.ref, 'refs/tags/')
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Setup Java
20+
uses: actions/setup-java@v1
21+
with:
22+
java-version: '8'
23+
24+
- name: Retrieve branch name
25+
id: branch_name
26+
run: |
27+
echo ::set-output name=SOURCE_NAME::${GITHUB_REF#refs/*/}
28+
echo ::set-output name=SOURCE_BRANCH::${GITHUB_REF#refs/heads/}
29+
echo ::set-output name=SOURCE_TAG::${GITHUB_REF#refs/tags/}
30+
31+
- run: ./gradlew -Pversion=$SOURCE_TAG bintrayMavenSync --info --stacktrace
32+
env:
33+
CTP_BINTRAY_USER: ${{ secrets.BINTRAY_USER }}
34+
CTP_BINTRAY_KEY: ${{ secrets.BINTRAY_KEY }}
35+
CTP_OSS_USER: ${{ secrets.OSS_USER }}
36+
CTP_OSS_SECRET: ${{ secrets.OSS_SECRET }}
37+
MAVEN_SYNC: "true"
38+
SOURCE_TAG: ${{ steps.branch_name.outputs.SOURCE_TAG }}

build.gradle

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,6 @@ subprojects { project ->
195195
gpg {
196196
sign = true
197197
}
198-
mavenCentralSync {
199-
sync = System.getenv("MAVEN_SYNC") == "true"
200-
user = System.getenv('CTP_OSS_USER')
201-
password = System.getenv('CTP_OSS_SECRET')
202-
}
203198
}
204199
}
205200
}
@@ -234,7 +229,91 @@ subprojects { project ->
234229
}
235230
}
236231

232+
task bintrayMavenSync(type: MavenCentralSync) {
233+
mustRunAfter bintrayPublish
234+
dryRun = System.getenv("MAVEN_SYNC") != "true"
235+
repo = 'maven'
236+
pkgName = project.name
237+
subject = 'commercetools'
238+
bintrayUser = System.getenv('CTP_BINTRAY_USER') ?: ''
239+
bintrayApiKey = System.getenv('CTP_BINTRAY_KEY') ?: ''
240+
ossUser = System.getenv('CTP_OSS_USER') ?: ''
241+
ossPassword = System.getenv('CTP_OSS_SECRET') ?: ''
242+
versionName = version.toString()
243+
}
237244
}
238245

239246
sourceCompatibility = 1.8
240247
}
248+
249+
import com.jfrog.bintray.gradle.Utils
250+
import com.jfrog.bintray.gradle.BintrayHttpClientFactory
251+
import static groovyx.net.http.ContentType.JSON
252+
import static groovyx.net.http.Method.POST
253+
254+
class MavenCentralSync extends DefaultTask {
255+
static final String TASK_NAME = "bintrayMavenSync"
256+
257+
@Input
258+
Boolean dryRun = true
259+
260+
@Input
261+
String repo
262+
263+
@Input
264+
String pkgName
265+
266+
@Input
267+
String subject
268+
269+
@Input
270+
String versionName
271+
272+
@Input
273+
String apiUrl = 'https://api.bintray.com'
274+
275+
@Input
276+
String bintrayUser
277+
278+
@Input
279+
String bintrayApiKey
280+
281+
@Input
282+
String ossUser
283+
284+
@Input
285+
String ossPassword
286+
287+
@Input
288+
String ossCloseRepo = true
289+
290+
291+
@TaskAction
292+
void taskAction() throws IOException {
293+
mavenCentralSync()
294+
}
295+
296+
297+
private void mavenCentralSync() {
298+
def httpBuilder = BintrayHttpClientFactory.create(apiUrl, bintrayUser, bintrayApiKey)
299+
def pkgPath = "$subject/$repo/$pkgName"
300+
if (dryRun) {
301+
logger.info("(Dry run) Sync to Maven Central performed for '$pkgPath/$versionName'.")
302+
return
303+
}
304+
httpBuilder.request(POST, JSON) {
305+
Utils.addHeaders(headers)
306+
uri.path = "/maven_central_sync/$pkgPath/versions/$versionName"
307+
body = [username: ossUser, password: ossPassword]
308+
if (ossCloseRepo != null) {
309+
body << [close: ossCloseRepo]
310+
}
311+
response.success = { resp ->
312+
logger.info("Sync to Maven Central performed for '$pkgPath/$versionName'.")
313+
}
314+
response.failure = { resp, reader ->
315+
throw new GradleException("Could not sync '$pkgPath/$versionName' to Maven Central: $resp.statusLine $reader")
316+
}
317+
}
318+
}
319+
}

0 commit comments

Comments
 (0)