Skip to content

Instantly share code, notes, and snippets.

@rjocoleman
Created May 23, 2022 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjocoleman/dad857cfb0ee9c9815c72453b68c1167 to your computer and use it in GitHub Desktop.
Save rjocoleman/dad857cfb0ee9c9815c72453b68c1167 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
owner_repo=${1:-}
state=${2:-}
environment=${3:-}
if [[ -z "$owner_repo" || -z "$state" || -z "$environment" ]]; then
echo -e 'WARNING: this will delete and overwrite stuff without asking for permission, and no backup is provided. Continue at your own risk'
echo -e 'This script overwrites environment name and state for deployments, 100 at a time. It then attempts to delete the deployment.'
echo -e 'You may need to run this script multiple times.'
echo -e 'It assumes you have gh cli and are logged in with at least repo scope & perhaps deployment scope.'
echo -e "Repo must be specified in owner/repo and suggested params are 'invalid' state and 'delete' environment."
echo -e "usage: $0 owner/repo state environment"
exit 1
fi
gh api \
-H "Accept: application/vnd.github.v3+json" \
"/repos/${owner_repo}/deployments?per_page=100" | jq -r '.[].id' | while read -r deployment_id; do
## set status and environment name
gh api \
-H "Accept: application/vnd.github.v3+json" \
--method POST \
-f state="${state}" \
-f environment="${environment}" \
"/repos/${owner_repo}/deployments/${deployment_id}/statuses"
## delete deployment
gh api \
-H "Accept: application/vnd.github.v3+json" \
--method DELETE \
"/repos/${owner_repo}/deployments/${deployment_id}"
done
echo -e 'Run Completed'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment