Skip to content

Instantly share code, notes, and snippets.

@vincenzo
Last active March 7, 2019 16:43
Show Gist options
  • Save vincenzo/9a09a41c7f46bee598538f2eb0624a90 to your computer and use it in GitHub Desktop.
Save vincenzo/9a09a41c7f46bee598538f2eb0624a90 to your computer and use it in GitHub Desktop.
Delete stale (>= 1-month old) environments from a platform.sh account
#!/usr/bin/env bash
# Requires Platform.sh CLI >= 3.28.0
# For all projects
platform projects --pipe |
while read PROJ; do
# For all PROJ's active environments (excluding qa, stage, master, snapshot)
platform environments --pipe -I -p ${PROJ} | grep -vo "master\|qa\|stage\|snapshot" |
while read ENV; do
# Date of last activity for ENV.
ENV_LAST_ACTIVITY=$(platform activities -q --type environment.push --limit=1 --format=csv --date-fmt="U" -p ${PROJ} -e ${ENV} | grep -Eo '[[:digit:]]{10}')
# Current date (epoch).
NOW=$(date +%s)
if [ -n "${ENV_LAST_ACTIVITY}" ]; then
# If last activity occurred a month ago or longer.
if [ $(expr ${NOW} - ${ENV_LAST_ACTIVITY}) -ge 2592000 ]; then
# By default the script prints the command rather than execute it.
# I suggest to leave this as it is, and pipe the script into a suitable
# execution loop.
echo "platform environment:delete -y --no-delete-branch -p ${PROJ} ${ENV}"
fi
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment