-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathprune-artifact-environments-pantheon
executable file
·64 lines (51 loc) · 1.52 KB
/
prune-artifact-environments-pantheon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
#
# Cleanup multidev environments.
#
set -e
usage() {
echo "
Cleanup multidev environments for branches that no longer exist for the remote of the current repository.
Options:
-h: Show help
-p: Set a regex pattern branch names must match to consider for pruning.
-s: Set the git repository that will be used as the source (defaults to origin).
-i: Set the Pantheon site that will be used (defaults to TERMINUS_SITE).
-n: Dry run (does not delete sites)
Usage:
Delete all multidev sites built off of a p-* branch that no longer exist on the current origin:
$0 -p 'p-.*' -i mysite
"
}
error_out() {
echo $1 >&2
exit $2
}
pattern=".*"
site="$TERMINUS_SITE"
source="origin"
dryrun=0
while getopts "hp:s:i:n" opt; do
case "$opt" in
h) usage; exit 0;;
p) pattern=$OPTARG;;
s) source=$OPTARG;;
i) site=$OPTARG;;
n) dryrun=1;;
esac
done
test -n "$site" || error_out "Site must be set using the -i flag or the TERMINUS_SITE environment variable" 1
# Auto-login if possible.
if [ -n "$TERMINUS_MACHINE_TOKEN" ]; then
[ -e ~/.terminus/cache/session ] || terminus auth:login --machine-token="$TERMINUS_MACHINE_TOKEN"
fi
multidevs=$(terminus multidev:list --field=id "$site")
IFS=$'\n';
for multidev in $multidevs; do
if [[ "$multidev" = $pattern ]]; then
if ! git ls-remote --quiet --exit-code "$source" "$multidev" > /dev/null 2>&1; then
echo "Deleting $multidev environment."
[[ $dryrun -eq 1 ]] || terminus multidev:delete -y --delete-branch "$site.$multidev"
fi
fi
done