Skip to content

Commit

Permalink
Add CI/CD for deploying to Modrinth/CurseForge/GH releases
Browse files Browse the repository at this point in the history
  • Loading branch information
comp500 committed Dec 4, 2021
1 parent c898249 commit 537b475
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Java CI with Gradle

on: ["push", "pull_request"]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up JDK 16
uses: actions/setup-java@v2
with:
java-version: '16'
distribution: 'adopt'
cache: gradle
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
- name: Cleanup Gradle Cache
# Remove some files from the Gradle cache, so they aren't cached by GitHub Actions.
# Restoring these files from a GitHub Actions cache might cause problems for future builds.
run: |
rm -f ~/.gradle/caches/modules-2/modules-2.lock
rm -f ~/.gradle/caches/modules-2/gc.properties
34 changes: 34 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Java Gradle Release

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up JDK 16
uses: actions/setup-java@v2
with:
java-version: '16'
distribution: 'adopt'
cache: gradle
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Publish with Gradle
run: ./gradlew publish
env:
GITHUB_TOKEN: ${{ Secrets.GITHUB_TOKEN }}
CURSEFORGE_TOKEN: ${{ Secrets.CURSEFORGE_TOKEN }}
MODRINTH_TOKEN: ${{ Secrets.MODRINTH_TOKEN }}
- name: Cleanup Gradle Cache
# Remove some files from the Gradle cache, so they aren't cached by GitHub Actions.
# Restoring these files from a GitHub Actions cache might cause problems for future builds.
run: |
rm -f ~/.gradle/caches/modules-2/modules-2.lock
rm -f ~/.gradle/caches/modules-2/gc.properties
95 changes: 94 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
plugins {
id 'fabric-loom' version '0.8-SNAPSHOT'
id 'maven-publish'

id "com.github.breadmoirai.github-release" version "2.2.12"
id "org.ajoberstar.grgit" version "4.1.0"
id "com.modrinth.minotaur" version "1.2.1"
id "com.matthewprenger.cursegradle" version "1.4.0"
}

sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16

String getGitVersion(Project project) {
if (grgit != null) {
if (grgit.describe(tags: true) == "${project.mod_version}+${project.minecraft_version}") {
return "${project.mod_version}+${project.minecraft_version}"
} else {
var dirty = grgit.status().clean ? "" : "-dirty"
return "${project.mod_version}-dev.${grgit.head().abbreviatedId}+${project.minecraft_version}${dirty}"
}
} else {
return "${project.mod_version}-dev.unknown+${project.minecraft_version}"
}
}

String getChangelog(String githubUrl) {
// Get changes since the last tag
return grgit.log(includes: ["HEAD"], excludes: [
// Get the last tag, removing the number of commits since the tag and the current HEAD~ hash
grgit.describe(commit: "HEAD~", tags: true).replaceAll("-\\d+-[a-z0-9]+\$", "")
]).collect {
"- ${it.shortMessage} (${it.author.name})"
}.join("\n") + (githubUrl == null ? "" : "\n\nSee the full changes on Github: ${githubUrl}commits/${grgit.describe(tags: true)}")
}

archivesBaseName = project.archives_base_name
version = project.mod_version + "+" + project.minecraft_version
version = getGitVersion(project)
group = project.maven_group

repositories {
Expand Down Expand Up @@ -90,3 +118,68 @@ publishing {
// retrieving dependencies.
}
}

if (System.getenv("GITHUB_TOKEN")) {
githubRelease {
owner = project.github_repo_user
repo = project.github_repo_name
tagName = project.mod_version
releaseName = "Release ${project.mod_version}"
targetCommitish = "1.17"
draft = false
body = getChangelog(null)
token System.getenv("GITHUB_TOKEN")
releaseAssets.from(remapJar)
overwrite = true
}

publish.dependsOn(tasks.githubRelease)
}

import com.modrinth.minotaur.TaskModrinthUpload
// TODO: infer from fabric.mod.json?!
def supportedVersions = ["1.17", "1.17.1"]
def versionSupportName = "(Fabric 1.17.x)"

task publishModrinth(type: TaskModrinthUpload) {
dependsOn remapJar
onlyIf {
System.getenv("MODRINTH_TOKEN")
}

token = System.getenv("MODRINTH_TOKEN")
projectId = project.modrinth_id
versionNumber = project.mod_version
uploadFile = remapJar
for (version in supportedVersions) {
addGameVersion(version)
}
addLoader("fabric")
detectLoaders = false
versionName = project.mod_version + " " + versionSupportName
changelog = getChangelog(project.github_url)
}

publish.dependsOn(publishModrinth)

if (System.getenv("CURSEFORGE_TOKEN")) {
curseforge {
apiKey = System.getenv("CURSEFORGE_TOKEN")
project {
id = project.curseforge_id_fabric
releaseType = "release"

mainArtifact(remapJar) {
displayName = project.mod_version + " " + versionSupportName
}
for (version in supportedVersions) {
addGameVersion(version)
}
addGameVersion("Fabric")
changelog = getChangelog(project.github_url)
changelogType = "markdown"
}
}

publish.dependsOn(tasks.named("curseforge"))
}
7 changes: 7 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ org.gradle.jvmargs=-Xmx1G
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.35.2+1.17
modmenu_version=2.0.2

# Publishing metadata
modrinth_id=kYq5qkSL
curseforge_id=310205
github_url=https://github.com/comp500/BorderlessMining
github_repo_user=comp500
github_repo_name=BorderlessMining

0 comments on commit 537b475

Please sign in to comment.