I am trying to execute a github workflow via the CLI, on a particular branch. The documentation for this is available here https://cli.github.com/manual/gh_workflow_run
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 is even better.
https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event As per the docs, it just returns a Status: 204 No Content
.
CodePudding user response:
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.