Recently the Support team created a script for easily getting the total number of credits used on a workflow. This was created since the insights endpoint for workflow credits is scoped per branch, which means you need to do an API call per branch to get this information.
This script will likely need to be modified to be used for your purposes, but should be a good starting point!
A few notes/caveats:
- It will only look at branches from the last 100 builds
- The credit data that is returned will be for the last 90 days
- It does create a file (
branches.txt) in the current working directory, but deletes it at the end
- It requires
bcto be installed
- Within the
Variablessection, you will need to enter your API token, organization and project
#!/bin/bash -e # This script will return the total number of credits used, per branch, for a particular workflow. # Can be called with "source name_of_file.sh" -- you will be prompted for a workflow name to get data on ### Variables ### # Set to personal API token CIRCLE_API_TOKEN= # set to github or bitbucket VCS=github # set to organization ORG= # set to project PROJECT= ### Commands ### # Check for Input read -p 'Workflow Name: ' WORKFLOW if [ -z "$WORKFLOW" ] then echo "No argument supplied, please enter a workflow name." exit 1 fi BRANCHES=$(curl https://circleci.com/api/v1.1/project/$VCS/$ORG/$PROJECT?circle-token=$CIRCLE_API_TOKEN\&shallow=true\&limit=100 | jq -r '. | select (.lifecycle? == "finished") | .branch' | sort | uniq) echo $BRANCHES | tr " " "\n" > branches.txt echo echo "Finding insights .........." echo for x in `cat branches.txt` ; do INSIGHTS=$(curl -s https://circleci.com/api/v2/insights/$VCS/$ORG/$PROJECT/workflows/$WORKFLOW?circle-token=$CIRCLE_API_TOKEN\&branch=$x | jq -r '.items | .credits_used'); echo; CREDITS=$(echo $INSIGHTS); echo "*** Credits for branch $x ***"; sed 's/ /+/g' <<< $CREDITS | bc; echo; done # cleanup rm -rf branches.txt #### END ####
The above should result in something like (if the workflow wasn’t run on the branch, the value below that branch will not be present):
Finding insights .......... *** Credits for branch staging *** 2295 *** Credits for branch param-test *** 1 *** Credits for branch new-workflow-test *** *** Credits for branch staging-site *** 84 *** Credits for branch test-workflows *** 100