From af0ad5e96198adc61738b6c8453676b8809bc95e Mon Sep 17 00:00:00 2001 From: Octavia Togami Date: Sun, 28 Jul 2024 21:40:53 -0700 Subject: [PATCH] Upload distributions to B2 --- .github/workflows/gradle.yml | 24 ++++---- build-logic/build.gradle.kts | 3 + .../org/enginehub/gradle/b2/B2Upload.kt | 58 +++++++++++++++++++ gui/build.gradle.kts | 24 ++++++-- 4 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 build-logic/src/main/kotlin/org/enginehub/gradle/b2/B2Upload.kt diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index e1f5815..5af8de7 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -35,6 +35,7 @@ jobs: path: '**/build/reports/**' build-all-distributions: + if: ${{ github.event_name == 'push' }} name: Build lin-bus NBT Editor distribtions on ${{ matrix.os }} runs-on: ${{ matrix.os }} needs: [ build ] @@ -56,22 +57,17 @@ jobs: - name: Setup Gradle uses: gradle/actions/setup-gradle@v3 - - name: Build jpackage distributions - run: ./gradlew jpackage -s + - name: Publish distributions + env: + B2_APPLICATION_KEY_ID: ${{ secrets.B2_KEY_ID }} + B2_APPLICATION_KEY: ${{ secrets.B2_APPLICATION_KEY }} + B2_BUCKET_NAME: octy-enginehub + B2_PREFIX: > + github.ref_name == github.event.repository.default_branch && + 'lin-bus-distributions' || format('lin-bus-distributions/{0}', github.ref_name) + run: ./gradlew :gui:uploadDistributions -s - name: "Kill Daemons Because Apparently GitHub Won't Do It For You" run: ./gradlew --stop - - uses: actions/upload-artifact@v4 - name: Archive Distributions - with: - name: distributions for ${{ matrix.os }} - path: 'gui/build/jpackage/*.*' - - - uses: actions/upload-artifact@v4 - name: Archive Reports - if: always() - with: - name: reports for ${{ matrix.os }} - path: '**/build/reports/**' diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index 54a4aa0..bb5d176 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -13,6 +13,9 @@ dependencies { implementation(gradleApi()) implementation("gradle.plugin.org.cadixdev.gradle:licenser:0.6.1") implementation("org.jfrog.buildinfo:build-info-extractor-gradle:4.33.1") + val b2Version = "6.2.1" + implementation("com.backblaze.b2:b2-sdk-core:$b2Version") + implementation("com.backblaze.b2:b2-sdk-httpclient:$b2Version") } configure { diff --git a/build-logic/src/main/kotlin/org/enginehub/gradle/b2/B2Upload.kt b/build-logic/src/main/kotlin/org/enginehub/gradle/b2/B2Upload.kt new file mode 100644 index 0000000..eb4cca8 --- /dev/null +++ b/build-logic/src/main/kotlin/org/enginehub/gradle/b2/B2Upload.kt @@ -0,0 +1,58 @@ +package org.enginehub.gradle.b2 + +import com.backblaze.b2.client.B2StorageClient +import com.backblaze.b2.client.B2StorageClientFactory +import com.backblaze.b2.client.contentSources.B2ContentTypes +import com.backblaze.b2.client.contentSources.B2FileContentSource +import com.backblaze.b2.client.structures.B2UploadFileRequest +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputDirectory +import org.gradle.api.tasks.TaskAction +import org.gradle.work.DisableCachingByDefault + +@DisableCachingByDefault(because = "Upload cannot be cached") +abstract class B2Upload : DefaultTask() { + /** + * The directory to upload. + */ + @get:InputDirectory + abstract val inputDir: DirectoryProperty + + /** + * The bucket to upload to. + */ + @get:Input + abstract val bucketName: Property + + /** + * The prefix to use for the files. + */ + @get:Input + abstract val prefix: Property + + @TaskAction + fun upload() { + val client: B2StorageClient = B2StorageClientFactory.createDefaultFactory().create( + "enginehub-b2-upload", + ) + val bucketId = client.getBucketOrNullByName(bucketName.get())?.bucketId + ?: error("Bucket ${bucketName.get()} not found") + inputDir.asFileTree.visit { + if (isDirectory) { + return@visit + } + logger.lifecycle("Uploading $path to $prefix/$path") + client.uploadSmallFile( + B2UploadFileRequest.builder( + bucketId, + "$prefix/$path", + B2ContentTypes.APPLICATION_OCTET, + B2FileContentSource.build(file) + ).build() + ) + } + } +} diff --git a/gui/build.gradle.kts b/gui/build.gradle.kts index 67d2022..07cb1b2 100644 --- a/gui/build.gradle.kts +++ b/gui/build.gradle.kts @@ -1,3 +1,5 @@ +import org.enginehub.gradle.b2.B2Upload + plugins { java application @@ -94,6 +96,13 @@ tasks.named("run") { jvmArgs("-XX:+EnableDynamicAgentLoading") } +// Separate version system since packages require it to be major.minor.build only +val runNumber = providers.environmentVariable("GITHUB_RUN_NUMBER") + .orElse("0") + .map { it.toInt() } + .get() +val appVersionValue = "1.0.$runNumber" + jlink { moduleName = "org.enginehub.linbus.gui" mainClass = mainClassValue @@ -109,11 +118,14 @@ jlink { jpackage { options = listOf("--verbose") installerOptions = listOf("--verbose") - // Separate version system since packages require it to be major.minor.build only - val runNumber = providers.environmentVariable("GITHUB_RUN_NUMBER") - .orElse("0") - .map { it.toInt() } - .get() - appVersion = "1.0.$runNumber" + appVersion = appVersionValue + installerOutputDir = layout.buildDirectory.dir("installers").get().asFile } } + +tasks.register("uploadDistributions") { + dependsOn(tasks.jpackage) + inputDir = tasks.jpackage.map { it.jpackageData.installerOutputDir } + bucketName = providers.environmentVariable("B2_BUCKET_NAME") + prefix = providers.environmentVariable("B2_PREFIX").map { "$it/$appVersionValue" } +}