While working with Github actions, I need to understand what would I need to do in order to capture the log lines when there is an error in any step and in a following job, send those to an external system.
Let's set the next scenario:
jobs:
job1:
name: job 1
steps:
- name: step1
...
- name: step2
...
- name: step3
...
...
job2:
name: job 2
needs: [job1]
if: always()
steps:
- name: Send error lines to third party system
...
During the execution of job1, one of the steps fail, consequently setting some error logs in the stdout and stderr.
What would be the way to capture those errored log lines in job2, in order to take some actions with them? Such as sending those to an external system.
CodePudding user response:
The way I've addressed a similar need is to use tee
:
steps:
- name: Run Tool
run: |
some_tool | tee output.log
shell: bash
That results in you getting the same logs you'd always see in the GitHub Actions console, while also persisting them to disk. If you want stderr too, do
some_tool 2>&1 | tee output.log
Then, in a later step in the same job, you can do whatever you like with those logs, using the if: ${{ failure() }}
syntax:
steps:
- name: Persist logs
if: ${{ failure() }}
run: |
cat output.log | do_something_with_logs
shell: bash
If you need to persist the logs across a job boundary, you could use artifacts.