Search code examples
githubgithub-apigithub-cli

How do I view the logs of a run?


According to the GitHub CLI docs for gh run view, I should be able to query the logs of a run with this command:

gh run view --log <run-id>

For instance, to get the full log for run 2796702959 in the vitejs/vite repo:

gh run view -R vitejs/vite --log 2796702959

But that command returns nothing. Enabling debug reveals no errors:

GH_DEBUG=true gh run view -R vitejs/vite --log 2796702959
⣾* Request at 2022-08-04 19:55:51.795659 -0500 CDT m=+0.012528935
* Request to https://api.github.com/repos/vitejs/vite/actions/runs/2796702959
⣻* Request took 283.249521ms
* Request at 2022-08-04 19:55:52.08233 -0500 CDT m=+0.299195200
* Request to https://api.github.com/repos/vitejs/vite/actions/runs/2796702959/jobs
⣾% 

Env: macOS Big Sur, gh v2.11.3, GNU bash v3.2.57

How do I actually query the run's logs with gh?


Solution

  • As of gh v2.11.3, that particular command is buggy (cli/cli#5011).

    A workaround is to use a Bash script to call the API directly via gh api:

    #!/usr/bin/env bash -e
    
    REPO_NAME=${1?Missing repo name argument} # example: vitejs/vite
    RUN_ID=${2?Missing run ID argument}
    
    ZIPFILE=$(mktemp)
    TMPDIR=$(mktemp -d)
    
    Color_Off='\033[0m'
    BBlack='\033[1;30m'
    On_Cyan='\033[46m'
    
    function downloadLogs() {
      echo "downloading logs for run $RUN_ID ..."
      gh api /repos/$REPO_NAME/actions/runs/$RUN_ID/logs > $ZIPFILE
      unzip -d $TMPDIR $ZIPFILE
      rm -f $ZIPFILE
    }
    
    function dumpLogs() {
      find $TMPDIR -type f -maxdepth 1 -print0 |
        while IFS= read -r -d $'\0' file; do
          echo
          basefile=$(basename "$file")
          echo -e "${BBlack}${On_Cyan} $basefile ${Color_Off}"
          echo
    
          cat "$file"
        done
    }
    
    downloadLogs
    dumpLogs
    

    Usage:

    ./gh-logs.sh <repo-name> <run-id>
    

    Example:

    ./gh-logs.sh vitejs/vite 2796702959 | less -R