-
Improvement
-
Resolution: Unresolved
-
Major
-
None
Like many of the commenters on -- I'd like to be able to capture the exit status and text written to standard out at the same time.JENKINS-26133
My current use case is calling git merge --no-edit $branches and if there was an error sending a slack notification with the output.
The current workaround is:
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'
Which works but isn't a great developer experience... It would be great if I could request an object that contained: status, stdout, and stderr.
- is duplicated by
-
JENKINS-64882 returnStdout does not capture output of failing command
-
- Resolved
-
- is related to
-
JENKINS-26133 Shell script taking/returning output/status
-
- Resolved
-
I also have wanted to be able to have the exitCode and the stdOut at the same time. About a year ago I started what I call the Jenkins Standard Library to help make building pipelines faster and easier. One of the first things I solved was these issues with `sh()`.
Also not documented here is `result.output` which contains both the stdErr and stdOut but the order can't be guaranteed because bash is async. I also made the exception the same as the result because why wouldn't you want to include things like stdErr, stdOut and exitCode in your failure notification. Also `bash.silent()` which will not output ANYTHING to the build console. That way you can keep your build logs short and concise. Finally `bash.ignoreErrors()` which will return the result object even if the script errors. It also supports not sending output to the build console.
You can also browse the full documentation.
I stopped working on the project when I couldn't find a way to test the code completely but about 2 weeks ago I found a new experimental way to test my library and now that I'm able to completely test the codebase I'm back to adding more features.
Right now the library only has the BashClient and LogClient but I think next I will add an easy way to POST and GET instead of dropping down to curl all the time. I think after that maybe github status checks or maybe a better slack/teams notification.
It would be awesome if you would use my library and open github issues for features/feedback. If not you can always look at the BashClient code and see how I made it work and use it in your own shared libraries. The code is completely OpenSource and free. I will also work on a contributors guide if anyone is interested.
drewish cyberwizzard childnode mpettigr joanperez too many to tag so I will stop here.