From 972dafad763890af83a56b58eed792236a284fe9 Mon Sep 17 00:00:00 2001 From: comp500 Date: Mon, 2 Jan 2023 05:46:55 +0000 Subject: [PATCH] Add release automation --- .github/workflows/gradle.yml | 21 ------ .github/workflows/push.yml | 31 +++++++++ .github/workflows/release.yml | 40 ++++++++++++ build.gradle | 120 +++++++++++++++++++++++++++++++--- gradle.properties | 6 ++ 5 files changed, 187 insertions(+), 31 deletions(-) delete mode 100644 .github/workflows/gradle.yml create mode 100644 .github/workflows/push.yml create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml deleted file mode 100644 index 76cf4f7..0000000 --- a/.github/workflows/gradle.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Java CI with Gradle - -on: [ push, pull_request ] - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: Set up JDK 17 - uses: actions/setup-java@v1 - with: - java-version: 17 - - name: Build with Gradle - run: ./gradlew build - - name: Upload build artifacts - uses: actions/upload-artifact@v1 - with: - name: build-artifacts - path: build/libs diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml new file mode 100644 index 0000000..217b4ad --- /dev/null +++ b/.github/workflows/push.yml @@ -0,0 +1,31 @@ +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 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + cache: gradle + - 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 + - name: Upload build artifacts + uses: actions/upload-artifact@v1 + with: + name: build-artifacts + path: build/libs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..6dc8090 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,40 @@ +name: Java Gradle Release + +on: + workflow_dispatch: + inputs: + version: + description: "Version/tag to create" + required: true + type: string + +jobs: + build: + runs-on: ubuntu-latest + environment: + name: release + url: https://modrinth.com/mod/indium + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + cache: gradle + - name: Publish with Gradle + run: ./gradlew publish + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CURSEFORGE_TOKEN: ${{ secrets.CURSEFORGE_TOKEN }} + MODRINTH_TOKEN: ${{ secrets.MODRINTH_TOKEN }} + EXPECTED_VERSION: ${{ inputs.version }} + - 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 \ No newline at end of file diff --git a/build.gradle b/build.gradle index c5fb9ea..91aa312 100644 --- a/build.gradle +++ b/build.gradle @@ -2,23 +2,42 @@ plugins { id 'fabric-loom' version '0.12-SNAPSHOT' id 'maven-publish' - id 'org.ajoberstar.grgit' version '4.1.0' + id "com.github.breadmoirai.github-release" version "2.4.1" + id "org.ajoberstar.grgit" version "4.1.0" + id "com.modrinth.minotaur" version "2.+" + id "com.matthewprenger.cursegradle" version "1.4.0" } sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 -archivesBaseName = project.archives_base_name -if (grgit != null) { - var dirty = grgit.status().clean ? "" : "-dirty" - if (grgit.describe(tags: true) == "${project.mod_version}+mc${project.minecraft_version}") { - version = "${project.mod_version}+mc${project.minecraft_version}${dirty}" +String getGitVersion(Project project) { + if (grgit != null) { + var dirty = grgit.status().clean ? "" : "-dirty" + // If we're going to create a git tag, don't use dev version + var willCreate = System.getenv("EXPECTED_VERSION") !== null && System.getenv("GITHUB_TOKEN") + if (willCreate || grgit.describe(tags: true) == "${project.mod_version}+mc${project.minecraft_version}") { + version = "${project.mod_version}+mc${project.minecraft_version}${dirty}" + } else { + version = "${project.mod_version}-dev.${grgit.head().abbreviatedId}+mc${project.minecraft_version}${dirty}" + } } else { - version = "${project.mod_version}-dev.${grgit.head().abbreviatedId}+mc${project.minecraft_version}${dirty}" + version = "${project.mod_version}-dev.unknown+mc${project.minecraft_version}" } -} else { - version = "${project.mod_version}-dev.unknown+mc${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 = getGitVersion(project) group = project.maven_group repositories { @@ -43,7 +62,7 @@ dependencies { // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. // You may need to force-disable transitiveness on them. - modImplementation "maven.modrinth:sodium:mc1.19.3-0.4.7" + modImplementation "maven.modrinth:sodium:mc${project.minecraft_version}-${project.sodium_version}" } processResources { @@ -96,3 +115,84 @@ publishing { // mavenLocal() } } + +// TODO: infer from fabric.mod.json?! +def supportedVersions = ["1.19.3"] +def verName = "Indium ${project.mod_version} for Minecraft ${project.minecraft_version}/Sodium ${project.sodium_version}" + +// Check version is as expected +if (System.getenv("EXPECTED_VERSION") !== null) { + assert System.getenv("EXPECTED_VERSION") == project.version +} + +publish { + doFirst { + // Require the user to "expect" a version to publish + assert System.getenv("EXPECTED_VERSION") !== null : "EXPECTED_VERSION environment variable must be set to publish" + } +} + +if (System.getenv("GITHUB_TOKEN")) { + assert System.getenv("GITHUB_REF_NAME") + githubRelease { + owner = System.getenv("GITHUB_REPOSITORY_OWNER") + repo = System.getenv("GITHUB_REPOSITORY").split("/", 2)[1] + tagName = project.version + releaseName = verName + targetCommitish = System.getenv("GITHUB_REF_NAME") + draft = false + body = getChangelog(null) + token System.getenv("GITHUB_TOKEN") + releaseAssets.from(remapJar) + } + + publish.dependsOn(tasks.githubRelease) +} + +if (System.getenv("MODRINTH_TOKEN")) { + modrinth { + token = System.getenv("MODRINTH_TOKEN") + projectId = project.modrinth_id + versionNumber = project.version + uploadFile = remapJar + for (version in supportedVersions) { + addGameVersion(version) + } + addLoader("fabric") + addLoader("quilt") + detectLoaders = false + versionName = verName + changelog = getChangelog(project.source_url) + dependencies { + required.version project.sodium_mr_version_id + } + } + + publish.dependsOn(tasks.modrinth) +} + +if (System.getenv("CURSEFORGE_TOKEN")) { + curseforge { + apiKey = System.getenv("CURSEFORGE_TOKEN") + project { + id = project.curseforge_id + releaseType = "release" + + mainArtifact(remapJar) { + displayName = verName + } + for (version in supportedVersions) { + addGameVersion(version) + } + addGameVersion("Fabric") + addGameVersion("Quilt") + changelog = getChangelog(project.source_url) + changelogType = "markdown" + relations { + requiredDependency "sodium" + } + } + } + + publish.dependsOn(tasks.named("curseforge")) +} diff --git a/gradle.properties b/gradle.properties index 532cbd8..e4a4b92 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,3 +12,9 @@ archives_base_name=indium # Dependencies # check this on https://modmuss50.me/fabric.html fabric_version=0.68.1+1.19.3 +sodium_version=0.4.7 +sodium_mr_version_id=ObtU68vj +# Publishing metadata +modrinth_id=Orvt0mRa +curseforge_id=459496 +source_url=https://github.com/comp500/Indium/ \ No newline at end of file