-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathorphaned-pr-environments
More file actions
executable file
·48 lines (40 loc) · 1.89 KB
/
orphaned-pr-environments
File metadata and controls
executable file
·48 lines (40 loc) · 1.89 KB
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
#!/bin/bash
# -----------------------------------------------------------------------------
# This script checks for orphaned PR environments by listing all PR workspaces
# and checking if the associated PR is closed. If the PR is closed the
# resources in the workspace should have been destroyed and the workspace
# deleted, so existing workspaces for closed PRs are considered orphaned.
# -----------------------------------------------------------------------------
set -euo pipefail
app_name="$1"
echo "::group::Initialize Terraform"
echo terraform -chdir="infra/${app_name}/service" init -input=false -reconfigure -backend-config="dev.s3.tfbackend"
terraform -chdir="infra/${app_name}/service" init -input=false -reconfigure -backend-config="dev.s3.tfbackend"
echo "::endgroup::"
echo "::group::List PRs with PR environments"
echo terraform -chdir="infra/${app_name}/service" workspace list
workspaces="$(terraform -chdir="infra/${app_name}/service" workspace list)"
# Search for workspaces that start with "p-" and extract the PR numbers
# The "|| true" is used to avoid errors if there are no PR environments
pr_nums="$(echo "${workspaces}" | grep -o 'p-[0-9]\+' || true | sed 's/p-//')"
echo "PRs"
echo "${pr_nums}"
echo "::endgroup::"
echo "::group::Check status of each PR"
closed_prs=()
for pr_num in $pr_nums; do
pr_status="$(gh pr view "$pr_num" --json state --jq ".state")"
echo "PR ${pr_num}: ${pr_status}"
if [ "$pr_status" == "CLOSED" ]; then
closed_prs+=("$pr_num")
fi
done
echo "::endgroup::"
# if closed_prs is not empty exit with 1 otherwise exit with 0
if [ ${#closed_prs[@]} -gt 0 ]; then
echo "Found orphaned PR environments for the following PRs: ${closed_prs[*]}"
echo "Found orphaned PR environments for the following PRs: ${closed_prs[*]}" >> "${GITHUB_STEP_SUMMARY}"
exit 1
fi
echo "No orphaned PR environments"
echo "No orphaned PR environments" >> "${GITHUB_STEP_SUMMARY}"