Selaa lähdekoodia

[build] 使用自增序号作为预览版本号 (#2516)

* [build] Use numerical build index numbers for snapshot versions

* use warning

* Use same index for all machines

* fix get

* different indexes for branches

* add docs
Him188 2 vuotta sitten
vanhempi
commit
f6dc4c6b55

+ 7 - 8
.github/workflows/build.yml

@@ -64,7 +64,8 @@ jobs:
           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 # always use -dev for now
+          MIRAI_BUILD_INDEX_AUTH_USERNAME: ${{ secrets.MIRAI_BUILD_INDEX_AUTH_USERNAME }}
+          MIRAI_BUILD_INDEX_AUTH_PASSWORD: ${{ secrets.MIRAI_BUILD_INDEX_AUTH_PASSWORD }}
 
       - name: "Assemble"
         run: ./gradlew assemble ${{ env.gradleArgs }}
@@ -218,7 +219,8 @@ jobs:
           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 # always use -dev for now
+          MIRAI_BUILD_INDEX_AUTH_USERNAME: ${{ secrets.MIRAI_BUILD_INDEX_AUTH_USERNAME }}
+          MIRAI_BUILD_INDEX_AUTH_PASSWORD: ${{ secrets.MIRAI_BUILD_INDEX_AUTH_PASSWORD }}
 
       - name: "Assemble"
         run: ./gradlew assemble ${{ env.gradleArgs }}
@@ -245,7 +247,6 @@ jobs:
           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
 
   build-mirai-core-native:
     name: "Native (${{ matrix.os }})"
@@ -258,7 +259,7 @@ jobs:
           - ubuntu-20.04
           #          - ubuntu-18.04
           # - macos-12
-#          - macos-11
+        #          - macos-11
         include:
           - os: windows-2022
             targetName: mingwX64
@@ -361,7 +362,8 @@ jobs:
           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 # always use -dev for now
+          MIRAI_BUILD_INDEX_AUTH_USERNAME: ${{ secrets.MIRAI_BUILD_INDEX_AUTH_USERNAME }}
+          MIRAI_BUILD_INDEX_AUTH_PASSWORD: ${{ secrets.MIRAI_BUILD_INDEX_AUTH_PASSWORD }}
 
       - name: "Test mirai-core-utils for ${{ matrix.os }}"
         run: ./gradlew :mirai-core-utils:${{ matrix.targetName }}Test ${{ env.gradleArgs }}
@@ -393,7 +395,6 @@ jobs:
           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: Publish LinuxX64 Snapshots
         if: ${{ github.event.pusher && env.isUbuntu == 'true' }}
@@ -403,7 +404,6 @@ jobs:
           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: Publish macOSX64 Snapshots
         if: ${{ github.event.pusher && env.isMac == 'true' }}
@@ -413,5 +413,4 @@ jobs:
           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
 

+ 68 - 7
ci-release-helper/build.gradle.kts

@@ -13,6 +13,7 @@ import java.io.ByteArrayOutputStream
 plugins {
     id("io.codearte.nexus-staging") version "0.22.0"
     kotlin("jvm")
+    kotlin("plugin.serialization")
 }
 
 tasks.register<JavaExec>("runcihelper") {
@@ -66,28 +67,75 @@ nexusStaging {
 }
 
 dependencies {
+    implementation(`ktor-client-okhttp`)
     implementation(`kotlinx-serialization-json`)
+    implementation("org.jetbrains.kotlinx", "kotlinx-datetime", "0.4.0")
 }
 
 tasks.register("updateSnapshotVersion") {
     group = "mirai"
 
+    dependsOn(tasks.compileKotlin)
+    dependsOn(tasks.compileJava)
+
     doLast {
-        setProjectVersionForFutureBuilds(snapshotVersion)
+        val out = ByteArrayOutputStream()
+
+        val sha = getSha()
+        val branch = getCurrentGitBranch()
+
+        val result = javaexec {
+            standardOutput = out
+            classpath(sourceSets.main.get().runtimeClasspath)
+            mainClass.set("cihelper.buildIndex.GetNextSnapshotIndex")
+            args(branch, sha)
+            environment(
+                "mirai.build.index.auth.username",
+                System.getenv("MIRAI_BUILD_INDEX_AUTH_USERNAME")
+                    ?: project.property("mirai.build.index.auth.username")
+
+            )
+            environment(
+                "mirai.build.index.auth.password",
+                System.getenv("MIRAI_BUILD_INDEX_AUTH_PASSWORD")
+                    ?: project.property("mirai.build.index.auth.password")
+            )
+        }
+        result.assertNormalExitValue()
+
+        val resultString = out.toByteArray().decodeToString()
+        val index = resultString
+            .substringAfter("<SNAPSHOT_VERSION_START>", "")
+            .substringBefore("<SNAPSHOT_VERSION_END>", "")
+
+        logger.info("Exec result:")
+        logger.info(resultString)
+
+        if (index.isEmpty()) {
+            throw GradleException("Failed to find version.")
+        }
+
+        logger.info("Snapshot version index is '$index'")
+        val versionName = "${Versions.project}-$branch-${index}"
+
+        // Add annotation on GitHub Actions build
+        // https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-a-notice-message
+        println("::notice ::本 commit 的预览版本号: $versionName    在 https://github.com/mamoe/mirai/blob/dev/docs/UsingSnapshots.md 查看如何使用预览版本")
+
+        setProjectVersionForFutureBuilds(versionName)
     }
 }
 
 tasks.register("publishSnapshotPage") {
     doLast {
-        UpdateSnapshotPage.run(project, getSha())
+        val sha = getSha()
+        logger.info("CommitRef is $sha")
+        UpdateSnapshotPage.run(project, sha)
     }
 }
 
-
-val snapshotVersion by lazy { getSnapshotVersionImpl() }
-
 fun getSnapshotVersionImpl(): String {
-    val branch = System.getenv("CURRENT_BRANCH_NAME")
+    val branch = getCurrentGitBranch()
     logger.info("Current branch name is '$branch'")
     val sha = getSha().trim().take(8)
     return "${Versions.project}-$branch-${sha}".also {
@@ -137,7 +185,20 @@ fun getSha(): String {
         standardOutput = out
         workingDir = rootProject.projectDir
     }
-    val sha = out.toString()
+    val sha = out.toString().trim()
     logger.info("Current commit sha is '$sha'")
     return sha
+}
+
+fun getCurrentGitBranch(): String {
+    val out = ByteArrayOutputStream()
+    exec {
+        commandLine("git")
+        args("rev-parse", "--abbrev-ref", "HEAD")
+        standardOutput = out
+        workingDir = rootProject.projectDir
+    }
+    val sha = out.toString().trim()
+    logger.info("Current branch is '$sha'")
+    return sha
 }

+ 56 - 0
ci-release-helper/src/buildIndex/Index.kt

@@ -0,0 +1,56 @@
+/*
+ * Copyright 2019-2022 Mamoe Technologies and contributors.
+ *
+ * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
+ * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+@file:UseSerializers(UuidAsStringSerializer::class)
+
+package cihelper.buildIndex
+
+import kotlinx.datetime.LocalDateTime
+import kotlinx.serialization.KSerializer
+import kotlinx.serialization.Serializable
+import kotlinx.serialization.UseSerializers
+import kotlinx.serialization.builtins.serializer
+import kotlinx.serialization.descriptors.SerialDescriptor
+import kotlinx.serialization.encoding.Decoder
+import kotlinx.serialization.encoding.Encoder
+import java.util.*
+
+@Serializable
+data class NextIndexResp(
+    val moduleId: UUID,
+    val branchId: UUID,
+    val previousIndexId: UUID?,
+    val previousIndexValue: UInt?,
+    val newIndex: Index
+)
+
+@Serializable
+data class Index(
+    val id: UUID,
+    val branchId: UUID,
+    val commitRef: String,
+    val value: UInt,
+    val date: LocalDateTime
+) {
+    init {
+        require(commitRef.length == 40) { "Invalid commit ref: '$commitRef'" }
+    }
+}
+
+
+object UuidAsStringSerializer : KSerializer<UUID> {
+    override val descriptor: SerialDescriptor = String.serializer().descriptor
+    override fun deserialize(decoder: Decoder): UUID {
+        return UUID.fromString(String.serializer().deserialize(decoder))
+    }
+
+    override fun serialize(encoder: Encoder, value: UUID) {
+        String.serializer().serialize(encoder, value.toString())
+    }
+}

+ 105 - 0
ci-release-helper/src/buildIndex/SnapshotVersions.kt

@@ -0,0 +1,105 @@
+/*
+ * Copyright 2019-2022 Mamoe Technologies and contributors.
+ *
+ * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
+ * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+package cihelper.buildIndex
+
+import io.ktor.client.*
+import io.ktor.client.request.*
+import io.ktor.client.statement.*
+import io.ktor.http.*
+import kotlinx.coroutines.runBlocking
+import kotlinx.serialization.builtins.ListSerializer
+import kotlinx.serialization.json.Json
+
+object GetNextSnapshotIndex {
+    @JvmStatic
+    fun main(args: Array<String>) {
+        val branch = args.getOrNull(0) ?: error("Missing branch argument")
+        val commitRef = args.getOrNull(1) ?: error("Missing commitRef argument")
+
+
+        println("Commit ref is: $commitRef")
+        println("Making request...")
+        HttpClient().use { client ->
+            runBlocking {
+                kotlin.runCatching { client.createBranch(branch = branch) }
+
+                var index = client.getExistingIndex(branch = branch, commitRef = commitRef)
+                if (index == null) {
+                    print("No existing index found. ")
+                    index = client.postNextIndex(branch = branch, commitRef = commitRef)
+                    println("Got new index: $index")
+                } else {
+                    print("Existing index: $index")
+                }
+                println()
+
+                println("<SNAPSHOT_VERSION_START>${index.value}<SNAPSHOT_VERSION_END>")
+            }
+        }
+    }
+}
+
+suspend fun HttpClient.getExistingIndex(
+    module: String = "mirai-core",
+    branch: String,
+    commitRef: String,
+): Index? {
+    // https://build.mirai.mamoe.net/v1/mirai-core/dev/indexes/?commitRef=29121565132bed6e996f3de32faaf49106ae8e39
+    val resp = get("https://build.mirai.mamoe.net/v1/$module/$branch/indexes/") {
+        basicAuth(
+            System.getenv("mirai.build.index.auth.username"),
+            System.getenv("mirai.build.index.auth.password")
+        )
+        parameter("commitRef", commitRef)
+    }
+    if (!resp.status.isSuccess()) {
+        val body = runCatching { resp.bodyAsText() }.getOrNull()
+        throw IllegalStateException("Request failed: ${resp.status}  $body")
+    }
+
+    val body = resp.bodyAsText()
+    if (body.isBlank()) return null
+    return Json.decodeFromString(ListSerializer(Index.serializer()), body).lastOrNull()
+}
+
+suspend fun HttpClient.createBranch(
+    module: String = "mirai-core",
+    branch: String,
+): Boolean {
+    // https://build.mirai.mamoe.net/v1/mirai-core/dev/indexes/?commitRef=29121565132bed6e996f3de32faaf49106ae8e39
+    val resp = put("https://build.mirai.mamoe.net/v1/$module/$branch") {
+        basicAuth(
+            System.getenv("mirai.build.index.auth.username"),
+            System.getenv("mirai.build.index.auth.password")
+        )
+    }
+    return resp.status.isSuccess()
+}
+
+suspend fun HttpClient.postNextIndex(
+    module: String = "mirai-core",
+    branch: String,
+    commitRef: String,
+): Index {
+    val resp = post("https://build.mirai.mamoe.net/v1/$module/$branch/indexes/next") {
+        basicAuth(
+            System.getenv("mirai.build.index.auth.username"),
+            System.getenv("mirai.build.index.auth.password")
+        )
+        parameter("commitRef", commitRef)
+    }
+    if (!resp.status.isSuccess()) {
+        val body = runCatching { resp.bodyAsText() }.getOrNull()
+        throw IllegalStateException("Request failed: ${resp.status}  $body")
+    }
+
+    val body = resp.bodyAsText()
+    return Json.decodeFromString(NextIndexResp.serializer(), body).newIndex
+}

BIN
docs/.images/snapshots-build-jvm.png


BIN
docs/.images/snapshots-find-actions.png


+ 21 - 10
docs/UsingSnapshots.md

@@ -12,6 +12,7 @@
 ### 1. 添加 Maven 仓库
 
 ```xml
+
 <repositories>
     <repository>
         <id>miraisnapshots</id>
@@ -23,26 +24,30 @@
 
 ### 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`.
+1. 选择需要测试的 commit, 在 GitHub 查看其构建状态, 如图所示:  
+   ![](.images/snapshots-find-actions.png)
+2. 点击 "Build / JVM" 右侧的 "Details":  
+   ![](.images/snapshots-build-jvm.png)
+3. 在打开的页面中点击 "Summary", 然后在 "Annotations" 栏目中找到类似 "本 commit 的预览版本号: 2.15.0-build-index-1" 的提示, 得到开发测试版本号 `2.15.0-build-index-1`. 其中, `build-index` 为此 commit 所属分支名, `2.15.0` 意为当前分支的主版本号, `1` 为此分支下的第 1 次成功构建.
+   通常在 `dev` 分支构建的预览版本号类似为 `2.15.0-dev-102`
 
 ```xml
+
 <dependencies>
     <dependency>
         <groupId>net.mamoe</groupId>
         <artifactId>mirai-core-jvm</artifactId>
-        <version>2.8.0-M1-dev-3cb39c4</version>
+        <version>2.15.0-build-index-1</version>
     </dependency>
 </dependencies>
 ```
 
 ## 在 Gradle 使用
 
-
 ### 1. 添加 Maven 仓库
 
 build.gradle(.kts)
+
 ```
 repositories {
    maven("https://repo.mirai.mamoe.net/snapshots") 
@@ -51,20 +56,25 @@ repositories {
 
 ### 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`.
+1. 选择需要测试的 commit, 在 GitHub 查看其构建状态, 如图所示:  
+   ![](.images/snapshots-find-actions.png)
+2. 点击 "Build / JVM" 右侧的 "Details":  
+   ![](.images/snapshots-build-jvm.png)
+3. 在打开的页面中点击 "Summary", 然后在 "Annotations" 栏目中找到类似 "本 commit 的预览版本号: 2.15.0-build-index-1" 的提示, 得到开发测试版本号 `2.15.0-build-index-1`. 其中, `build-index` 为此 commit 所属分支名, `2.15.0` 意为当前分支的主版本号, `1` 为此分支下的第 1 次成功构建.
+   通常在 `dev` 分支构建的预览版本号类似为 `2.15.0-dev-102`
 
 build.gradle(.kts)
+
 ```
 dependencies {
-    implementation("net.mamoe:mirai-core:2.8.0-M1-dev-3cb39c4")
+    implementation("net.mamoe:mirai-core:2.15.0-build-index-1")
 }
 ```
 
 ## 使用测试版本 Mirai Console Gradle 插件
 
 settings.gradle(.kts)
+
 ```
 pluginManagement {
     repositories {
@@ -75,9 +85,10 @@ pluginManagement {
 ```
 
 plugin.gradle(.kts)
+
 ```
 plugins {
     // ...
-    id("net.mamoe.mirai-console") version "2.8.0-M1-dev-3cb39c4"
+    id("net.mamoe.mirai-console") version "2.15.0-build-index-1"
 }
 ```