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 f31b276
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 20 deletions.
21 changes: 7 additions & 14 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,15 @@ 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: lin-bus-distributions
run: ./gradlew :gui:publishDistributions -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/**'

4 changes: 4 additions & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ 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")
implementation("com.google.guava:guava:33.2.1-jre")
}

configure<JavaPluginExtension> {
Expand Down
60 changes: 60 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,60 @@
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 com.google.common.hash.Hashing
import com.google.common.io.Files
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",
)
inputDir.asFileTree.visit {
if (isDirectory) {
return@visit
}
logger.lifecycle("Uploading $path to $prefix/$path")
@Suppress("DEPRECATION")
val sha1 = Files.asByteSource(file).hash(Hashing.sha1())
client.uploadSmallFile(
B2UploadFileRequest.builder(
bucketName.get(),
"$prefix/$path",
B2ContentTypes.APPLICATION_OCTET,
B2FileContentSource.builder(file).setSha1(sha1.toString()).build()
).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 f31b276

Please sign in to comment.