Skip to content

Instantly share code, notes, and snippets.

@martinbonnin
Created October 29, 2021 12:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinbonnin/3c4d79190aad8ec9c306ac86eecbb26d to your computer and use it in GitHub Desktop.
Save martinbonnin/3c4d79190aad8ec9c306ac86eecbb26d to your computer and use it in GitHub Desktop.
#!/usr/bin/env kotlin
/**
* A Kotlin script that will collect all test reports and core dumps from your CI runs
* zip them and upload them as a Github Actions artifact
*
* Copy this file to scripts/collect-diagnostics.main.kts and add the below to your
* Github Actions workflow file:
*
* - name: Collect Diagnostics
* if: always()
* run: ./scripts/collect-diagnostics.main.kts
* - uses: actions/upload-artifact@v2.2.4
* if: always()
* with:
* name: diagnostics.zip
* path: diagnostics.zip
*/
import java.io.File
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
val home = System.getenv("HOME")
println("HOME: $home")
val coreDir = File("$home/Library/Logs/DiagnosticReports/")
ZipOutputStream(File("diagnostics.zip").outputStream()).use { zipOutputStream ->
if (coreDir.exists()) {
zipOutputStream.addDirectory(coreDir, "diagnostics/cores")
}
File(".").walk().filter {
it.name == "build"
}.map {
it.resolve("reports")
}.filter {
it.exists()
}.forEach {
val projectDir = it.parentFile.parentFile
zipOutputStream.addDirectory(it, "diagnostics/tests/${projectDir.name}")
}
}
fun ZipOutputStream.addDirectory(dir: File, prefix: String) {
dir.walk().filter { it.isFile }.forEach {
val name = "$prefix/${it.relativeTo(dir).path}"
println("- $name")
putNextEntry(ZipEntry(name))
it.inputStream().use {
it.copyTo(this)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment