From 9126cf4b63a439efb5b724d7672525a984ef63ee Mon Sep 17 00:00:00 2001 From: Lukellmann <47486203+Lukellmann@users.noreply.github.com> Date: Sun, 30 Apr 2023 23:24:44 +0200 Subject: [PATCH] Simplify CI (#823) * call Git CLI from Gradle instead of depending on CI specifics * use normal environment variables for signing * remove JitPack support, we publish snapshots for all branches anyway --- .github/workflows/deployment-ci.yml | 10 +---- .github/workflows/docs-ci.yml | 3 -- build.gradle.kts | 2 +- buildSrc/src/main/kotlin/Documentation.kt | 2 +- buildSrc/src/main/kotlin/Git.kt | 13 ++++++ buildSrc/src/main/kotlin/Projects.kt | 40 ++++++------------- .../src/main/kotlin/kord-module.gradle.kts | 4 -- .../main/kotlin/kord-publishing.gradle.kts | 27 ++++++------- common/build.gradle.kts | 6 +-- 9 files changed, 44 insertions(+), 63 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..20f02ea035d 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: @@ -44,10 +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 }} - GITHUB_TAG_NAME: ${{ github.event.release.tag_name }} - GITHUB_BRANCH_NAME: ${{ github.ref }} + SIGNING_KEY: ${{ secrets.signingKey }} + SIGNING_PASSWORD: ${{ secrets.signingPassword }} steps: - uses: actions/checkout@v3 with: @@ -57,8 +53,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..bb86568ec4e 100644 --- a/.github/workflows/docs-ci.yml +++ b/.github/workflows/docs-ci.yml @@ -15,9 +15,6 @@ jobs: concurrency: # Allow one concurrent deployment group: pages 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..321fe3fbbc1 --- /dev/null +++ b/buildSrc/src/main/kotlin/Git.kt @@ -0,0 +1,13 @@ +import org.gradle.api.Project +import java.io.ByteArrayOutputStream + +internal fun Project.git(vararg command: String): String { + val output = ByteArrayOutputStream() + exec { + commandLine("git", *command) + standardOutput = output + errorOutput = output + workingDir = rootDir + }.rethrowFailure().assertNormalExitValue() + return output.toString().trim() +} diff --git a/buildSrc/src/main/kotlin/Projects.kt b/buildSrc/src/main/kotlin/Projects.kt index 77a8c531dd3..63c52546fcc 100644 --- a/buildSrc/src/main/kotlin/Projects.kt +++ b/buildSrc/src/main/kotlin/Projects.kt @@ -1,40 +1,24 @@ -/** - * whether the process has been invoked by JitPack - */ -val isJitPack get() = "true" == System.getenv("JITPACK") +import org.gradle.api.Project 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") +private val Project.tag + get() = git("tag", "--no-column", "--points-at", "HEAD") + .takeIf { it.isNotBlank() } + ?.lines() + ?.single() - val isRelease: Boolean get() = !isSnapshot && !isUndefined +val Project.libraryVersion get() = tag ?: "${git("branch", "--show-current").replace('/', '-')}-SNAPSHOT" - val isUndefined get() = version == "undefined" -} +val Project.commitHash get() = git("rev-parse", "--verify", "HEAD") +val Project.shortCommitHash get() = git("rev-parse", "--short", "HEAD") + +val Project.isRelease get() = tag != null 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..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 { @@ -19,7 +20,7 @@ publishing { groupId = Library.group artifactId = "kord-$artifactId" - version = Library.version + version = libraryVersion pom { name = Library.name @@ -58,27 +59,23 @@ publishing { } } - if (!isJitPack) { - repositories { - maven { - url = uri(if (Library.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 = getenv("NEXUS_USER") + password = getenv("NEXUS_PASSWORD") } } } } -if (!isJitPack && Library.isRelease) { +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) } } 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\"") }