Skip to content

Instantly share code, notes, and snippets.

@skwid138
Last active November 14, 2024 16:25
Show Gist options
  • Save skwid138/161cab8f97b73eccb2cb054695fd21c1 to your computer and use it in GitHub Desktop.
Save skwid138/161cab8f97b73eccb2cb054695fd21c1 to your computer and use it in GitHub Desktop.
This script monitors the latest GitHub Actions workflow for the current repository, providing an optional voice notification upon completion, with customizable voice and message options.
#!/bin/bash
# Default values for optional arguments
VOICE=""
MESSAGE="workflow completed"
function show_help() {
echo "Usage: workflow_tail.sh [options]"
echo ""
echo "Options:"
echo " -v, --voice Specify the voice for the 'say' command (optional)"
echo " -m, --message Customize the message for 'say' after the workflow completes (default: 'workflow completed')"
echo " -h, --help Display this help message"
exit 0
}
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-v|--voice) VOICE="$2"; shift ;;
-m|--message) MESSAGE="$2"; shift ;;
-h|--help) show_help ;;
*) echo "Unknown option: $1"; show_help ;;
esac
shift
done
# Check if gh CLI is installed
if ! command -v gh &> /dev/null; then
echo "Error: GitHub CLI (gh) is not installed. Please install it and try again."
exit 1
fi
# Check if the user is authenticated with GitHub CLI
if ! gh auth status &> /dev/null; then
echo "Error: GitHub CLI is not authenticated. Run 'gh auth login' to authenticate."
exit 1
fi
# Get the GitHub repository info from the current directory's Git configuration
REPO_URL=$(git config --get remote.origin.url)
# Check if we're in a git repository with a GitHub remote
if [ -z "$REPO_URL" ]; then
echo "Error: Not a git repository or no remote.origin.url found. Please navigate to a GitHub repository and try again."
exit 1
fi
# Format the user/repo structure from the Git remote URL
USER_REPO=$(echo "$(dirname "$REPO_URL")/$(basename -s .git "$REPO_URL")" | cut -d: -f2)
# Fetch the latest workflow run
RUN_ID=$(gh run list --limit 1 --repo "$USER_REPO" | cut -f7)
if [ -z "$RUN_ID" ]; then
echo "Error: Could not retrieve the latest workflow run. Please check your GitHub permissions or if the repository has any workflows."
exit 1
fi
# Watch the workflow log in real-time
gh run watch "$RUN_ID" --repo "$USER_REPO"
# Say a message after the workflow completes
if [ -n "$VOICE" ]; then
say -v "$VOICE" "$MESSAGE"
else
say "$MESSAGE"
fi
@skwid138
Copy link
Author

Thank you @wpromatt for fixing USER_REPO and RUN_ID

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