Skip to content

Instantly share code, notes, and snippets.

@kelvintaywl
Created May 23, 2023 07:06
Show Gist options
  • Save kelvintaywl/726ab96f4d2eb26193512b29a22e9f0f to your computer and use it in GitHub Desktop.
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
# 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