Skip to content

Commit

Permalink
Upload distributions to B2
Browse files Browse the repository at this point in the history
  • Loading branch information
octylFractal committed Jul 29, 2024
1 parent 9557e8d commit 1d268df
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 20 deletions.
24 changes: 10 additions & 14 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -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/**'

3 changes: 3 additions & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JavaPluginExtension> {
Expand Down
59 changes: 59 additions & 0 deletions build-logic/src/main/kotlin/org/enginehub/gradle/b2/B2Upload.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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<String>

/**
* The prefix to use for the files.
*/
@get:Input
abstract val prefix: Property<String>

@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")
val prefixValue = prefix.get()
inputDir.asFileTree.visit {
if (isDirectory) {
return@visit
}
logger.lifecycle("Uploading $path to $prefixValue/$path")
client.uploadSmallFile(
B2UploadFileRequest.builder(
bucketId,
"$prefixValue/$path",
B2ContentTypes.APPLICATION_OCTET,
B2FileContentSource.build(file)
).build()
)
}
}
}
24 changes: 18 additions & 6 deletions gui/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.enginehub.gradle.b2.B2Upload

plugins {
java
application
Expand Down Expand Up @@ -94,6 +96,13 @@ tasks.named<JavaExec>("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
Expand All @@ -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<B2Upload>("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" }
}

0 comments on commit 1d268df

Please sign in to comment.