Skip to content

Instantly share code, notes, and snippets.

@acaire
Created November 15, 2023 03:36
Show Gist options
  • Save acaire/816cfd2715e47f1f00b1b62fec576d50 to your computer and use it in GitHub Desktop.
Save acaire/816cfd2715e47f1f00b1b62fec576d50 to your computer and use it in GitHub Desktop.
Fetch Github Actions Billable Usage per repo
#!/bin/bash
set -euo pipefail
#####
# Use this script to identify billable GitHub actions usage for a repo
# within the current billable period.
#
# Prerequisite:
#
# Fetch a PAT from https://github.com/settings/tokens?type=beta
# and assign read access to actions. Export as "GITHUB_PAT".
#
# Usage:
#
# export GITHUB_PAT=github_pat_example1234
# ./github-actions-billable-usage.sh MyOrg MyRepo
#
# Sample output:
#
# MyOrg-123456 billable usage for UBUNTU is 8 minutes and 0 seconds
#
#####
ORG=$1
REPO=$2
response=$(curl -sL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_PAT}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${ORG}/${REPO}/actions/workflows)
WORKFLOW_IDS=$(echo "$response" | jq -r '.workflows[].id')
for WORKFLOW_ID in $WORKFLOW_IDS; do
curl -sL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_PAT}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${ORG}/${REPO}/actions/workflows/${WORKFLOW_ID}/timing |
jq -r \
--arg repo "$REPO" \
--arg workflow "$WORKFLOW_ID" \
'
.billable as $billable |
($billable | keys_unsorted[] as $os | "\($repo)-\($workflow) billable usage for \($os) is \($billable[$os].total_ms / 60000 | floor) minutes and \($billable[$os].total_ms % 60000 / 1000 | floor) seconds")
'
done
MIT License
Copyright (c) 2023-present Ash Caire
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment