Monitor your GitHub Actions storage usage with a script


GitHub Actions is a powerful tool for automating workflows, but managing storage usage for artifacts and caches can be challenging. GitHub does not currently provide a consolidated overview of storage usage across repositories, making it difficult to track how much space your workflows consume. To address this, I created a script that calculates the total storage used by artifacts and caches for all repositories under a given owner/organization.

Table of Contents

Why This Script?

GitHub Actions has storage quotas for artifacts:

Artifacts: 500 MB (free tier), 1GB (pro tier), 50GB (enterprise tier).

Exceeding these quotas can lead to failed workflows or additional costs.

Show image
Exceeded the GitHub Artifacts storage quota

Exceeded the GitHub Artifacts storage quota

This script which I am sharing provides a detailed breakdown of storage usage for each repository and calculates the total usage across all repositories in an organization.

Prerequisites

Before running the script, ensure you have the following:

  1. GitHub CLI (gh): Install it from GitHub CLI.
  2. Authentication: Log in to the GitHub CLI using gh auth login.
  3. jq: A lightweight JSON processor. Install it using your package manager (e.g., brew install jq on macOS).

The script to check the storage usage

# Ensure the GitHub CLI is authenticated

if ! gh auth status > /dev/null 2>&1; then

echo "Please authenticate with the GitHub CLI using 'gh auth login'."

# Check if the owner is provided

# Check if the owner type is provided

echo "Is the owner an organization or a user? (org/user)"

if [[ "$OWNER_TYPE" != "org" && "$OWNER_TYPE" != "user" ]]; then

echo "Invalid input. Please specify 'org' or 'user'."

echo "Fetching repositories for owner: $OWNER"

# Fetch repositories for the given owner based on type (paginated, including private and internal)

if [ "$OWNER_TYPE" == "org" ]; then

REPOS_RESPONSE=$(gh api -H "Accept: application/vnd.github+json" \

"/orgs/$OWNER/repos?type=all&per_page=100&page=$PAGE")

REPOS_RESPONSE=$(gh api -H "Accept: application/vnd.github+json" \

"/users/$OWNER/repos?type=all&per_page=100&page=$PAGE")

REPOS=$(echo "$REPOS_RESPONSE" | jq -r '.[].full_name')

echo "Repositories on page $PAGE:"

echo "Processing repository: $REPO"

# Fetch artifacts for the repository (paginated)

RESPONSE=$(gh api -H "Accept: application/vnd.github+json" \

"/repos/$REPO/actions/artifacts?per_page=100&page=$REPO_PAGE")

# Extract artifact sizes and sum them up

SIZES=$(echo "$RESPONSE" | jq '.artifacts[].size_in_bytes' 2>/dev/null)

REPO_ARTIFACT_SIZE=$((REPO_ARTIFACT_SIZE + SIZE))

# Check if there are more pages

HAS_NEXT_PAGE=$(echo "$RESPONSE" | jq '.artifacts | length == 100')

if [ "$HAS_NEXT_PAGE" != "true" ]; then

REPO_PAGE=$((REPO_PAGE + 1))

# Fetch cache usage for the repository

CACHE_RESPONSE=$(gh api -H "Accept: application/vnd.github+json" \

"/repos/$REPO/actions/cache/usage")

REPO_CACHE_SIZE=$(echo "$CACHE_RESPONSE" | jq '.active_caches_size_in_bytes // 0')

TOTAL_ARTIFACT_SIZE=$((TOTAL_ARTIFACT_SIZE + REPO_ARTIFACT_SIZE))

TOTAL_CACHE_SIZE=$((TOTAL_CACHE_SIZE + REPO_CACHE_SIZE))

REPO_ARTIFACT_SIZE_GB=$(echo "scale=2; $REPO_ARTIFACT_SIZE / 1024 / 1024 / 1024" | bc)

REPO_CACHE_SIZE_GB=$(echo "scale=2; $REPO_CACHE_SIZE / 1024 / 1024 / 1024" | bc)

echo "Total artifact size for $REPO: $REPO_ARTIFACT_SIZE_GB GB"

echo "Total cache size for $REPO: $REPO_CACHE_SIZE_GB GB"

# Check if there are more pages of repositories

HAS_NEXT_PAGE=$(echo "$REPOS_RESPONSE" | jq 'length == 100')

if [ "$HAS_NEXT_PAGE" != "true" ]; then

# Log total sizes across all repositories

TOTAL_ARTIFACT_SIZE_GB=$(echo "scale=2; $TOTAL_ARTIFACT_SIZE / 1024 / 1024 / 1024" | bc)

TOTAL_CACHE_SIZE_GB=$(echo "scale=2; $TOTAL_CACHE_SIZE / 1024 / 1024 / 1024" | bc)

echo "========================================"

echo "Total artifact size across all repositories: $TOTAL_ARTIFACT_SIZE_GB GB"

echo "Total cache size across all repositories: $TOTAL_CACHE_SIZE_GB GB"

How to Use the Script

  1. Save the script to a file, e.g., check-artifacts-usage.sh.
  2. Make it executable:

    chmod +x check-artifacts-usage.sh

  3. Run the script with the organization name as an argument:

    ./check-artifacts-usage.sh

    Replace with the name of your GitHub owner or organization.

  4. Specify whether the owner is an organization or a user when prompted.
    The script will:
  • Fetch all repositories for the given organization.
  • Calculate the total size of artifacts and caches for each repository.
  • Display the total storage usage across all repositories.

Example Output

> ./check-artifacts-usage.sh my-organization

Is the owner an organization or a user? (org/user)

Fetching repositories for owner: my-organization

Processing repository: my-organization/repo1

Total artifact size for my-organization/repo1: 0 GB

Total cache size for my-organization/repo1: 0 GB

========================================

Total artifact size across all repositories: 24.42 GB

Total cache size across all repositories: 4.09 GB

GitHub Actions Storage Quotas

For more details on GitHub Actions storage quotas, refer to the GitHub Actions usage limits.

Conclusion

This script provides a quick and easy way to monitor your GitHub Actions storage usage, helping you stay within your quotas and avoid unexpected costs. Feel free to adapt it to your needs and share your feedback!

Happy automating!


Share this content:

I am a passionate blogger with extensive experience in web design. As a seasoned YouTube SEO expert, I have helped numerous creators optimize their content for maximum visibility.

Leave a Comment