[轉] Jenkins pipeline 中獲取 exit code, stdout and stderr 返回值和輸出

[From] https://issues.jenkins-ci.org/browse/JENKINS-44930git

其作法是,把stdout定向到一個文件,sh 配置 returnStatus: true,它的返回是一個0或非0的整數,而後從文件讀取stdout的內容。stderr同理可得。ui

def status = sh(returnStatus: true, script: "git merge --no-edit $branches > merge_output.txt")
if (status != 0) {
  currentBuild.result = 'FAILED'
  def output = readFile('merge_output.txt').trim()
  slackSend channel: SLACK_CHANNEL, message: "<${env.JOB_URL}|${env.JOB_NAME}> ran into an error merging the PR branches into the ${TARGET_BRANCH} branch:\n```\n${output}\n```\n<${env.BUILD_URL}/console|See the full output>", color: 'warning', tokenCredentialId: 'slack-token'
  error 'Merge conflict'
}
sh 'rm merge_output.txt'
相關文章
相關標籤/搜索