Skip to content

Commit

Permalink
Add github action for publishing (#2739)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment authored Oct 5, 2024
1 parent b036b20 commit 2195954
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 54 deletions.
16 changes: 7 additions & 9 deletions .github/workflows/artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v2
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin
- name: Grant execute permission for gradlew
run: chmod +x gradlew
java-version: 21
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Build Artifacts
uses: gradle/[email protected]
with:
arguments: build
run: ./gradlew build
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
Expand Down
17 changes: 8 additions & 9 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ jobs:
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v2
- name: Checkout sources
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin
- name: Grant execute permission for gradlew
run: chmod +x gradlew
java-version: 21
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Generate documentation directory
uses: gradle/[email protected]
with:
arguments: javadoc
run: ./gradlew javadoc
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Publish to Maven Central

on:
workflow_dispatch:

concurrency:
group: "publish"

jobs:
publish:
environment: maven-release
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Release to Maven Central
env:
ORG_GRADLE_PROJECT_ossrhUser: ${{ secrets.OSSRH_USER }}
ORG_GRADLE_PROJECT_ossrhPassword: ${{ secrets.OSSRH_PASSWORD }}
ORG_GRADLE_PROJECT_stagingProfile: ${{ secrets.STAGING_PROFILE_ID }}
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.GPG_KEY }}
ORG_GRADLE_PROJECT_signingKeyId: ${{ secrets.GPG_KEYID }}
run: ./gradlew rebuild release
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: Artifacts
path: |
build/libs/*.jar
build/libs/*.jar.asc
22 changes: 11 additions & 11 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle

name: Pull Request Validation

on:
Expand All @@ -12,12 +9,15 @@ on:
jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: 11
- uses: gradle/[email protected]
with:
arguments: build test
- name: Checkout sources
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Build and test
run: ./gradlew build test
49 changes: 24 additions & 25 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ plugins {

val javaVersion = JavaVersion.current()
val versionObj = Version(major = "5", minor = "1", revision = "1", classifier = null)
val isGithubAction = System.getProperty("GITHUB_ACTION") != null || System.getenv("GITHUB_ACTION") != null
val isCI = System.getProperty("BUILD_NUMBER") != null // jenkins
|| System.getenv("BUILD_NUMBER") != null
|| System.getProperty("GIT_COMMIT") != null // jitpack
|| System.getenv("GIT_COMMIT") != null
|| System.getProperty("GITHUB_ACTION") != null // Github Actions
|| System.getenv("GITHUB_ACTION") != null
|| isGithubAction // Github Actions

// Check the commit hash and version information
val commitHash: String by lazy {
Expand All @@ -66,10 +66,23 @@ val previousVersion: Version by lazy {
versionObj
}

val isNewVersion = previousVersion != versionObj
val signingKey: String? by project
val signingKeyId: String? by project
val ossrhUser: String? by project
val ossrhPassword: String? by project
val stagingProfile: String? by project

val ossrhConfigured = ossrhUser != null && ossrhPassword != null
val canSign = signingKey != null && signingKeyId != null
val shouldPublish = canSign && ossrhConfigured && isGithubAction

// Use normal version string for new releases and commitHash for other builds
project.version = "$versionObj" + if (isNewVersion) "" else "_$commitHash"
if (shouldPublish) {
project.version = "$versionObj"
} else {
project.version = "${versionObj}_$commitHash"
}

project.group = "net.dv8tion"


Expand Down Expand Up @@ -395,21 +408,17 @@ publishing {
}
}

val ossrhConfigured = getProjectProperty("ossrhUser") != null
val canSign = getProjectProperty("signing.keyId") != null
val shouldPublish = isNewVersion && canSign && ossrhConfigured

if (canSign) {
signing {
sign(publishing.publications.getByName("Release"))
}
signing {
useInMemoryPgpKeys(signingKeyId, signingKey, "")
sign(publishing.publications.getByName("Release"))
isRequired = shouldPublish
}

nexusPublishing {
repositories.sonatype {
username.set(getProjectProperty("ossrhUser"))
password.set(getProjectProperty("ossrhPassword"))
stagingProfileId.set(getProjectProperty("stagingProfileId"))
username.set(ossrhUser)
password.set(ossrhPassword)
stagingProfileId.set(stagingProfile)
}

connectTimeout.set(Duration.ofMinutes(1))
Expand Down Expand Up @@ -452,13 +461,6 @@ val release by tasks.creating(Task::class) {
enabled = shouldPublish

dependsOn(publishingTasks)

doLast { // Only runs when shouldPublish = true
logger.lifecycle("Saving version $versionObj to .version")
val file = layout.projectDirectory.file(".version")
file.asFile.createNewFile()
file.asFile.writeText(versionObj.toString())
}
}

afterEvaluate {
Expand All @@ -476,9 +478,6 @@ afterEvaluate {
// //
////////////////////////////////////


fun getProjectProperty(name: String) = project.properties[name] as? String

fun nullableReplacement(string: String?): String {
return if (string == null) "null"
else "\"$string\""
Expand Down

0 comments on commit 2195954

Please sign in to comment.