From fdc0004b7599fc7ce1efaa8395047e00b1c317b4 Mon Sep 17 00:00:00 2001 From: LoJoSho Date: Thu, 2 Feb 2023 15:23:05 -0600 Subject: [PATCH] Repo workflow --- .github/workflows/publish.yml | 29 +++++++++++++++++++ build.gradle.kts | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 00000000..4fc677e7 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,29 @@ +name: Publish to HibiscusMC Repository + +on: + push: + branches: + - master + +jobs: + build: + if: "!contains(github.event.head_commit.message, 'dependabot') && !contains(github.event.head_commit.message, 'nopush')" + environment: build + runs-on: ubuntu-latest + env: + HAS_TOKEN: ${{ secrets.REPO_PASSWORD != '' }} + steps: + - uses: actions/checkout@v2.3.4 + - name: Set up Java + uses: actions/setup-java@v2 + with: + distribution: 'temurin' + java-version: 17 + - name: Build with Gradle + run: ./gradlew api:clean api:build --no-daemon --stacktrace + - name: Publish to Nexus + if: ${{ env.HAS_TOKEN == 'true' }} + run: ./gradlew api:publishMavenPublicationToSkyRepositoryRepository + env: + REPO_USERNAME: ${{ secrets.REPO_USERNAME }} + REPO_PASSWORD: ${{ secrets.REPO_PASSWORD }} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 79b4ee11..7d0c3e21 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -232,6 +232,7 @@ java { publishing { + val publishData = PublishData(project) publications { create("maven") { groupId = "${project.group}" @@ -241,4 +242,57 @@ publishing { from(components["java"]) } } + + repositories { + maven { + authentication { + credentials(PasswordCredentials::class) { + username = System.getenv("REPO_USERNAME") + password = System.getenv("REPO_PASSWORD") + } + } + + name = "HibiscusMCRepository" + url = uri(publishData.getRepository()) + } + } +} + +class PublishData(private val project: Project) { + var type: Type = getReleaseType() + var hashLength: Int = 7 + + private fun getReleaseType(): Type { + val branch = getCheckedOutBranch() + return when { + branch.contentEquals("master") || branch.contentEquals("local") -> Type.RELEASE + branch.startsWith("dev") -> Type.DEV + else -> Type.SNAPSHOT + } + } + + private fun getCheckedOutGitCommitHash(): String = + System.getenv("GITHUB_SHA")?.substring(0, hashLength) ?: "local" + + private fun getCheckedOutBranch(): String = + System.getenv("GITHUB_REF")?.replace("refs/heads/", "") ?: "local" + + fun getVersion(): String = getVersion(false) + + fun getVersion(appendCommit: Boolean): String = + type.append(getVersionString(), appendCommit, getCheckedOutGitCommitHash()) + + private fun getVersionString(): String = + (project.version as String).replace("-SNAPSHOT", "").replace("-DEV", "") + + fun getRepository(): String = type.repo + + enum class Type(private val append: String, val repo: String, private val addCommit: Boolean) { + RELEASE("", "https://repo.hibiscusmc.com//releases/", false), + DEV("-DEV", "https://repo.hibiscusmc.com//development/", true), + SNAPSHOT("-SNAPSHOT", "https://repo.hibiscusmc.com//snapshots/", true); + + fun append(name: String, appendCommit: Boolean, commitHash: String): String = + name.plus(append).plus(if (appendCommit && addCommit) "-".plus(commitHash) else "") + } } \ No newline at end of file