ソースを参照

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
Him188 4 年 前
コミット
f90320bd2b

+ 11 - 19
.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

+ 63 - 0
.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/[email protected]
+
+      - 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

+ 7 - 9
build.gradle.kts

@@ -60,15 +60,6 @@ configure<kotlinx.validation.ApiValidationExtension> {
 
 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<ShadowJar>().forEach { shadow ->
+                shadow.enabled = false // they are too big
+            }
+            logger.info("Disabled all shadow tasks.")
+        }
     }
 }
 

+ 3 - 2
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"))

+ 12 - 0
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 {

+ 73 - 0
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<String>("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
+}

+ 5 - 5
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) |
 
 ### 配置项目
 

+ 83 - 0
docs/UsingSnapshots.md

@@ -0,0 +1,83 @@
+# Mirai - Using Snapshots
+
+每个 commit 在构建成功后都会发布一个开发测试版本到 mirai 仓库。如有需要,可添加仓库并使用。开发测试版本非常不稳定,仅用于测试某 commit 对一个问题的修复情况,而不建议在生产或开发环境使用。
+
+每个开发测试版本只保留一个月。
+
+- [在 Maven 使用](#在-maven-使用)
+- [在 Gradle 使用](#在-gradle-使用)
+
+## 在 Maven 使用
+
+### 1. 添加 Maven 仓库
+
+```xml
+<repositories>
+    <repository>
+        <id>miraisnapshots</id>
+        <name>mirai snapshots</name>
+        <url>https://repo.mirai.mamoe.net/snapshots</url>
+    </repository>
+</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`.
+
+```xml
+<dependencies>
+    <dependency>
+        <groupId>net.mamoe</groupId>
+        <artifactId>mirai-core-jvm</artifactId>
+        <version>2.8.0-M1-dev-3cb39c4</version>
+    </dependency>
+</dependencies>
+```
+
+## 在 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"
+}
+```

+ 3 - 1
mirai-core-all/build.gradle.kts

@@ -25,4 +25,6 @@ dependencies {
     api(project(":mirai-core-utils"))
 }
 
-configurePublishing("mirai-core-all")
+if (!System.getenv("MIRAI_IS_SNAPSHOTS_PUBLISHING").toBoolean()) {
+    configurePublishing("mirai-core-all")
+}

+ 3 - 2
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()
     )
-}
+}
+
+include(":ci-release-helper")