Skip to content

Instantly share code, notes, and snippets.

@lazuee
Created January 13, 2025 17:28
Show Gist options
  • Save lazuee/fb053ffd8729a52ad42d45c20f145aef to your computer and use it in GitHub Desktop.
Save lazuee/fb053ffd8729a52ad42d45c20f145aef to your computer and use it in GitHub Desktop.
Remove deployment for non-existent commits
#!/bin/bash
OWNER="lazuee"
REPO="react-router"
BRANCH="main"
deployment_shas=$(gh api repos/$OWNER/$REPO/deployments --jq '.[].sha')
existing_shas=$(gh api repos/$OWNER/$REPO/commits?sha=$BRANCH --jq '.[].sha')
for deployment_sha in $deployment_shas; do
if ! echo "$existing_shas" | grep -q "$deployment_sha"; then
echo "Deactivating and deleting deployment for non-existent commit SHA: $deployment_sha"
deployment_id=$(gh api repos/$OWNER/$REPO/deployments --jq ".[] | select(.sha == \"$deployment_sha\") | .id")
gh api --method POST /repos/$OWNER/$REPO/deployments/$deployment_id/statuses -f "state=inactive"
gh api --method DELETE /repos/$OWNER/$REPO/deployments/$deployment_id
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment