I've been using GitHub CLI more and more lately. Recently, I had to debug a failing of GitHub Action run. Browsing long logs in the WebUI is a bit clunky, so I started downloading the full logs via the CLI.
It is straightforward `gh` command if you already know the run number -- and that information can be obtained from the `gh` command with different options.
I end up with following two-part shell snippet:
VIEW=$(\
gh run list \
| grep $(git branch --show-current) \
| head -1\
| awk '{print $(NF-2)}') \
&& \
gh run view ${VIEW} --log > ~/Downloads/${VIEW}.log
The first command assign the run number to the VIEW variable. It parses the output of the `gh run list` command by:
- filtering run for the current git branch (`grep`)
- taking the most recent one (`head`)
- extracting the run number (third column from the end via `awk`).
The VIEW variable is then used to fetch the logs for the specific run and save them to the uniquely named file in the Downloads folder.
Assumptions:
- the workflow run belongs to the current git branch
- it's the latest run for the branch
- The Downloads folder doesn't already contain a file with the same number (it would be overwritten)