Zum Beispiel:
var output=sh "echo foo";
echo "output=$output";
Ich werde bekommen:
output=0
Anscheinend bekomme ich eher den Exit-Code als den Standardcode. Ist es möglich, das stdout in einer Pipeline-Variablen zu erfassen, sodass ich Folgendes erhalten kann: output=foo
as mein Ergebnis?
Hinweis: Das verknüpfte Jenkins-Problem wurde inzwischen gelöst.
Wie in JENKINS-26133 erwähnt, war es nicht möglich, die Shell-Ausgabe als Variable abzurufen. Als Problemumgehung wird die Verwendung von writ-read aus temporärer Datei vorgeschlagen. Ihr Beispiel hätte also so ausgesehen:
sh "echo foo > result";
def output=readFile('result').trim()
echo "output=$output";
Jetzt unterstützt der Schritt sh
die Rückgabe von stdout durch Angabe des Parameters returnStdout
.
// These should all be performed at the point where you've
// checked out your sources on the slave. A 'git' executable
// must be available.
// Most typical, if you're not cloning into a sub directory
gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
// short SHA, possibly better for chat notifications, etc.
shortCommit = gitCommit.take(6)
Siehe dieses Beispiel .
Versuche dies:
def get_git_sha(git_dir='') {
dir(git_dir) {
return sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
}
}
node(BUILD_NODE) {
...
repo_SHA = get_git_sha('src/FooBar.git')
echo repo_SHA
...
}
Getestet am:
Eine kurze Version wäre:
echo sh(script: 'ls -al', returnStdout: true).result
Sie können auch diese Funktionen verwenden, um StdErr StdOut und den Rückgabecode zu erfassen.
def runShell(String command){
def responseCode = sh returnStatus: true, script: "${command} &> tmp.txt"
def output = readFile(file: "tmp.txt")
if (responseCode != 0){
println "[ERROR] ${output}"
throw new Exception("${output}")
}else{
return "${output}"
}
}
Beachten:
&>name means 1>name 2>name -- redirect stdout and stderr to the file name