Last active
February 20, 2025 23:06
-
-
Save 0xack13/46433b67e61cc72ff9f4a3b6b097fc1a to your computer and use it in GitHub Desktop.
gha_action_duration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Author
0xack13
commented
Feb 20, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment