Sub topic 7
Watching and triggering Actions runs
This chapter assumes you can open and merge pull requests with gh, know what a GitHub Actions workflow and a run are, and are comfortable in a shell. A workflow is a YAML file in .github/workflows/. A run is one execution of that workflow.
The working model
gh splits Actions into two command groups. gh run works with individual executions. gh workflow works with the workflow definitions. Most of the time you start with a run, because something just happened (a push, a pull request) and you want to know whether it passed.
Finding and following runs
List recent runs in the current repository:
gh run list --limit 5
gh run watch
gh run view --log-failedgh run list prints status, conclusion, workflow name, branch, and run ID. gh run watch with no argument asks you to choose from in-progress runs, then refreshes until the run finishes. gh run view --log-failed prints only the logs of failed steps. This is usually the quickest way to find the error without scrolling through a long log.
You can narrow the list with filters:
gh run list --workflow ci.yml --branch main --status failure --limit 10To skip the picker, pass a run ID:
gh run watch 1234567890 --exit-status
gh run view 1234567890 --log-failed--exit-status makes gh run watch exit with a non-zero code if the run fails. Without it, the command exits 0 even when the run failed, so a script can't tell the difference. This is a common mistake.
Triggering a workflow
A workflow can only be started from the command line if it declares the workflow_dispatch trigger:
name: Deploy
on:
workflow_dispatch:
inputs:
environment:
description: Target environment
required: true
default: staging
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- run: echo "Deploying to ${{ inputs.environment }}"Trigger it by file name or workflow name, and pass inputs with -f:
gh workflow list
gh workflow run deploy.yml --ref main -f environment=production--ref selects the branch or tag to run against. The workflow file must already exist on the default branch for the dispatch trigger to be recognized. Branch-only versions can be run with --ref after that.
gh workflow run returns as soon as GitHub accepts the request. It doesn't print a run ID, and the run can take a few seconds to appear. To follow it, list the newest run for that workflow:
gh workflow run deploy.yml -f environment=staging
sleep 5
gh run watch "$(gh run list --workflow deploy.yml --limit 1 --json databaseId --jq '.[0].databaseId')" --exit-statusRerunning after a failure
Flaky tests and expired credentials are common reasons for a red run. Rerun only the jobs that failed:
gh run rerun 1234567890 --failedOmit --failed to rerun everything. Rerunning does not pick up workflow file changes made since the original run, so if you fixed the YAML, push the change and let a new run start.
Common errors
| Symptom | Likely cause |
|---|---|
| `Workflow does not have 'workflow_dispatch' trigger` | The workflow's `on:` block lacks `workflow_dispatch`. |
| `could not find any workflows named ...` | Wrong name, or the file is not on the default branch. |
| `gh run watch` returns success on a failed run | `--exit-status` was not set. |
| `HTTP 403` when triggering | The token lacks the `workflow` scope. Run `gh auth refresh -s workflow`. |
Exercise
A script runs gh run watch 1234567890 and then deploys if the command succeeds. The run fails, but the deploy still happens. What is wrong, and how do you fix it?
Show answer
gh run watch exits with 0 when the run finishes, even if the run concluded with a failure. Add --exit-status so the command exits non-zero on failure, and the script's && or if check will then stop the deploy.
Exercise
You run gh workflow run ci.yml and get an error that the workflow has no workflow_dispatch trigger. What do you change?
Show answer
Add workflow_dispatch: under on: in the workflow file and get that change onto the default branch. Then run the command again.