From f90320bd2b6e942b92b83b39a8bbd0b2bca01bff Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 1 Sep 2021 13:47:02 +0800 Subject: [PATCH] Snapshots publishing (#1465) * Add snapshots publishing * fix workflow * fix * fix * fix * fix * fix * fix * fix * Create tag on releasing snapshots * fix * fix * fix * Publish only when tagged `v*` * MAMOE_TOKEN * fix * fix * Close repository but not publish on release * Ignore documentations for snapshots publishing * Update docs for snapshots * Remove unused newly added files * Disable all shadow tasks on snapshots publishing * Remove concurrency limitation for publish-mirai * fix * fix * fix * Remove publishMiraiCoreArtifactsToMavenLocal --- .github/workflows/release.yml | 30 +++----- .github/workflows/snapshots.yml | 63 +++++++++++++++++ build.gradle.kts | 16 ++--- buildSrc/build.gradle.kts | 5 +- buildSrc/src/main/kotlin/JvmPublishing.kt | 12 ++++ ci-release-helper/build.gradle.kts | 73 ++++++++++++++++++++ docs/ConfiguringProjects.md | 10 +-- docs/UsingSnapshots.md | 83 +++++++++++++++++++++++ mirai-core-all/build.gradle.kts | 4 +- settings.gradle.kts | 5 +- 10 files changed, 263 insertions(+), 38 deletions(-) create mode 100644 .github/workflows/snapshots.yml create mode 100644 docs/UsingSnapshots.md diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cbddb30ca..dacd7ea98 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,19 +1,11 @@ -# This is a basic workflow to help you get started with Actions - name: Release Publish -# Controls when the action will run. Triggers the workflow on push or pull request -# events but only for the master branch on: push: tags: - - '*-dev*' - - '*-release' - - 'v*' + - 'v*' # 正式版本 -# A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: - # This workflow contains a single job called "build" publish-mirai: runs-on: ubuntu-latest steps: @@ -69,39 +61,39 @@ jobs: - name: Gradle :mirai-core-utils:publish run: > - ./gradlew :mirai-core-utils:publish --scan + ./gradlew :mirai-core-utils:publish --scan --scan - name: Gradle :mirai-core-api:publish run: > - ./gradlew :mirai-core-api:publish --scan + ./gradlew :mirai-core-api:publish --scan --scan - name: Gradle :mirai-core:publish run: > - ./gradlew :mirai-core:publish --scan + ./gradlew :mirai-core:publish --scan --scan - name: Gradle :mirai-core-all:publish run: > - ./gradlew :mirai-core-all:publish --info + ./gradlew :mirai-core-all:publish --info --scan - name: Gradle :mirai-console:publish run: > ./gradlew - :mirai-console:publish --info + :mirai-console:publish --info --scan - name: Gradle :mirai-console-terminal:publish run: > ./gradlew - :mirai-console-terminal:publish --info + :mirai-console-terminal:publish --info --scan - name: Gradle :mirai-console-compiler-common:publish run: > ./gradlew - :mirai-console-compiler-common:publish --info + :mirai-console-compiler-common:publish --info --scan - name: Gradle :mirai-console-compiler-annotations:publish run: > ./gradlew - :mirai-console-compiler-annotations:publish --info + :mirai-console-compiler-annotations:publish --info --scan - name: Publish Gradle plugin run: > @@ -110,7 +102,7 @@ jobs: -Dgradle.publish.key=${{ secrets.GRADLE_PUBLISH_KEY }} -Pgradle.publish.key=${{ secrets.GRADLE_PUBLISH_KEY }} -Dgradle.publish.secret=${{ secrets.GRADLE_PUBLISH_SECRET }} -Pgradle.publish.secret=${{ secrets.GRADLE_PUBLISH_SECRET }} - - name: Gradle :ci-release-helper:closeAndReleaseRepository + - name: Gradle :ci-release-helper:closeRepository run: > ./gradlew - :ci-release-helper:closeAndReleaseRepository --info + :ci-release-helper:closeRepository --info --scan diff --git a/.github/workflows/snapshots.yml b/.github/workflows/snapshots.yml new file mode 100644 index 000000000..fd15a4ae1 --- /dev/null +++ b/.github/workflows/snapshots.yml @@ -0,0 +1,63 @@ +name: Snapshots Publish + +on: + push: + branches: + - dev + - snapshots-publishing + paths-ignore: + - 'docs/**' + - '**/README*.md' + +jobs: + publish-mirai: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: git submodule update --init --recursive + - uses: actions/setup-java@v2 + with: + distribution: 'adopt' + java-version: '11' + + - run: chmod -R 777 * + + - name: Get branch name + id: branch-name + uses: tj-actions/branch-names@v4.8 + + - name: Init gradle project + run: ./gradlew clean --scan + + - run: > + ./gradlew updateSnapshotVersion --info --scan + env: + MIRAI_IS_SNAPSHOTS_PUBLISHING: true + SNAPSHOTS_PUBLISHING_USER: ${{ secrets.SNAPSHOTS_PUBLISHING_USER }} + SNAPSHOTS_PUBLISHING_KEY: ${{ secrets.SNAPSHOTS_PUBLISHING_KEY }} + SNAPSHOTS_PUBLISHING_URL: ${{ secrets.SNAPSHOTS_PUBLISHING_URL }} + CURRENT_BRANCH_NAME: dev + + - name: fillBuildConstants + run: > + ./gradlew + fillBuildConstants --scan + + - name: Assemble + run: ./gradlew assemble --scan + + - name: Check + run: > + ./gradlew check --scan + -Dmirai.network.show.all.components=true + -Dkotlinx.coroutines.debug=on + -Dmirai.network.show.packet.details=true + + - run: > + ./gradlew publish --info --scan + env: + MIRAI_IS_SNAPSHOTS_PUBLISHING: true + SNAPSHOTS_PUBLISHING_USER: ${{ secrets.SNAPSHOTS_PUBLISHING_USER }} + SNAPSHOTS_PUBLISHING_KEY: ${{ secrets.SNAPSHOTS_PUBLISHING_KEY }} + SNAPSHOTS_PUBLISHING_URL: ${{ secrets.SNAPSHOTS_PUBLISHING_URL }} + CURRENT_BRANCH_NAME: dev diff --git a/build.gradle.kts b/build.gradle.kts index eca177824..e1e90c3cd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -60,15 +60,6 @@ configure { GpgSigner.setup(project) -tasks.register("publishMiraiCoreArtifactsToMavenLocal") { - group = "mirai" - dependsOn( - project(":mirai-core-api").tasks.getByName("publishToMavenLocal"), - project(":mirai-core-utils").tasks.getByName("publishToMavenLocal"), - project(":mirai-core").tasks.getByName("publishToMavenLocal") - ) -} - analyzes.CompiledCodeVerify.run { registerAllVerifyTasks() } allprojects { @@ -102,6 +93,13 @@ allprojects { configureFlattenSourceSets() } configureJarManifest() + + if (System.getenv("MIRAI_IS_SNAPSHOTS_PUBLISHING") != null) { + project.tasks.filterIsInstance().forEach { shadow -> + shadow.enabled = false // they are too big + } + logger.info("Disabled all shadow tasks.") + } } } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 07769bde5..2bd0a700b 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -26,10 +26,10 @@ kotlin { } +private val versionsText = project.projectDir.resolve("src/main/kotlin/Versions.kt").readText() fun version(name: String): String { - val versions = project.projectDir.resolve("src/main/kotlin/Versions.kt").readText() - return versions.lineSequence() + return versionsText.lineSequence() .map { it.trim() } .single { it.startsWith("const val $name") } .substringAfter('"', "") @@ -52,6 +52,7 @@ dependencies { api("com.github.jengelman.gradle.plugins", "shadow", version("shadow")) api("org.jetbrains.kotlin", "kotlin-gradle-plugin", version("kotlinCompiler")) api("org.jetbrains.kotlin", "kotlin-compiler-embeddable", version("kotlinCompiler")) + api(ktor("client-okhttp", "1.4.3")) api("com.android.tools.build", "gradle", version("androidGradlePlugin")) api(asm("tree")) api(asm("util")) diff --git a/buildSrc/src/main/kotlin/JvmPublishing.kt b/buildSrc/src/main/kotlin/JvmPublishing.kt index 7f3e37b3b..673bc7b96 100644 --- a/buildSrc/src/main/kotlin/JvmPublishing.kt +++ b/buildSrc/src/main/kotlin/JvmPublishing.kt @@ -39,6 +39,18 @@ fun Project.configureRemoteRepos() { // sonatype val keys = SecretKeys.getCache(project) repositories { + if (System.getenv("MIRAI_IS_SNAPSHOTS_PUBLISHING").toBoolean()) { + maven { + name = "MiraiRepo" + setUrl(System.getenv("SNAPSHOTS_PUBLISHING_URL")) + + credentials { + username = System.getenv("SNAPSHOTS_PUBLISHING_USER") + password = System.getenv("SNAPSHOTS_PUBLISHING_KEY") + } + } + } + val sonatype = keys.loadKey("sonatype") if (sonatype.isValid) { maven { diff --git a/ci-release-helper/build.gradle.kts b/ci-release-helper/build.gradle.kts index 3a04e0834..1037ca812 100644 --- a/ci-release-helper/build.gradle.kts +++ b/ci-release-helper/build.gradle.kts @@ -7,6 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ import keys.SecretKeys +import java.io.ByteArrayOutputStream plugins { id("io.codearte.nexus-staging") version "0.22.0" @@ -20,3 +21,75 @@ nexusStaging { username = keys.user password = keys.password } + +tasks.register("updateSnapshotVersion") { + group = "mirai" + + doLast { + rootProject.file("buildSrc/src/main/kotlin/Versions.kt").run { + var text = readText() + check(text.indexOf("project = \"${project.version}\"") != -1) { "Cannot find \"project = \\\"${project.version}\\\"\"" } + text = text.replace("project = \"${project.version}\"", "project = \"${snapshotVersion}\"") + writeText(text) + } + } +} + + +val snapshotVersion by lazy { getSnapshotVersionImpl() } + +fun getSnapshotVersionImpl(): String { + val branch = System.getenv("CURRENT_BRANCH_NAME") + logger.info("Current branch name is '$branch'") + val sha = getSha().trim().take(8) + return "${Versions.project}-$branch-${sha}".also { + logger.info("Snapshot version is '$it'") + } +} + +//tasks.register("createTagOnGitHub") { +// group = "mirai" +// dependsOn(gradle.includedBuild("snapshots-publishing").task(":check")) +// +// doLast { +// val token = System.getenv("MAMOE_TOKEN") +// require(!token.isNullOrBlank()) { "" } +// +// val out = ByteArrayOutputStream() +// exec { +// commandLine("git") +// args("rev-parse", "HEAD") +// standardOutput = out +// workingDir = rootProject.projectDir +// } +// val sha = out.toString() +// logger.info("Current sha is $sha") +// +// runBlocking { +// val resp = HttpClient().post("https://api.github.com/repos/mamoe/mirai/git/refs") { +// header("Authorization", "token $token") +// header("Accept", "application/vnd.github.v3+json") +// body = Gson().toJson( +// mapOf( +// "ref" to "refs/tags/build-$nextVersion", +// "sha" to sha, +// ) +// ) +// } +// logger.info(resp) +// } +// } +//} + +fun getSha(): String { + val out = ByteArrayOutputStream() + exec { + commandLine("git") + args("rev-parse", "HEAD") + standardOutput = out + workingDir = rootProject.projectDir + } + val sha = out.toString() + logger.info("Current commit sha is '$sha'") + return sha +} \ No newline at end of file diff --git a/docs/ConfiguringProjects.md b/docs/ConfiguringProjects.md index ecdd2968b..55b454b43 100644 --- a/docs/ConfiguringProjects.md +++ b/docs/ConfiguringProjects.md @@ -11,11 +11,11 @@ [Version]: https://img.shields.io/maven-central/v/net.mamoe/mirai-core-api.svg?label=Maven%20Central [Central Download]: https://search.maven.org/search?q=net.mamoe%20mirai -| 版本类型 | 版本号 | -|:------:|:------------------------------:| -| 稳定 | 2.7.0 | -| 预览 | - | -| 开发 | - | +| 版本类型 | 版本号 | +|:------:|:--------------------------------------:| +| 稳定 | 2.7.0 | +| 预览 | - | +| 开发 | [UsingSnapshots.md](UsingSnapshots.md) | ### 配置项目 diff --git a/docs/UsingSnapshots.md b/docs/UsingSnapshots.md new file mode 100644 index 000000000..ec2a6fd56 --- /dev/null +++ b/docs/UsingSnapshots.md @@ -0,0 +1,83 @@ +# Mirai - Using Snapshots + +每个 commit 在构建成功后都会发布一个开发测试版本到 mirai 仓库。如有需要,可添加仓库并使用。开发测试版本非常不稳定,仅用于测试某 commit 对一个问题的修复情况,而不建议在生产或开发环境使用。 + +每个开发测试版本只保留一个月。 + +- [在 Maven 使用](#在-maven-使用) +- [在 Gradle 使用](#在-gradle-使用) + +## 在 Maven 使用 + +### 1. 添加 Maven 仓库 + +```xml + + + miraisnapshots + mirai snapshots + https://repo.mirai.mamoe.net/snapshots + + +``` + +### 2. 修改依赖版本 + +1. 选择需要测试的 commit, 找到其 revision id (即 SHA), 取前 8 位, 如 `3cb39c4`. +2. 在该 commit 所属分支的 `buildSrc/src/main/kotlin/Versions.kt` 确认 mirai 主版本号如 `2.8.0-M1`. +3. 得到开发测试版本号 `2.8.0-M1-dev-3cb39c4`. + +```xml + + + net.mamoe + mirai-core-jvm + 2.8.0-M1-dev-3cb39c4 + + +``` + +## 在 Gradle 使用 + + +### 1. 添加 Maven 仓库 + +build.gradle(.kts) +``` +repositories { + maven("https://repo.mirai.mamoe.net/snapshots") +} +``` + +### 2. 修改依赖版本 + +1. 选择需要测试的 commit, 找到其 revision id (即 SHA), 取前 8 位, 如 `3cb39c4`. +2. 在该 commit 所属分支的 `buildSrc/src/main/kotlin/Versions.kt` 确认 mirai 主版本号如 `2.8.0-M1`. +3. 得到开发测试版本号 `2.8.0-M1-dev-3cb39c4`. + +build.gradle(.kts) +``` +dependencies { + implementation("net.mamoe:mirai-core:2.8.0-M1-dev-3cb39c4") +} +``` + +## 使用测试版本 Mirai Console Gradle 插件 + +settings.gradle(.kts) +``` +pluginManagement { + dependencies { + gradlePluginPortal() + maven("https://repo.mirai.mamoe.net/snapshots") + } +} +``` + +plugin.gradle(.kts) +``` +plugins { + // ... + id("net.mamoe.mirai-console") version "2.8.0-M1-dev-3cb39c4" +} +``` \ No newline at end of file diff --git a/mirai-core-all/build.gradle.kts b/mirai-core-all/build.gradle.kts index 3d2215701..b8fee75bb 100644 --- a/mirai-core-all/build.gradle.kts +++ b/mirai-core-all/build.gradle.kts @@ -25,4 +25,6 @@ dependencies { api(project(":mirai-core-utils")) } -configurePublishing("mirai-core-all") \ No newline at end of file +if (!System.getenv("MIRAI_IS_SNAPSHOTS_PUBLISHING").toBoolean()) { + configurePublishing("mirai-core-all") +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 5f7b234d9..cc6706219 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -31,7 +31,6 @@ include(":mirai-core-all") include(":binary-compatibility-validator") include(":binary-compatibility-validator-android") project(":binary-compatibility-validator-android").projectDir = file("binary-compatibility-validator/android") -include(":ci-release-helper") includeProject(":mirai-logging-log4j2", "logging/mirai-logging-log4j2") includeProject(":mirai-logging-slf4j", "logging/mirai-logging-slf4j") @@ -81,4 +80,6 @@ if (isMiraiConsoleCloned()) { If you develop only on mirai-core, it's not compulsory to include mirai-console. """.trimIndent() ) -} \ No newline at end of file +} + +include(":ci-release-helper") \ No newline at end of file