Created
January 18, 2025 21:30
-
-
Save dudo/0afbc32c7ee04e2f6fb0479d4b8b76b0 to your computer and use it in GitHub Desktop.
Delete Github Deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Variables | |
OWNER="owner" | |
REPO="repo" | |
# List all deployments | |
DEPLOYMENTS=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github+json" \ | |
https://api.github.com/repos/$OWNER/$REPO/deployments | jq -r '.[].id') | |
# Check if any deployments were found | |
if [ -z "$DEPLOYMENTS" ]; then | |
echo "No deployments found." | |
fi | |
# Iterate through each deployment | |
echo "$DEPLOYMENTS" | while IFS= read -r ID; do | |
echo "Attempting to delete deployment ID: $ID" | |
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE \ | |
-H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github+json" \ | |
https://api.github.com/repos/$OWNER/$REPO/deployments/$ID) | |
if [ "$RESPONSE" -eq 204 ]; then | |
echo "Successfully deleted deployment ID: $ID" | |
elif [ "$RESPONSE" -eq 422 ]; then | |
echo "Deployment ID: $ID cannot be deleted - marking as inactive." | |
INACTIVE_RESPONSE=$(curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github+json" \ | |
-d '{"state": "inactive"}' \ | |
https://api.github.com/repos/$OWNER/$REPO/deployments/$ID/statuses) | |
if [[ "$INACTIVE_RESPONSE" == *"state"* ]]; then | |
echo "Deployment ID: $ID marked as inactive. Retrying deletion..." | |
RETRY_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE \ | |
-H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github+json" \ | |
https://api.github.com/repos/$OWNER/$REPO/deployments/$ID) | |
if [ "$RETRY_RESPONSE" -eq 204 ]; then | |
echo "Successfully deleted deployment ID: $ID after marking as inactive." | |
else | |
echo "Failed to delete deployment ID: $ID after marking as inactive." | |
fi | |
else | |
echo "Failed to mark deployment ID: $ID as inactive." | |
fi | |
else | |
echo "Failed to delete deployment ID: $ID (HTTP status: $RESPONSE)" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment