From 48799f6f5e3f96c21f3c17442ae18ea75e9c43bd Mon Sep 17 00:00:00 2001 From: Lukellmann Date: Wed, 26 Apr 2023 20:03:49 +0200 Subject: [PATCH 1/6] Call git CLI for branch and commit hashes --- .github/workflows/deployment-ci.yml | 5 --- .github/workflows/docs-ci.yml | 1 - build.gradle.kts | 2 +- buildSrc/src/main/kotlin/Documentation.kt | 2 +- buildSrc/src/main/kotlin/Git.kt | 12 ++++++ buildSrc/src/main/kotlin/Projects.kt | 39 +++++++------------ .../src/main/kotlin/kord-module.gradle.kts | 4 -- .../main/kotlin/kord-publishing.gradle.kts | 6 +-- common/build.gradle.kts | 6 +-- 9 files changed, 35 insertions(+), 42 deletions(-) create mode 100644 buildSrc/src/main/kotlin/Git.kt diff --git a/.github/workflows/deployment-ci.yml b/.github/workflows/deployment-ci.yml index 387731fe16b..acee03fe2b1 100644 --- a/.github/workflows/deployment-ci.yml +++ b/.github/workflows/deployment-ci.yml @@ -28,8 +28,6 @@ jobs: with: distribution: temurin java-version: 8 - - name: Set SHORT_SHA environment variable to short commit hash - run: echo "SHORT_SHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV - name: Build with Gradle uses: gradle/gradle-build-action@v2 with: @@ -47,7 +45,6 @@ jobs: ORG_GRADLE_PROJECT_signingKey: ${{ secrets.signingKey }} ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.signingPassword }} GITHUB_TAG_NAME: ${{ github.event.release.tag_name }} - GITHUB_BRANCH_NAME: ${{ github.ref }} steps: - uses: actions/checkout@v3 with: @@ -57,8 +54,6 @@ jobs: with: distribution: temurin java-version: 8 - - name: Set SHORT_SHA environment variable to short commit hash - run: echo "SHORT_SHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV - name: Build with Gradle uses: gradle/gradle-build-action@v2 with: diff --git a/.github/workflows/docs-ci.yml b/.github/workflows/docs-ci.yml index ce1b70fb5fa..a171be53dc1 100644 --- a/.github/workflows/docs-ci.yml +++ b/.github/workflows/docs-ci.yml @@ -17,7 +17,6 @@ jobs: cancel-in-progress: true env: GITHUB_TAG_NAME: ${{ github.event.release.tag_name }} - GITHUB_BRANCH_NAME: ${{ github.ref }} steps: - uses: actions/checkout@v3 - name: Set up JDK diff --git a/build.gradle.kts b/build.gradle.kts index a1cd46774e8..0ad64bff26f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,4 +7,4 @@ repositories { } group = Library.group -version = Library.version +version = libraryVersion diff --git a/buildSrc/src/main/kotlin/Documentation.kt b/buildSrc/src/main/kotlin/Documentation.kt index f75e9fb8c3f..090ed8310b6 100644 --- a/buildSrc/src/main/kotlin/Documentation.kt +++ b/buildSrc/src/main/kotlin/Documentation.kt @@ -16,7 +16,7 @@ fun AbstractDokkaLeafTask.applyKordDokkaOptions() { sourceLink { localDirectory = project.projectDir - remoteUrl = URL("https://github.com/kordlib/kord/blob/${Library.commitHashOrDefault("0.9.x")}/${project.name}") + remoteUrl = URL("https://github.com/kordlib/kord/blob/${project.commitHash}/${project.name}") remoteLineSuffix = "#L" } diff --git a/buildSrc/src/main/kotlin/Git.kt b/buildSrc/src/main/kotlin/Git.kt new file mode 100644 index 00000000000..4c49c0fe737 --- /dev/null +++ b/buildSrc/src/main/kotlin/Git.kt @@ -0,0 +1,12 @@ +import org.gradle.api.Project + +internal fun Project.git(vararg command: String): String { + val process = ProcessBuilder("git", *command) + .redirectErrorStream(true) + .directory(rootDir) + .start() + val output = process.inputStream.reader().use { it.readText() }.trim() + val exitStatus = process.waitFor() + check(exitStatus == 0) { "git exited with status $exitStatus, output:\n$output" } + return output +} diff --git a/buildSrc/src/main/kotlin/Projects.kt b/buildSrc/src/main/kotlin/Projects.kt index 77a8c531dd3..131703d8932 100644 --- a/buildSrc/src/main/kotlin/Projects.kt +++ b/buildSrc/src/main/kotlin/Projects.kt @@ -1,3 +1,5 @@ +import org.gradle.api.Project + /** * whether the process has been invoked by JitPack */ @@ -6,35 +8,24 @@ val isJitPack get() = "true" == System.getenv("JITPACK") object Library { const val name = "kord" const val group = "dev.kord" - val version: String - get() = if (isJitPack) System.getenv("RELEASE_TAG") - else { - val tag = System.getenv("GITHUB_TAG_NAME") - val branch = System.getenv("GITHUB_BRANCH_NAME") - when { - !tag.isNullOrBlank() -> tag - !branch.isNullOrBlank() && branch.startsWith("refs/heads/") -> - branch.substringAfter("refs/heads/").replace("/", "-") + "-SNAPSHOT" - else -> "undefined" - } - - } - - val commitHash get() = System.getenv("GITHUB_SHA") ?: "unknown" - fun commitHashOrDefault(default: String) = System.getenv("GITHUB_SHA") ?: default - - // this environment variable isn't available out of the box, we set it ourselves - val shortCommitHash get() = System.getenv("SHORT_SHA") ?: "unknown" - const val description = "Idiomatic Kotlin Wrapper for The Discord API" const val projectUrl = "https://github.com/kordlib/kord" +} - val isSnapshot: Boolean get() = version.endsWith("-SNAPSHOT") - val isRelease: Boolean get() = !isSnapshot && !isUndefined +val Project.libraryVersion: String + get() = if (isJitPack) { + System.getenv("RELEASE_TAG") + } else { + val tag = System.getenv("GITHUB_TAG_NAME").takeUnless { it.isNullOrBlank() } + tag ?: "${git("branch", "--show-current").replace('/', '-')}-SNAPSHOT" + } - val isUndefined get() = version == "undefined" -} +val Project.commitHash get() = git("rev-parse", "HEAD") +val Project.shortCommitHash get() = git("rev-parse", "--short", "HEAD") + +val Project.isSnapshot get() = libraryVersion.endsWith("-SNAPSHOT") +val Project.isRelease get() = !isSnapshot object Repo { const val releasesUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" diff --git a/buildSrc/src/main/kotlin/kord-module.gradle.kts b/buildSrc/src/main/kotlin/kord-module.gradle.kts index 1c7608680a4..6c97b0afac8 100644 --- a/buildSrc/src/main/kotlin/kord-module.gradle.kts +++ b/buildSrc/src/main/kotlin/kord-module.gradle.kts @@ -51,10 +51,6 @@ tasks { withType().configureEach { applyKordDokkaOptions() } - - withType().configureEach { - doFirst { require(!Library.isUndefined) { "No release/snapshot version found." } } - } } publishing { diff --git a/buildSrc/src/main/kotlin/kord-publishing.gradle.kts b/buildSrc/src/main/kotlin/kord-publishing.gradle.kts index 546502d228b..2109c6f42ab 100644 --- a/buildSrc/src/main/kotlin/kord-publishing.gradle.kts +++ b/buildSrc/src/main/kotlin/kord-publishing.gradle.kts @@ -19,7 +19,7 @@ publishing { groupId = Library.group artifactId = "kord-$artifactId" - version = Library.version + version = libraryVersion pom { name = Library.name @@ -61,7 +61,7 @@ publishing { if (!isJitPack) { repositories { maven { - url = uri(if (Library.isSnapshot) Repo.snapshotsUrl else Repo.releasesUrl) + url = uri(if (isSnapshot) Repo.snapshotsUrl else Repo.releasesUrl) credentials { username = System.getenv("NEXUS_USER") @@ -72,7 +72,7 @@ publishing { } } -if (!isJitPack && Library.isRelease) { +if (!isJitPack && isRelease) { signing { val signingKey = findProperty("signingKey")?.toString() val signingPassword = findProperty("signingPassword")?.toString() diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 624f44ac9af..1d592ab41f0 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -60,7 +60,7 @@ buildConfig { internalVisibility = true } - buildConfigField("String", "BUILD_CONFIG_GENERATED_LIBRARY_VERSION", "\"${Library.version}\"") - buildConfigField("String", "BUILD_CONFIG_GENERATED_COMMIT_HASH", "\"${Library.commitHash}\"") - buildConfigField("String", "BUILD_CONFIG_GENERATED_SHORT_COMMIT_HASH", "\"${Library.shortCommitHash}\"") + buildConfigField("String", "BUILD_CONFIG_GENERATED_LIBRARY_VERSION", "\"$libraryVersion\"") + buildConfigField("String", "BUILD_CONFIG_GENERATED_COMMIT_HASH", "\"$commitHash\"") + buildConfigField("String", "BUILD_CONFIG_GENERATED_SHORT_COMMIT_HASH", "\"$shortCommitHash\"") } From 1c0a7a8fe30cc4f39e3970a16d93693cc7a8caa4 Mon Sep 17 00:00:00 2001 From: Lukellmann Date: Wed, 26 Apr 2023 23:46:29 +0200 Subject: [PATCH 2/6] Remove JitPack support We publish snapshots for every branch anyway. --- buildSrc/src/main/kotlin/Projects.kt | 15 +++------------ .../src/main/kotlin/kord-publishing.gradle.kts | 16 +++++++--------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/buildSrc/src/main/kotlin/Projects.kt b/buildSrc/src/main/kotlin/Projects.kt index 131703d8932..fee29439195 100644 --- a/buildSrc/src/main/kotlin/Projects.kt +++ b/buildSrc/src/main/kotlin/Projects.kt @@ -1,10 +1,5 @@ import org.gradle.api.Project -/** - * whether the process has been invoked by JitPack - */ -val isJitPack get() = "true" == System.getenv("JITPACK") - object Library { const val name = "kord" const val group = "dev.kord" @@ -12,20 +7,16 @@ object Library { const val projectUrl = "https://github.com/kordlib/kord" } - val Project.libraryVersion: String - get() = if (isJitPack) { - System.getenv("RELEASE_TAG") - } else { + get() { val tag = System.getenv("GITHUB_TAG_NAME").takeUnless { it.isNullOrBlank() } - tag ?: "${git("branch", "--show-current").replace('/', '-')}-SNAPSHOT" + return tag ?: "${git("branch", "--show-current").replace('/', '-')}-SNAPSHOT" } val Project.commitHash get() = git("rev-parse", "HEAD") val Project.shortCommitHash get() = git("rev-parse", "--short", "HEAD") -val Project.isSnapshot get() = libraryVersion.endsWith("-SNAPSHOT") -val Project.isRelease get() = !isSnapshot +val Project.isRelease get() = !libraryVersion.endsWith("-SNAPSHOT") object Repo { const val releasesUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" diff --git a/buildSrc/src/main/kotlin/kord-publishing.gradle.kts b/buildSrc/src/main/kotlin/kord-publishing.gradle.kts index 2109c6f42ab..00e6b7d10ff 100644 --- a/buildSrc/src/main/kotlin/kord-publishing.gradle.kts +++ b/buildSrc/src/main/kotlin/kord-publishing.gradle.kts @@ -58,21 +58,19 @@ publishing { } } - if (!isJitPack) { - repositories { - maven { - url = uri(if (isSnapshot) Repo.snapshotsUrl else Repo.releasesUrl) + repositories { + maven { + url = uri(if (isRelease) Repo.releasesUrl else Repo.snapshotsUrl) - credentials { - username = System.getenv("NEXUS_USER") - password = System.getenv("NEXUS_PASSWORD") - } + credentials { + username = System.getenv("NEXUS_USER") + password = System.getenv("NEXUS_PASSWORD") } } } } -if (!isJitPack && isRelease) { +if (isRelease) { signing { val signingKey = findProperty("signingKey")?.toString() val signingPassword = findProperty("signingPassword")?.toString() From d52a5b63f28947addb2599448baa318086335e5b Mon Sep 17 00:00:00 2001 From: Lukellmann Date: Thu, 27 Apr 2023 00:07:04 +0200 Subject: [PATCH 3/6] Use regular environment variables for signing --- .github/workflows/deployment-ci.yml | 4 ++-- buildSrc/src/main/kotlin/kord-publishing.gradle.kts | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.github/workflows/deployment-ci.yml b/.github/workflows/deployment-ci.yml index acee03fe2b1..0876a394d95 100644 --- a/.github/workflows/deployment-ci.yml +++ b/.github/workflows/deployment-ci.yml @@ -42,8 +42,8 @@ jobs: KORD_TEST_TOKEN: ${{ secrets.KORD_TEST_TOKEN }} NEXUS_USER: ${{ secrets.NEXUS_USER }} NEXUS_PASSWORD: ${{ secrets.NEXUS_PASSWORD }} - ORG_GRADLE_PROJECT_signingKey: ${{ secrets.signingKey }} - ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.signingPassword }} + SIGNING_KEY: ${{ secrets.signingKey }} + SIGNING_PASSWORD: ${{ secrets.signingPassword }} GITHUB_TAG_NAME: ${{ github.event.release.tag_name }} steps: - uses: actions/checkout@v3 diff --git a/buildSrc/src/main/kotlin/kord-publishing.gradle.kts b/buildSrc/src/main/kotlin/kord-publishing.gradle.kts index 00e6b7d10ff..30e9c7a19ac 100644 --- a/buildSrc/src/main/kotlin/kord-publishing.gradle.kts +++ b/buildSrc/src/main/kotlin/kord-publishing.gradle.kts @@ -1,3 +1,4 @@ +import java.lang.System.getenv import java.util.Base64 plugins { @@ -63,8 +64,8 @@ publishing { url = uri(if (isRelease) Repo.releasesUrl else Repo.snapshotsUrl) credentials { - username = System.getenv("NEXUS_USER") - password = System.getenv("NEXUS_PASSWORD") + username = getenv("NEXUS_USER") + password = getenv("NEXUS_PASSWORD") } } } @@ -72,11 +73,9 @@ publishing { if (isRelease) { signing { - val signingKey = findProperty("signingKey")?.toString() - val signingPassword = findProperty("signingPassword")?.toString() - if (signingKey != null && signingPassword != null) { - useInMemoryPgpKeys(String(Base64.getDecoder().decode(signingKey)), signingPassword) - } + val secretKey = String(Base64.getDecoder().decode(getenv("SIGNING_KEY"))) + val password = getenv("SIGNING_PASSWORD") + useInMemoryPgpKeys(secretKey, password) sign(publishing.publications) } } From 5381371db9b2e4798a2262687e28c97ac18ded72 Mon Sep 17 00:00:00 2001 From: Lukellmann Date: Fri, 28 Apr 2023 01:29:57 +0200 Subject: [PATCH 4/6] Use exec instead of ProcessBuilder --- buildSrc/src/main/kotlin/Git.kt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/buildSrc/src/main/kotlin/Git.kt b/buildSrc/src/main/kotlin/Git.kt index 4c49c0fe737..321fe3fbbc1 100644 --- a/buildSrc/src/main/kotlin/Git.kt +++ b/buildSrc/src/main/kotlin/Git.kt @@ -1,12 +1,13 @@ import org.gradle.api.Project +import java.io.ByteArrayOutputStream internal fun Project.git(vararg command: String): String { - val process = ProcessBuilder("git", *command) - .redirectErrorStream(true) - .directory(rootDir) - .start() - val output = process.inputStream.reader().use { it.readText() }.trim() - val exitStatus = process.waitFor() - check(exitStatus == 0) { "git exited with status $exitStatus, output:\n$output" } - return output + val output = ByteArrayOutputStream() + exec { + commandLine("git", *command) + standardOutput = output + errorOutput = output + workingDir = rootDir + }.rethrowFailure().assertNormalExitValue() + return output.toString().trim() } From db082dcc1b27c5ff08afa6e563944dd090d16416 Mon Sep 17 00:00:00 2001 From: Lukellmann Date: Sat, 29 Apr 2023 16:42:40 +0200 Subject: [PATCH 5/6] Add --verify --- buildSrc/src/main/kotlin/Projects.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildSrc/src/main/kotlin/Projects.kt b/buildSrc/src/main/kotlin/Projects.kt index fee29439195..12264e1a1e8 100644 --- a/buildSrc/src/main/kotlin/Projects.kt +++ b/buildSrc/src/main/kotlin/Projects.kt @@ -13,7 +13,7 @@ val Project.libraryVersion: String return tag ?: "${git("branch", "--show-current").replace('/', '-')}-SNAPSHOT" } -val Project.commitHash get() = git("rev-parse", "HEAD") +val Project.commitHash get() = git("rev-parse", "--verify", "HEAD") val Project.shortCommitHash get() = git("rev-parse", "--short", "HEAD") val Project.isRelease get() = !libraryVersion.endsWith("-SNAPSHOT") From e281bb9404efef9d848db942ceb97cc48008ceba Mon Sep 17 00:00:00 2001 From: Lukellmann Date: Sun, 30 Apr 2023 22:43:44 +0200 Subject: [PATCH 6/6] Call Git CLI for tag --- .github/workflows/deployment-ci.yml | 1 - .github/workflows/docs-ci.yml | 2 -- buildSrc/src/main/kotlin/Projects.kt | 14 ++++++++------ 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.github/workflows/deployment-ci.yml b/.github/workflows/deployment-ci.yml index 0876a394d95..20f02ea035d 100644 --- a/.github/workflows/deployment-ci.yml +++ b/.github/workflows/deployment-ci.yml @@ -44,7 +44,6 @@ jobs: NEXUS_PASSWORD: ${{ secrets.NEXUS_PASSWORD }} SIGNING_KEY: ${{ secrets.signingKey }} SIGNING_PASSWORD: ${{ secrets.signingPassword }} - GITHUB_TAG_NAME: ${{ github.event.release.tag_name }} steps: - uses: actions/checkout@v3 with: diff --git a/.github/workflows/docs-ci.yml b/.github/workflows/docs-ci.yml index a171be53dc1..bb86568ec4e 100644 --- a/.github/workflows/docs-ci.yml +++ b/.github/workflows/docs-ci.yml @@ -15,8 +15,6 @@ jobs: concurrency: # Allow one concurrent deployment group: pages cancel-in-progress: true - env: - GITHUB_TAG_NAME: ${{ github.event.release.tag_name }} steps: - uses: actions/checkout@v3 - name: Set up JDK diff --git a/buildSrc/src/main/kotlin/Projects.kt b/buildSrc/src/main/kotlin/Projects.kt index 12264e1a1e8..63c52546fcc 100644 --- a/buildSrc/src/main/kotlin/Projects.kt +++ b/buildSrc/src/main/kotlin/Projects.kt @@ -7,16 +7,18 @@ object Library { const val projectUrl = "https://github.com/kordlib/kord" } -val Project.libraryVersion: String - get() { - val tag = System.getenv("GITHUB_TAG_NAME").takeUnless { it.isNullOrBlank() } - return tag ?: "${git("branch", "--show-current").replace('/', '-')}-SNAPSHOT" - } +private val Project.tag + get() = git("tag", "--no-column", "--points-at", "HEAD") + .takeIf { it.isNotBlank() } + ?.lines() + ?.single() + +val Project.libraryVersion get() = tag ?: "${git("branch", "--show-current").replace('/', '-')}-SNAPSHOT" val Project.commitHash get() = git("rev-parse", "--verify", "HEAD") val Project.shortCommitHash get() = git("rev-parse", "--short", "HEAD") -val Project.isRelease get() = !libraryVersion.endsWith("-SNAPSHOT") +val Project.isRelease get() = tag != null object Repo { const val releasesUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"