Skip to content

Commit 13e719d

Browse files
authored
feat: Auto publish to Github Packages on tag (#31)
1 parent 7c7ca17 commit 13e719d

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

.github/workflows/publish.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
name: Gradle Package
99

1010
on:
11-
release:
12-
types:
13-
- created
11+
push:
12+
tags:
13+
- v*
1414

1515
jobs:
1616
build:
@@ -41,4 +41,4 @@ jobs:
4141
arguments: publish
4242
env:
4343
USERNAME: ${{ github.actor }}
44-
TOKEN: ${{ secrets.GITHUB_TOKEN }}
44+
TOKEN: ${{ secrets.REPO_TOKEN }}

build.gradle.kts

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,35 @@ plugins {
88
`maven-publish`
99
}
1010

11-
val artifact = System.getenv("artifact") ?: "sdk"
12-
group = System.getenv("group") ?: "net.tcgdex"
13-
version = System.getenv("version") ?: "2.0.0"
11+
// from: https://discuss.kotlinlang.org/t/use-git-hash-as-version-number-in-build-gradle-kts/19818/8
12+
fun String.runCommand(
13+
workingDir: File = File("."),
14+
timeoutAmount: Long = 60,
15+
timeoutUnit: TimeUnit = TimeUnit.SECONDS
16+
): String = ProcessBuilder(split("\\s(?=(?:[^'\"`]*(['\"`])[^'\"`]*\\1)*[^'\"`]*$)".toRegex()))
17+
.directory(workingDir)
18+
.redirectOutput(ProcessBuilder.Redirect.PIPE)
19+
.redirectError(ProcessBuilder.Redirect.PIPE)
20+
.start()
21+
.apply { waitFor(timeoutAmount, timeoutUnit) }
22+
.run {
23+
val error = errorStream.bufferedReader().readText().trim()
24+
if (error.isNotEmpty()) {
25+
return@run ""
26+
}
27+
inputStream.bufferedReader().readText().trim()
28+
}
29+
30+
val branch = "git rev-parse --abbrev-ref HEAD".runCommand(workingDir = rootDir)
31+
val tag = "git tag -l --points-at HEAD".runCommand(workingDir = rootDir)
32+
val commitId = "git rev-parse HEAD".runCommand(workingDir = rootDir)
33+
34+
val finalVersion = System.getenv("version") as String? ?: tag.drop(1) ?: "2.0.0"
35+
val finalGroup = System.getenv("group") as String? ?: "net.tcgdex"
36+
val artifact = System.getenv("artifact") as String? ?: "sdk"
37+
38+
group = finalGroup
39+
version = finalVersion
1440

1541
repositories {
1642
mavenCentral()
@@ -56,39 +82,9 @@ val javadocJar = tasks.named<Jar>("javadocJar") {
5682
publishing {
5783
publications {
5884
create<MavenPublication>("maven") {
59-
// groupId = group
60-
artifactId = artifact
61-
// version = ver
62-
63-
from(components["java"])
64-
65-
pom {
66-
name.set("TCGdex SDK")
67-
description.set("Communicate with the Open Source TCGdex API in Kotlin/Java using the SDK")
68-
url.set("https://github.com/tcgdex/java-sdk")
69-
licenses {
70-
license {
71-
name.set("MIT License")
72-
url.set("https://github.com/tcgdex/java-sdk/blob/master/LICENSE.txt")
73-
}
74-
}
75-
developers {
76-
developer {
77-
id.set("avior")
78-
name.set("Avior")
79-
email.set("[email protected]")
80-
}
81-
}
82-
scm {
83-
connection.set("scm:[email protected]:tcgdex/java-sdk.git")
84-
url.set("https://github.com/tcgdex/java-sdk")
85-
}
86-
}
87-
}
88-
register<MavenPublication>("gpr") {
89-
// groupId = group
85+
groupId = finalGroup
9086
artifactId = artifact
91-
// version = ver
87+
version = finalVersion
9288

9389
from(components["java"])
9490

0 commit comments

Comments
 (0)