Skip to content

Commit

Permalink
Add publish workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Nov 19, 2024
1 parent ccf9d0d commit 8bfd53e
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 10 deletions.
87 changes: 87 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Publish Release

on:
workflow_dispatch:
inputs:
close_milestone:
description: "Close milestone"
required: true
type: boolean
default: true
upload_backups:
description: "Upload to backups server"
required: true
type: boolean
default: true
publish_github:
description: "Publish to GitHub"
required: true
type: boolean
default: true
publish_curseforge:
description: "Publish to CurseForge"
required: true
type: boolean
default: true
publish_modrinth:
description: "Publish to Modrinth"
required: true
type: boolean
default: true

jobs:
publish:
runs-on: ubuntu-latest
env:
WI_BACKUPS_API_KEY: ${{ secrets.WI_BACKUPS_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CURSEFORGE_API_KEY: ${{ secrets.CURSEFORGE_API_KEY }}
MODRINTH_TOKEN: ${{ secrets.MODRINTH_TOKEN }}

steps:
- uses: actions/checkout@v4

- name: Set up Java 21
uses: actions/setup-java@v4
with:
java-version: "21"
distribution: "microsoft"

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Build
run: ./gradlew build --stacktrace --warning-mode=fail

- name: Create and push tag
run: |
MOD_VERSION=$(grep "mod_version" gradle.properties | cut -d'=' -f2 | tr -d ' ')
git config --global user.name "Wurst-Bot"
git config --global user.email "[email protected]"
git tag $MOD_VERSION
git push origin $MOD_VERSION
- name: Close milestone
if: ${{ inputs.close_milestone }}
run: ./gradlew closeMilestone --stacktrace

- name: Upload backups
if: ${{ inputs.upload_backups }}
run: ./gradlew uploadBackups --stacktrace

- name: Publish to GitHub
if: ${{ inputs.publish_github }}
env:
GITHUB_TOKEN: ${{ secrets.MCX_PUBLISH_TOKEN }}
run: ./gradlew github --stacktrace

- name: Publish to CurseForge
if: ${{ inputs.publish_curseforge }}
run: ./gradlew publishCurseforge --stacktrace

- name: Publish to Modrinth
if: ${{ inputs.publish_modrinth }}
run: ./gradlew publishModrinth --stacktrace
95 changes: 85 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
dependencies {
classpath 'org.kohsuke:github-api:1.326'
classpath "org.kohsuke:github-api:1.326"
}
}

Expand Down Expand Up @@ -72,9 +72,9 @@ java {

jar {
from("LICENSE") {
rename { "${it}_${project.base.archivesName.get()}"}
rename {"${it}_${base.archivesName.get()}"}
}

exclude("/assets/mo_glass/textures/block/*.psd")
}

Expand All @@ -92,17 +92,25 @@ spotless {
}
}

def getGhVersion() {
return "v" + version.substring(0, version.indexOf("-MC"))
}

def getChangelogUrl() {
def modSlug = archives_base_name.toLowerCase()
def versionSlug = version.substring(0, version.indexOf("-MC")).replace(".", "-")
return "https://www.wimods.net/${modSlug}/${modSlug}-${versionSlug}/"
}

publishMods {
file = remapJar.archiveFile
def versionString = project.version as String
def ghVersion = "v" + versionString.substring(0, versionString.indexOf("-"))
def changelogUrl = "https://www.wimods.net/mo-glass/mo-glass-1-10/"
def archivesName = project.base.archivesName.get() as String
def archivesName = base.archivesName.get() as String
additionalFiles.from(
file("${project.buildDir}/libs/${archivesName}-${versionString}-sources.jar"),
file("${project.buildDir}/libs/${archivesName}-${version}-sources.jar"),
)
type = ghVersion.contains("pre") ? BETA : STABLE
type = getGhVersion().contains("pre") ? BETA : STABLE
modLoaders.add("fabric")
def changelogUrl = getChangelogUrl()

curseforge {
projectId = "353426"
Expand Down Expand Up @@ -137,7 +145,7 @@ task github(dependsOn: build) {
doLast {
def github = GitHub.connectUsingOAuth(ENV.GITHUB_TOKEN as String)
def repository = github.getRepository("Wurst-Imperium-MCX/Mo-Glass")
def ghVersion = "v" + version.substring(0, version.indexOf("-"))
def ghVersion = getGhVersion()

def ghRelease = repository.getReleaseByTagName(ghVersion as String)
if(ghRelease == null) {
Expand All @@ -149,3 +157,70 @@ task github(dependsOn: build) {
ghRelease.uploadAsset(remapSourcesJar.archiveFile.get().getAsFile(), "application/java-archive")
}
}

import java.time.LocalDate
import org.kohsuke.github.GHIssueState
import org.kohsuke.github.GHMilestoneState
import java.time.ZoneId

task closeMilestone {
onlyIf {
ENV.GITHUB_TOKEN
}

doLast {
def github = GitHub.connectUsingOAuth(ENV.GITHUB_TOKEN as String)
def repository = github.getRepository("Wurst-Imperium/Mo-Glass")
def ghVersion = getGhVersion()

// Weird API design: listMilestones() requires GHIssueState while everything else uses GHMilestoneState.
def milestone = repository.listMilestones(GHIssueState.ALL).find { it.title == ghVersion }
if (milestone == null) {
milestone = repository.createMilestone(ghVersion, "")
}

if (milestone.getState() != GHMilestoneState.CLOSED) {
milestone.setDueOn(Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()))
milestone.setDescription(getChangelogUrl())
milestone.close()
}
}
}

task uploadBackups {
dependsOn build

onlyIf {
ENV.WI_BACKUPS_API_KEY
}

doLast {
def shortVersion = getGhVersion().substring(1)
def backupUrl = "https://api.wurstclient.net/artifact-backups/Mo-Glass/${shortVersion}"

def connection = new URL(backupUrl).openConnection() as HttpURLConnection
def boundary = UUID.randomUUID().toString()
connection.setRequestMethod("POST")
connection.setRequestProperty("X-API-Key", ENV.WI_BACKUPS_API_KEY)
connection.setRequestProperty("Accept", "application/json")
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=$boundary")
connection.doOutput = true

def output = connection.outputStream
[remapJar, remapSourcesJar].each { jarTask ->
def file = jarTask.archiveFile.get().asFile
output << "--${boundary}\r\n"
output << "Content-Disposition: form-data; name=\"files\"; filename=\"${file.name}\"\r\n"
output << "Content-Type: application/java-archive\r\n\r\n"
file.withInputStream { input ->
output << input
}
output << "\r\n"
}
output << "--${boundary}--\r\n"
output.flush()

if(connection.responseCode != 200)
throw new GradleException("Failed to upload backups: ${connection.responseCode} ${connection.responseMessage}")
}
}

0 comments on commit 8bfd53e

Please sign in to comment.