Skip to content

Commit

Permalink
fix(shell/octo): adds workaround for dotnet runtime issue with additi…
Browse files Browse the repository at this point in the history
…onal logging
  • Loading branch information
Marcel Kesselring authored and Marcel Kesselring committed May 27, 2021
1 parent 5e5a2cd commit 8bab6b1
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 21 deletions.
20 changes: 15 additions & 5 deletions src/main/kotlin/com/liftric/octopusdeploy/shell.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package com.liftric.octopusdeploy

import org.slf4j.Logger
import java.io.File

internal fun shell(cmd: String): ShellResult = File(".").shell(cmd)
internal fun shell(cmd: String, logger: Logger): ShellResult = File(".").shell(cmd, logger)

internal fun File.shell(cmd: String): ShellResult {
val process = Runtime.getRuntime().exec(arrayOf("sh", "-c", cmd), emptyArray(), this)
internal fun File.shell(cmd: String, logger: Logger): ShellResult {
val tmpDir = System.getProperty("java.io.tmpdir")
// DOTNET_BUNDLE_EXTRACT_BASE_DIR is a workaround for https://github.com/dotnet/runtime/issues/3846
val cmdarray = arrayOf("sh", "-c", "DOTNET_BUNDLE_EXTRACT_BASE_DIR=$tmpDir $cmd")
val cmdDir = this
logger.debug("shell: cmdarray='${cmdarray.toList()}'")
logger.debug("shell: cmdDir='$cmdDir'")
logger.debug("shell: tmpDir='$tmpDir'")
val process = Runtime.getRuntime().exec(cmdarray, emptyArray(), cmdDir)
val exitCode = process.waitFor()
val inputText = process.inputStream.bufferedReader().readText().trim()
val errorText = process.errorStream.bufferedReader().readText().trim()
return ShellResult(exitCode, inputText, errorText)
val result = ShellResult(exitCode, inputText, errorText)
logger.debug("shell: result=$result")
return result
}

data class ShellResult(val exitCode: Int, val inputText: String, val errorText: String)
data class ShellResult(val exitCode: Int, val inputText: String, val errorText: String)
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ open class CommitsSinceLastTagTask : DefaultTask() {
val firstCommitHash: String =
firstCommitFile?.readText() ?: error("couldn't read firstCommitFile!")
val (exitCode, inputText, errorText) = workingDir.shell(
"git log --pretty='format:%H#%s \\(%an\\)' ${previousTag ?: firstCommitHash}..HEAD"
"git log --pretty='format:%H#%s \\(%an\\)' ${previousTag ?: firstCommitHash}..HEAD",
logger
)
if (exitCode == 0) {
logger.info("previous tag: $inputText")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ open class FirstCommitHashTask : DefaultTask() {

@TaskAction
fun execute() {
val (exitCode, inputText, errorText) = workingDir.shell("git log --pretty='format:%H' --reverse | head -1")
val (exitCode, inputText, errorText) = workingDir.shell(
"git log --pretty='format:%H' --reverse | head -1",
logger
)
if (exitCode == 0) {
logger.info("first commit hash: $inputText")
outputFile = File(outputDir, "firstCommitHash").apply {
Expand All @@ -36,4 +39,4 @@ open class FirstCommitHashTask : DefaultTask() {
throw IllegalStateException("git log exitCode: $exitCode")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.liftric.octopusdeploy.task

import com.liftric.octopusdeploy.shell
import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.*
import java.io.File

Expand All @@ -24,7 +23,7 @@ open class PreviousTagTask : DefaultTask() {

@TaskAction
fun execute() {
val (exitCode, inputText, errorText) = workingDir.shell("git describe --tags --abbrev=0 @^")
val (exitCode, inputText, errorText) = workingDir.shell("git describe --tags --abbrev=0 @^", logger)
if (exitCode == 0) {
logger.info("previous tag: $inputText")
outputFile = File(outputDir, "previousTagName").apply {
Expand All @@ -35,4 +34,4 @@ open class PreviousTagTask : DefaultTask() {
logger.error(errorText)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ open class PromoteReleaseTask : DefaultTask() {
"--project=$projectNameValue",
"--from=$fromValue",
"--to=$toValue"
).joinToString(" ").let { shell(it) }
).joinToString(" ").let { shell(it, logger) }
if (exitCode == 0) {
println(inputText)
println(errorText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ open class UploadBuildInformationTask : DefaultTask() {
packageName.get(),
"--version=${version.get()}",
overwriteMode?.let { "--overwrite-mode=$it" }
).filterNotNull().joinToString(" ").let { shell(it) }
).filterNotNull().joinToString(" ").let { shell(it, logger) }
if (exitCode == 0) {
println(inputText)
println(errorText)
} else {
logger.error("octo build-information returned non-zero exitCode: $exitCode")
logger.error(inputText)
logger.error("inputStream='$inputText'")
logger.error("errorStream='$errorText'")
throw IllegalStateException("octo build-information exitCode: $exitCode")
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.property
import java.io.File

open class UploadPackageTask : DefaultTask() {
init {
Expand Down Expand Up @@ -73,7 +72,7 @@ open class UploadPackageTask : DefaultTask() {
"--package",
packageFile.get().asFile.absolutePath ?: error("couldn't find build-information.json"),
overwriteMode?.let { "--overwrite-mode=$it" }
).filterNotNull().joinToString(" ").let { shell(it) }
).filterNotNull().joinToString(" ").let { shell(it, logger) }
if (exitCode == 0) {
println(inputText)
println(errorText)
Expand Down
8 changes: 5 additions & 3 deletions src/test/kotlin/com/liftric/octopusdeploy/task/shared.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.liftric.octopusdeploy.task

import com.liftric.octopusdeploy.shell
import junit.framework.TestCase
import junit.framework.TestCase.assertEquals
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File

private val logger: Logger = LoggerFactory.getLogger("shared")
fun File.setupGitRepo() {
println("setupGitRepo=${this.absolutePath}")
try {
Expand All @@ -21,7 +23,7 @@ fun File.setupGitRepo() {
} catch (e: Exception) {
println(e.message)
e.printStackTrace()
val (exitCode, inputText, errorText) = this.shell("ls -lah")
val (exitCode, inputText, errorText) = this.shell("ls -lah", logger)
println("exitCode=$exitCode")
println("inputText=$inputText")
println("errorText=$errorText")
Expand All @@ -31,7 +33,7 @@ fun File.setupGitRepo() {

private fun File.verboseTestShell(cmd: String) {
println("verboseTestShell=$cmd")
val (exitCode, inputText, errorText) = this.shell(cmd)
val (exitCode, inputText, errorText) = this.shell(cmd, logger)
println("verboseTestShell exitCode=$exitCode")
println("verboseTestShell inputText=$inputText")
println("verboseTestShell errorText=$errorText")
Expand Down

0 comments on commit 8bab6b1

Please sign in to comment.