githubcontinuous-integrationgithub-actions

Watch GitHub Actions workflow output in CLI?


I am trying to execute a GitHub workflow via the CLI on a particular branch. I found the documentation for this.

Is it possible to somehow get the same kind of logs you get with the browser UI in the terminal? I would like to programmatically interact with it.

If its possible with their REST API, then that would be even better. As per the docs, it just returns a Status: 204 No Content.


Solution

  • It's possible to do trigger a workflow remotely using the Github API with the dispatch_event API you mentioned.

    The Github CLI have various command for workflow (to run, list, view, enable ou disable). You can find more information on the official documentation


    To get the logs from the Github CLI, as explained here, you can use commands such as:

    gh run view run-id --log
    

    Note that if you don't specify run-id, GitHub CLI returns an interactive menu for you to choose a recent run, and then returns another interactive menu for you to choose a job from the run.

    You can also use the --job flag to specify a job ID. Replace job-id with the ID of the job that you want to view logs for.

    gh run view --job job-id --log
    

    You can use grep to search the log. For example, this command will return all log entries that contain the word error.

    gh run view --job job-id --log | grep error
    

    To filter the logs for any failed steps, use --log-failed instead of --log.

    gh run view --job job-id --log-failed
    

    Note that you can get a workflow run id from the Github API as well.


    Therefore, as you should already have the job_id from the workflow file, it could be possible to start a workflow with a dispatch_event through the API, then get the workflow run_id from the workflow runs list API as well, and use the Github CLI command(s) in loop to get the logs.

    It's not pretty, but it should work gathering all those steps in a script as a workaround.