2016-09-23 62 views
1

我有一個運行shell命令的Jenkinsfile,我想將sh方法的輸出作爲slackSend方法的message:發送。在Jenkins文件中捕獲併發送sh方法的輸出

到目前爲止,我有以下信息,但消息到閒置通道是空的。我不能確定如何捕獲輸出的方式,我可以在消息部分引用它:

node { 

     checkout scm 

     stage 'run shell command' 

     def shell_command = sh "ls -l" 
     def shell_output = apply_cluster 

     stage 'notify slack-notification' 
     slackSend channel: '#slack-notifications', color: 'good', message:  shell_output, teamDomain: 'company', token: env.SLACK_TOKEN 
} 
+0

的可能的複製[詹金斯流水線插件:執行shell和解析輸出(http://stackoverflow.com/questions/36304131/jenkins-pipeline-plugin-execute-shell-和解析輸出) – Pom12

回答

1

多一點研究,我發現了幾件事情我沒有初步瞭解:第一即使在Jenkinsfile中定義了一個函數,如果它是一個像sh這樣的DSL方法,它仍然會被執行,所以我沒有必要定義第二個函數。其次,如果你想返回標準輸出,你必須指定它作爲命令的一部分。新代碼如下所示:

node { 

     checkout scm 

     stage 'run shell command' 

     def shell_command = sh script: "ls -l", returnStdout: true 

     stage 'notify slack-notification' 
     slackSend channel: '#slack-notifications', color: 'good', message: shell_command, teamDomain: 'company', token: env.SLACK_TOKEN 
} 
相關問題