Skip to content

Instantly share code, notes, and snippets.

@Zoramite
Last active June 21, 2019 17:26
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 Zoramite/2ce145a92e892317697c8ccad7d079e7 to your computer and use it in GitHub Desktop.
Save Zoramite/2ce145a92e892317697c8ccad7d079e7 to your computer and use it in GitHub Desktop.
GAE Version Cleanup
#!/bin/bash
while getopts :a:p: opt
do
case $opt in
a)
account="$OPTARG"
;;
p)
project_id="$OPTARG"
;;
*)
echo "Invalid option: -$OPTARG" >&2
exit 2
;;
esac
done
echo "account = $account"
echo "project_id = $project_id"
# Shift the options down.
shift $((OPTIND - 1))
VERSIONS=$(gcloud app versions list --account $account --project=$project_id --service $1 --sort-by "~last_deployed_time" --filter "traffic_split<.1" --format 'value(version.id)')
COUNT=0
INDEX=-1
DELETE_VERSIONS=()
echo "Keeping the $2 latest versions of the $1 service"
for VERSION in $VERSIONS
do
((COUNT++))
if [ $COUNT -gt $2 ]
then
# echo "Going to delete version $VERSION of the $1 service."
DELETE_VERSIONS+=($VERSION)
else
echo "Going to keep version $VERSION of the $1 service."
fi
done
if [ ${#DELETE_VERSIONS[@]} -gt 0 ]
then
gcloud app versions delete ${DELETE_VERSIONS[*]} --account $account --project=$project_id --service $1 -q
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment