Skip to content

Instantly share code, notes, and snippets.

@0xack13
Last active February 20, 2025 23:06
Show Gist options
  • Save 0xack13/46433b67e61cc72ff9f4a3b6b097fc1a to your computer and use it in GitHub Desktop.
Save 0xack13/46433b67e61cc72ff9f4a3b6b097fc1a to your computer and use it in GitHub Desktop.
gha_action_duration
#!/bin/bash
# Set your GitHub personal access token (Use a fine-grained PAT with "actions: read" permission)
GITHUB_TOKEN="your_personal_access_token"
OWNER="your_github_username_or_org"
REPO="your_repository_name"
# Get the latest workflow run ID
RUN_ID=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$OWNER/$REPO/actions/runs?per_page=1" | jq -r '.workflow_runs[0].id')
if [[ "$RUN_ID" == "null" || -z "$RUN_ID" ]]; then
echo "Error: No workflow runs found."
exit 1
fi
echo "Fetching jobs for Workflow Run ID: $RUN_ID"
# Get all jobs for the latest workflow run
JOBS_JSON=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$OWNER/$REPO/actions/runs/$RUN_ID/jobs")
# Extract job names, start times, and end times
echo "$JOBS_JSON" | jq -r '.jobs[] | "\(.name) \(.started_at) \(.completed_at)"' | while read -r name start end; do
if [[ "$start" == "null" || "$end" == "null" ]]; then
echo "$name: Still running or failed"
else
# Convert timestamps to seconds since epoch
START_SEC=$(date -d "$start" +%s)
END_SEC=$(date -d "$end" +%s)
DURATION=$((END_SEC - START_SEC))
echo "$name: $DURATION seconds"
fi
done
@0xack13
Copy link
Author

0xack13 commented Feb 20, 2025

awk -F ': ' '
    NR==FNR { jobs[$1] = $2; next }
    $1 in jobs { printf "%-30s %10s -> %10s\n", $1, jobs[$1], $2 }
' output1.txt output2.txt

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