2016-04-08 7 views

risposta

27

Come menzionato in JENKINS-26133 non è possibile ottenere l'output della shell come variabile. Come soluzione alternativa suggerita l'uso di writ-read dal file temporaneo. Così il vostro esempio sarà simile:

sh "echo foo > result"; 
def output=readFile('result').trim() 
echo "output=$output"; 
+8

Per i nuovi arrivati, si prega di vedere la risposta http://stackoverflow.com/a/38912813/345845 di sotto, questo è stato reso più facile dal momento che con il nuovo parametro di 'returnStdout' passato al passo 'sh'. –

+2

"non è possibile ottenere l'output della shell come variabile" - non è vero. Questo è un trucco, la risposta corretta è returnStdout. – Graham

+0

la risposta è più pulita – mfalto

113

Now, il sh step supporta ritorno stdout fornendo il parametro 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) 

Vedere this example.

+0

Qualcuno può aiutarmi per http://stackoverflow.com/questions/40946697/cant-store-sh-command-output-through-dsl-groovy-in-jenkins-pipeline-job? Grazie in anticipo! –

+4

nota la parte '.trim()' di questa risposta, altrimenti potresti ottenere un carattere di fine riga alla fine della riga –

+2

append 'shire' a 'rev-parse' può solo ottenere direttamente un hash breve – Leon

3

Prova questo:

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 
    ... 
} 

provata su:

  • Jenkins ver. 2.19.1
  • Pipeline 2.4
Problemi correlati