From 99fda410bac11a93ba596fdf861e65accdc69bab Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Thu, 14 Aug 2025 18:44:49 +0200 Subject: [PATCH] rework logging in gitrunner --- .../alfred/web/core/build/BuildContext.kt | 4 +- .../kotlin/alfred/web/core/build/LogFile.kt | 9 ++- .../alfred/web/core/runner/GitRunner.kt | 56 +++++++++++-------- src/main/resources/banner.txt | 5 ++ 4 files changed, 50 insertions(+), 24 deletions(-) create mode 100644 src/main/resources/banner.txt diff --git a/src/main/kotlin/alfred/web/core/build/BuildContext.kt b/src/main/kotlin/alfred/web/core/build/BuildContext.kt index bc4cdb9..88a58ca 100644 --- a/src/main/kotlin/alfred/web/core/build/BuildContext.kt +++ b/src/main/kotlin/alfred/web/core/build/BuildContext.kt @@ -5,4 +5,6 @@ data class BuildContext( val logFile: LogFile, val rev: String, val buildId: BuildId -) +) { + fun log(s: String) = logFile.append(s) +} diff --git a/src/main/kotlin/alfred/web/core/build/LogFile.kt b/src/main/kotlin/alfred/web/core/build/LogFile.kt index 052b845..095487c 100644 --- a/src/main/kotlin/alfred/web/core/build/LogFile.kt +++ b/src/main/kotlin/alfred/web/core/build/LogFile.kt @@ -1,5 +1,7 @@ package alfred.web.core.build +import org.slf4j.Logger +import org.slf4j.LoggerFactory import java.io.File import java.nio.file.Path import java.time.ZonedDateTime @@ -13,6 +15,8 @@ class LogFile( val backingFile: File ) { + val logger: Logger = LoggerFactory.getLogger(this::class.java) + fun header(buildId: BuildId, rev: String?, workspace: Path) { append("At your service.") append("Build $buildId, rev [${rev ?: "-none-"}] started at ${ZonedDateTime.now()}") @@ -23,7 +27,9 @@ class LogFile( append("Build finished at ${ZonedDateTime.now()}") } - fun append(content: String) = + fun append(content: String) { + logger.debug("logging to file: $content") + backingFile.appendText( content.lines().joinToString(separator = "\n") { if (it.trim().isEmpty()) { @@ -33,5 +39,6 @@ class LogFile( } } + "\n" ) + } } diff --git a/src/main/kotlin/alfred/web/core/runner/GitRunner.kt b/src/main/kotlin/alfred/web/core/runner/GitRunner.kt index 7c01133..d0bf8e5 100644 --- a/src/main/kotlin/alfred/web/core/runner/GitRunner.kt +++ b/src/main/kotlin/alfred/web/core/runner/GitRunner.kt @@ -30,7 +30,7 @@ class GitRunner( val config = builds.buildConfig(buildId) val logFile = builds.createLogFile(buildId) - logger.info("preparing process for build $buildId with config $config") + logger.info("preparing process for build $buildId") logger.info("log file: $logFile") val ctx = BuildContext( @@ -55,38 +55,43 @@ class GitRunner( } private fun clone(ctx: BuildContext, wsPath: Path) { - logger.info("build ${ctx.buildId}: cloning ${ctx.config.gitRepo} into $wsPath") + ctx.log("build ${ctx.buildId}: cloning ${ctx.config.gitRepo} into $wsPath") - val process = processes.builder(ctx.config, ctx.logFile, "") + val cloneProc = processes.builder(ctx.config, ctx.logFile, "") .command("git", "clone", ctx.config.gitRepo, ".") .directory(wsPath.toFile()) .start() - handles.add(Handle(process.toHandle(), ctx.buildId)) - val cloneSuccess = process.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS) + handles.add(Handle(cloneProc.toHandle(), ctx.buildId)) - logger.info("build ${ctx.buildId}: checkout rev ${ctx.rev}") - processes.builder(ctx.config, ctx.logFile, "") + val cloneSuccess = cloneProc.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS) + if (!cloneSuccess) { + throw FailedToClone(ctx.buildId, ctx.config.gitRepo ?: "[no repo configured]") + } + + ctx.log("build ${ctx.buildId}: checkout rev ${ctx.rev}") + val checkoutProc = processes.builder(ctx.config, ctx.logFile, "") .command("git", "checkout", ctx.rev) .directory(wsPath.toFile()) .start() - .waitFor() + handles.add(Handle(checkoutProc.toHandle(), ctx.buildId)) - if (!cloneSuccess) { - throw FailedToClone(ctx.buildId, ctx.config.gitRepo ?: "[no repo configured]") + val checkoutSuccess = checkoutProc.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS) + if (!checkoutSuccess) { + throw FailedToCheckout(ctx.buildId, ctx.config.gitRepo!!, ctx.rev) } } private fun execScripts(ctx: BuildContext, wsPath: Path) { val scriptFiles = listOf("pre.sh", "job.sh", "post.sh") - logger.info("build ${ctx.buildId}: looking for scripts $scriptFiles in $scriptsDir/") + ctx.log("build ${ctx.buildId}: looking for scripts $scriptFiles in $scriptsDir/") scriptFiles.forEach { script -> val scriptFile = shFile(wsPath, script) if (scriptFile.exists()) { - logger.info("build ${ctx.buildId}: found script $script, running it") + ctx.log("build ${ctx.buildId}: found script $script, running it") - ctx.logFile.append("\nRunning build file: $script\n") + ctx.log("Running build file: $script") val scriptProcess = processes.builder(ctx.config, ctx.logFile, ctx.rev) .command(scriptFile.absolutePath) @@ -95,11 +100,11 @@ class GitRunner( handles.add(Handle(scriptProcess.toHandle(), ctx.buildId)) val ret = scriptProcess.waitFor() - logger.info("build ${ctx.buildId}: script $script returned $ret") + ctx.log("build ${ctx.buildId}: script $script returned $ret") - ctx.logFile.append("\n$script returned $ret\n") + ctx.log("$script returned $ret") } else { - ctx.logFile.append("\nBuild file $scriptsDir/$script not found\n") + ctx.log("Build file $scriptsDir/$script not found") } } } @@ -113,9 +118,16 @@ class GitRunner( class FailedToClone( - private val buildId: BuildId, - private val gitRepo: String -) : RuntimeException() { - override fun toString() = - "${this::class}: failed to clone $gitRepo for build id $buildId" -} + buildId: BuildId, + gitRepo: String +) : RuntimeException( + "${FailedToClone::class}: failed to clone $gitRepo for build id $buildId" +) + +class FailedToCheckout( + buildId: BuildId, + gitRepo: String, + rev: String +) : RuntimeException( + "failed to checkout revision $rev on repo $gitRepo for build id $buildId" +) diff --git a/src/main/resources/banner.txt b/src/main/resources/banner.txt new file mode 100644 index 0000000..93427a4 --- /dev/null +++ b/src/main/resources/banner.txt @@ -0,0 +1,5 @@ + █████ ██ ███████ ██████ ███████ ██████ ██████ ██████ ███████ █████ █████ ███████ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +███████ ██ █████ ██████ █████ ██ ██ █████ ██████ ██ █████ ███████ ███████ ███████ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██ ██ ███████ ██ ██ ██ ███████ ██████ ██ ██ ██████ ███████ ██ ██ ██ ██ ███████