Created
May 23, 2023 07:06
-
-
Save kelvintaywl/726ab96f4d2eb26193512b29a22e9f0f to your computer and use it in GitHub Desktop.
Bash script to print each job's duration for a given CircleCI workflow
This file contains 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
# NOTE: This script requires: | |
# - curl | |
# - jq | |
# - your CircleCI API token (to be set via the $CIRCLE_TOKEN environment variable) | |
# Usage: sh ./calculate.sh <workflow ID> | |
get_job_numbers_in_workflow () { | |
echo "For Workflow ID ${1}:" | |
curl -s -H "Circle-Token: $CIRCLE_TOKEN" \ | |
"https://circleci.com/api/v2/workflow/${1}/job" | jq '.items| map(select(.started_at != null)) | map({ p: .project_slug, j: .job_number|tostring}) | map(.|join("/job/"))' > "${1}.json" | |
} | |
print_duration_in_workflow () { | |
ms=0; | |
for path in $(jq -r ".[]" "${1}.json") | |
do | |
d=$(curl -s -H "Circle-Token: $CIRCLE_TOKEN" "https://circleci.com/api/v2/project/${path}" | jq -r ".duration") | |
echo "\tjob ${path} took ${d} ms." | |
done | |
rm "${1}.json" | |
} | |
for w in "$@" | |
do | |
get_job_numbers_in_workflow $w | |
print_duration_in_workflow $w | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment