rework logging in gitrunner

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

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

@ -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"
)
}
}

@ -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"
)

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