Skip to content

Instantly share code, notes, and snippets.

@fuji44
Created November 3, 2023 08:32
Show Gist options
  • Save fuji44/f481cd55a1b61f03744653f7e023369a to your computer and use it in GitHub Desktop.
Save fuji44/f481cd55a1b61f03744653f7e023369a to your computer and use it in GitHub Desktop.
Watch the latest action execution using GitHub CLI

Watch the latest action execution using GitHub CLI

# Run git repository dir
runId=$(gh run list -u fuji44 -b develop -L 1 --json databaseId | jq .[].databaseId)
gh run watch $runId

Example script

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"

Usage

# 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment