Skip to content

Instantly share code, notes, and snippets.

@andronedev
Last active April 22, 2024 17:05
Show Gist options
  • Save andronedev/a27df5c3b70cd4e1fdce62500e38304a to your computer and use it in GitHub Desktop.
Save andronedev/a27df5c3b70cd4e1fdce62500e38304a to your computer and use it in GitHub Desktop.
GitHub Artifact Cleaner
#!/bin/bash
# Basic Documentation
cat << EOF
GitHub Artifact Cleaner
-----------------------
This script deletes all artifacts from a specified GitHub repository.
USAGE:
1. Ensure that the provided GitHub token has the necessary permissions to access the artifacts.
2. Run the script and follow the on-screen instructions to enter the repository URL and GitHub token.
EOF
# Prompt for repository URL
read -p "Enter the GitHub repository URL: " repo_url
# Prompt for GitHub authentication token
read -p "Enter your GitHub token: " github_token
# Extract owner and repository name from the URL
owner=$(echo $repo_url | sed -E 's|https://github.com/([^/]*)/([^/]*)|\1|')
repo=$(echo $repo_url | sed -E 's|https://github.com/([^/]*)/([^/]*)|\2|')
# Function to fetch all artifacts
function fetch_artifacts {
echo "Fetching list of artifacts..."
artifact_ids=$(curl -s -H "Authorization: token $github_token" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$owner/$repo/actions/artifacts" | jq -r '.artifacts[] | .id')
if [[ -z "$artifact_ids" ]]; then
echo "No artifacts to delete."
exit 0
fi
echo "$artifact_ids"
}
# Function to delete an artifact
function delete_artifact {
artifact_id=$1
echo "Deleting artifact ID $artifact_id..."
response=$(curl -s -X DELETE -H "Authorization: token $github_token" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$owner/$repo/actions/artifacts/$artifact_id")
echo "Artifact $artifact_id deleted."
}
# Get all artifacts and delete them one by one
artifact_ids=$(fetch_artifacts)
for id in $artifact_ids; do
delete_artifact $id
done
echo "All artifacts have been deleted."

GitHub Artifact Cleaner

This tool is designed to help manage and clean up artifacts in a GitHub repository. It deletes all artifacts associated with a given GitHub repository using the GitHub API.

Prerequisites

Before you begin, ensure you have the following installed:

  • curl: Used to make API calls to GitHub.
  • jq: Used to parse JSON data returned by API calls.

These tools are available on most Unix-like systems and can be installed using your system's package manager.

Getting Started

  1. Clone the repository:

    git clone https://github.com/yourusername/github-artifact-cleaner.git
    cd github-artifact-cleaner
    
  2. Make the script executable:

    chmod +x delete_github_artifacts.sh
    

Usage

To use the script, follow these steps:

  1. Run the script:

    ./delete_github_artifacts.sh
    
  2. Enter the GitHub repository URL: Follow the prompt to enter the repository URL, for example:

    https://github.com/username/repository
    
  3. Enter your GitHub token: You will need a GitHub token with appropriate permissions (i.e., repo scope which includes repo:status, repo_deployment, public_repo, repo:invite, security_events, delete_repo, and write:packages).

    Follow the prompt to enter your token securely.

The script will then proceed to fetch and delete all artifacts, logging each action to the terminal.

Security

Ensure that your GitHub token is kept secure. Do not share it in publicly accessible areas, and use environment variables or secure vaults to manage credentials in production environments.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment