Skip to content

Commit 45b8cb0

Browse files
Add terraform destroy action (hashicorp#143)
* Add `destroy` subcommand option * Create `terraform_destroy` shell script * Add clean-up for failed `apply` * Fix typo in log message Co-Authored-By: Igor Veselinovic <[email protected]> * Remove `destroy` when `apply` fails Co-authored-by: Igor Veselinovic <[email protected]>
1 parent 27fd5b5 commit 45b8cb0

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/main.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ function main {
114114
source ${scriptDir}/terraform_output.sh
115115
source ${scriptDir}/terraform_import.sh
116116
source ${scriptDir}/terraform_taint.sh
117+
source ${scriptDir}/terraform_destroy.sh
117118

118119
parseInputs
119120
configureCLICredentials
@@ -152,6 +153,10 @@ function main {
152153
installTerraform
153154
terraformTaint ${*}
154155
;;
156+
destroy)
157+
installTerraform
158+
terraformDestroy ${*}
159+
;;
155160
*)
156161
echo "Error: Must provide a valid value for terraform_subcommand"
157162
exit 1

src/terraform_destroy.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
function terraformDestroy {
4+
# Gather the output of `terraform destroy`.
5+
echo "destroy: info: destroying Terraform-managed infrastructure in ${tfWorkingDir}"
6+
destroyOutput=$(terraform destroy -auto-approve -input=false ${*} 2>&1)
7+
destroyExitCode=${?}
8+
destroyCommentStatus="Failed"
9+
10+
# Exit code of 0 indicates success. Print the output and exit.
11+
if [ ${destroyExitCode} -eq 0 ]; then
12+
echo "destroy: info: successfully destroyed Terraform-managed infrastructure in ${tfWorkingDir}"
13+
echo "${destroyOutput}"
14+
echo
15+
destroyCommentStatus="Success"
16+
fi
17+
18+
# Exit code of !0 indicates failure.
19+
if [ ${destroyExitCode} -ne 0 ]; then
20+
echo "destroy: error: failed to destroy Terraform configuration in ${tfWorkingDir}"
21+
echo "${destroyOutput}"
22+
echo
23+
fi
24+
25+
# Comment on the pull request if necessary.
26+
if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ]; then
27+
destroyCommentWrapper="#### \`terraform destroy\` ${destroyCommentStatus}
28+
<details><summary>Show Output</summary>
29+
30+
\`\`\`
31+
${destroyOutput}
32+
\`\`\`
33+
34+
</details>
35+
36+
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*"
37+
38+
destroyCommentWrapper=$(stripColors "${destroyCommentWrapper}")
39+
echo "destroy: info: creating JSON"
40+
destroyPayload=$(echo "${destroyCommentWrapper}" | jq -R --slurp '{body: .}')
41+
destroyCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
42+
echo "destroy: info: commenting on the pull request"
43+
echo "${destroyPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${destroyCommentsURL}" > /dev/null
44+
fi
45+
46+
exit ${destroyExitCode}
47+
}

0 commit comments

Comments
 (0)