-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdestroy-pr-environment
More file actions
executable file
·59 lines (47 loc) · 2.24 KB
/
destroy-pr-environment
File metadata and controls
executable file
·59 lines (47 loc) · 2.24 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
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Destroy the temporary environment that was created for the pull request.
#
# Positional parameters:
# app_name (required) – the name of subdirectory of /infra that holds the
# application's infrastructure code.
# environment - the name of the application environment (e.g. dev, staging, prod)
# pr_number - the pull request number in GitHub
# -----------------------------------------------------------------------------
set -euo pipefail
app_name="$1"
environment="$2"
pr_number="$3"
workspace="p-${pr_number}"
echo "::group::Initialize Terraform with backend for environment: ${environment}"
terraform -chdir="infra/${app_name}/service" init -backend-config="${environment}.s3.tfbackend"
echo "::endgroup::"
echo "Select Terraform workspace: ${workspace}"
terraform -chdir="infra/${app_name}/service" workspace select "${workspace}"
echo "::group::Destroy resources"
terraform -chdir="infra/${app_name}/service" destroy -var="environment_name=${environment}" -input=false -auto-approve
echo "::endgroup::"
echo "Select default workspace"
terraform -chdir="infra/${app_name}/service" workspace select default
echo "Delete workspace: ${workspace}"
terraform -chdir="infra/${app_name}/service" workspace delete "${workspace}"
pr_info=$(cat <<EOF
<!-- ${app_name} - begin PR environment info -->
## Preview environment for ${app_name}
♻️ Environment destroyed ♻️
<!-- ${app_name} - end PR environment info -->
EOF
)
pr_body="$(gh pr view "${pr_number}" --json body | jq --raw-output .body)"
# clean up older single-app section if present
if [[ $pr_body == *"<!-- begin PR environment info -->"*"<!-- end PR environment info -->"* ]]; then
pr_body="${pr_body//<!-- begin PR environment info -->*<!-- end PR environment info -->}"
fi
if [[ $pr_body == *"<!-- ${app_name} - begin PR environment info -->"*"<!-- ${app_name} - end PR environment info -->"* ]]; then
pr_body="${pr_body//<!-- ${app_name} - begin PR environment info -->*<!-- ${app_name} - end PR environment info -->/$pr_info}"
else
pr_body="${pr_body}"$'\n\n'"${pr_info}"
fi
echo "Update PR description with PR environment info"
echo "${pr_info}"
gh pr edit "${pr_number}" --body "${pr_body}"