Skip to content

Commit

Permalink
Add github publish plugin & workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpgr committed Feb 11, 2024
1 parent 4df9554 commit a1f1530
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
if: ${{ runner.os != 'Windows' }}
run: chmod +x ./gradlew
- name: build
run: ./gradlew build modrinth --stacktrace
run: ./gradlew build modrinth github --stacktrace
env:
GITHUB_TOKEN: ${{ secrets.GH_API_KEY }}
MODRINTH_TOKEN: ${{ secrets.MODRINTH_TOKEN }}
47 changes: 46 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import org.ajoberstar.grgit.Grgit
import org.kohsuke.github.GitHub
import org.kohsuke.github.GHReleaseBuilder

buildscript {
dependencies {
classpath("org.kohsuke:github-api:${project.property("github_api_version") as String}")
}
}

plugins {
id("maven-publish")
id("fabric-loom")
id("org.jetbrains.kotlin.jvm")
id("org.ajoberstar.grgit")
id("com.modrinth.minotaur")
}

Expand All @@ -10,7 +21,20 @@ operator fun Project.get(property: String): String {
}

fun getChangeLog(): String {
return "A changelog can be found at https://github.com/johnpgr/$name/commits/"
return "https://github.com/johnpgr/$name/commits/"
}

fun getBranch(): String {
environment["GITHUB_REF"]?.let { branch ->
return branch.substring(branch.lastIndexOf("/") + 1)
}
val grgit = try {
extensions.getByName("grgit") as Grgit
}catch (ignored: Exception) {
return "unknown"
}
val branch = grgit.branch.current().name
return branch.substring(branch.lastIndexOf("/") + 1)
}

val environment: Map<String, String> = System.getenv()
Expand Down Expand Up @@ -84,6 +108,27 @@ tasks.jar {
}
}

//Github publishing
task("github") {
dependsOn(tasks.remapJar)
group = "upload"

onlyIf { environment.containsKey("GITHUB_TOKEN") }

doLast {
val github = GitHub.connectUsingOAuth(environment["GITHUB_TOKEN"])
val repository = github.getRepository(environment["GITHUB_REPOSITORY"])

val releaseBuilder = GHReleaseBuilder(repository, version as String)
releaseBuilder.name(releaseName)
releaseBuilder.body(getChangeLog())
releaseBuilder.commitish(getBranch())

val ghRelease = releaseBuilder.create()
ghRelease.uploadAsset(file(releaseFile), "application/java-archive")
}
}

//Modrinth publishing
modrinth {
environment["MODRINTH_TOKEN"]?.let { token.set(it) }
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ kotlin_version=1.9.22
loom_version=1.5-SNAPSHOT
modrinth_version=2.8.7
github_api_version=1.314
grgit_version=5.0.0

# Fabric Properties
# check these on https://fabricmc.net/develop
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pluginManagement {
plugins {
id("org.jetbrains.kotlin.jvm") version settings["kotlin_version"]
id("fabric-loom") version settings["loom_version"]
id ("org.ajoberstar.grgit") version settings["grgit_version"]
id ("com.modrinth.minotaur") version settings["modrinth_version"]
}
}

0 comments on commit a1f1530

Please sign in to comment.