# Run git repository dir
runId=$(gh run list -u fuji44 -b develop -L 1 --json databaseId | jq .[].databaseId)
gh run watch $runId
This is an example of a script that monitors the execution of the latest Github action.
#! /usr/bin/env bash
validate_commands() {
if ! command -v gh &>/dev/null; then
echo "GitHub CLI (gh) is not installed. Please install it."
exit 1
fi
if ! command -v jq &>/dev/null; then
echo "jq is not installed. Please install it."
exit 1
fi
}
validate_commands
display_usage() {
echo "Usage: $0 [-u <GitHub Username>] [-b <Branch Name>] [-d] [-h]"
echo "Options:"
echo " -u <GitHub Username>: Specify the GitHub username."
echo " -b <Branch Name>: Specify the branch name."
echo " -d: Dry run mode (displays options but doesn't watch)."
echo " -h: Display this help message."
echo
echo "Required tools:"
echo " - GitHub CLI (gh) 1.9.0+"
echo " - jq"
}
user=""
branch=""
dry_run=false
while getopts ":u:b:dh" opt; do
case $opt in
u)
user="$OPTARG"
;;
b)
branch="$OPTARG"
;;
d)
dry_run=true
;;
h)
display_usage
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
display_usage
exit 1
;;
esac
done
run_list_options=""
generate_run_list_options() {
if [ -n "$user" ]; then
run_list_options="${run_list_options} -u ${user}"
fi
if [ -n "$branch" ]; then
run_list_options="${run_list_options} -b ${branch}"
fi
}
generate_run_list_options
if [ "$dry_run" = true ]; then
echo "gh run list options: ${run_list_options}"
fi
target_run_info="$(gh run list ${run_list_options} -L 1 --json databaseId,number,displayTitle,status)"
run_id="$(echo "${target_run_info}" | jq .[].databaseId)"
run_number="$(echo "${target_run_info}" | jq .[].number)"
run_title="$(echo "${target_run_info}" | jq .[].displayTitle)"
run_status="$(echo "${target_run_info}" | jq .[].status)"
if [ -z "$run_id" ]; then
echo "Watch target none."
exit 0
fi
echo "Wathing #${run_number} ${run_title} (${run_id}) current status is ${run_status}"
if [ "$dry_run" = true ]; then
echo "dry run is specified, so it is not watch."
exit 0
fi
gh run watch "$run_id"
# no option
./watch_github_actions.sh
# specify user and branch
./watch_github_actions.sh -u your_username -b main
Example of use in combination with desktop notifications (e.g. linux)
./watch_github_actions.sh && notify-send "done!"
You can use any command for desktop notification: osascript
for macos, snoretoast
for windows, or node-notifier-cli
if you are cross-platform and node.js is available.