[转] Jenkins pipeline 中获取 exit code, stdout and stderr 返回值和输出
[From] https://issues.jenkins-ci.org/browse/JENKINS-44930
其做法是,把stdout定向到一个文件,sh 配置 returnStatus: true,它的返回是一个0或非0的整数,然后从文件读取stdout的内容。stderr同理可得。
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'