rework logging in gitrunner

master
Josha von Gizycki 12 hours ago
parent f613a15b63
commit 99fda410ba

@ -5,4 +5,6 @@ data class BuildContext(
val logFile: LogFile, val logFile: LogFile,
val rev: String, val rev: String,
val buildId: BuildId val buildId: BuildId
) ) {
fun log(s: String) = logFile.append(s)
}

@ -1,5 +1,7 @@
package alfred.web.core.build package alfred.web.core.build
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File import java.io.File
import java.nio.file.Path import java.nio.file.Path
import java.time.ZonedDateTime import java.time.ZonedDateTime
@ -13,6 +15,8 @@ class LogFile(
val backingFile: File val backingFile: File
) { ) {
val logger: Logger = LoggerFactory.getLogger(this::class.java)
fun header(buildId: BuildId, rev: String?, workspace: Path) { fun header(buildId: BuildId, rev: String?, workspace: Path) {
append("At your service.") append("At your service.")
append("Build $buildId, rev [${rev ?: "-none-"}] started at ${ZonedDateTime.now()}") append("Build $buildId, rev [${rev ?: "-none-"}] started at ${ZonedDateTime.now()}")
@ -23,7 +27,9 @@ class LogFile(
append("Build finished at ${ZonedDateTime.now()}") append("Build finished at ${ZonedDateTime.now()}")
} }
fun append(content: String) = fun append(content: String) {
logger.debug("logging to file: $content")
backingFile.appendText( backingFile.appendText(
content.lines().joinToString(separator = "\n") { content.lines().joinToString(separator = "\n") {
if (it.trim().isEmpty()) { if (it.trim().isEmpty()) {
@ -33,5 +39,6 @@ class LogFile(
} }
} + "\n" } + "\n"
) )
}
} }

@ -30,7 +30,7 @@ class GitRunner(
val config = builds.buildConfig(buildId) val config = builds.buildConfig(buildId)
val logFile = builds.createLogFile(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") logger.info("log file: $logFile")
val ctx = BuildContext( val ctx = BuildContext(
@ -55,38 +55,43 @@ class GitRunner(
} }
private fun clone(ctx: BuildContext, wsPath: Path) { 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, ".") .command("git", "clone", ctx.config.gitRepo, ".")
.directory(wsPath.toFile()) .directory(wsPath.toFile())
.start() .start()
handles.add(Handle(process.toHandle(), ctx.buildId)) handles.add(Handle(cloneProc.toHandle(), ctx.buildId))
val cloneSuccess = process.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS)
logger.info("build ${ctx.buildId}: checkout rev ${ctx.rev}") val cloneSuccess = cloneProc.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS)
processes.builder(ctx.config, ctx.logFile, "") 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) .command("git", "checkout", ctx.rev)
.directory(wsPath.toFile()) .directory(wsPath.toFile())
.start() .start()
.waitFor() handles.add(Handle(checkoutProc.toHandle(), ctx.buildId))
if (!cloneSuccess) { val checkoutSuccess = checkoutProc.waitFor(ctx.config.gitCloneTimeout, TimeUnit.SECONDS)
throw FailedToClone(ctx.buildId, ctx.config.gitRepo ?: "[no repo configured]") if (!checkoutSuccess) {
throw FailedToCheckout(ctx.buildId, ctx.config.gitRepo!!, ctx.rev)
} }
} }
private fun execScripts(ctx: BuildContext, wsPath: Path) { private fun execScripts(ctx: BuildContext, wsPath: Path) {
val scriptFiles = listOf("pre.sh", "job.sh", "post.sh") 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 -> scriptFiles.forEach { script ->
val scriptFile = shFile(wsPath, script) val scriptFile = shFile(wsPath, script)
if (scriptFile.exists()) { 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) val scriptProcess = processes.builder(ctx.config, ctx.logFile, ctx.rev)
.command(scriptFile.absolutePath) .command(scriptFile.absolutePath)
@ -95,11 +100,11 @@ class GitRunner(
handles.add(Handle(scriptProcess.toHandle(), ctx.buildId)) handles.add(Handle(scriptProcess.toHandle(), ctx.buildId))
val ret = scriptProcess.waitFor() 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 { } 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( class FailedToClone(
private val buildId: BuildId, buildId: BuildId,
private val gitRepo: String gitRepo: String
) : RuntimeException() { ) : RuntimeException(
override fun toString() = "${FailedToClone::class}: failed to clone $gitRepo for build id $buildId"
"${this::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"
)

@ -0,0 +1,5 @@
█████ ██ ███████ ██████ ███████ ██████ ██████ ██████ ███████ █████ █████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ █████ ██████ █████ ██ ██ █████ ██████ ██ █████ ███████ ███████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ███████ ██ ██ ██ ███████ ██████ ██ ██ ██████ ███████ ██ ██ ██ ██ ███████
Loading…
Cancel
Save