This repository was archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathterraform_plan.sh
executable file
·75 lines (62 loc) · 2.95 KB
/
terraform_plan.sh
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
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
function terraformPlan {
# Gather the output of `terraform plan`.
echo "plan: info: planning Terraform configuration in ${tfWorkingDir}"
planOutput=$(terraform plan -detailed-exitcode -input=false ${*} 2>&1)
planExitCode=${?}
planHasChanges=false
planCommentStatus="Failed"
# Exit code of 0 indicates success with no changes. Print the output and exit.
if [ ${planExitCode} -eq 0 ]; then
echo "plan: info: successfully planned Terraform configuration in ${tfWorkingDir}"
echo "${planOutput}"
echo
echo ::set-output name=tf_actions_plan_has_changes::${planHasChanges}
exit ${planExitCode}
fi
# Exit code of 2 indicates success with changes. Print the output, change the
# exit code to 0, and mark that the plan has changes.
if [ ${planExitCode} -eq 2 ]; then
planExitCode=0
planHasChanges=true
planCommentStatus="Success"
echo "plan: info: successfully planned Terraform configuration in ${tfWorkingDir}"
echo "${planOutput}"
echo
if echo "${planOutput}" | egrep '^-{72}$' &> /dev/null; then
planOutput=$(echo "${planOutput}" | sed -n -r '/-{72}/,/-{72}/{ /-{72}/d; p }')
fi
planOutput=$(echo "${planOutput}" | sed -r -e 's/^ \+/\+/g' | sed -r -e 's/^ ~/~/g' | sed -r -e 's/^ -/-/g')
# If output is longer than max length (65536 characters), keep last part
planOutput=$(echo "${planOutput}" | tail -c 65000 )
fi
# Exit code of !0 indicates failure.
if [ ${planExitCode} -ne 0 ]; then
echo "plan: error: failed to plan Terraform configuration in ${tfWorkingDir}"
echo "${planOutput}"
echo
fi
# Comment on the pull request if necessary.
if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ] && ([ "${planHasChanges}" == "true" ] || [ "${planCommentStatus}" == "Failed" ]); then
planCommentWrapper="#### \`terraform plan\` ${planCommentStatus}
<details><summary>Show Output</summary>
\`\`\`
${planOutput}
\`\`\`
</details>
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*"
planCommentWrapper=$(stripColors "${planCommentWrapper}")
echo "plan: info: creating JSON"
planPayload=$(echo "${planCommentWrapper}" | jq -R --slurp '{body: .}')
planCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
echo "plan: info: commenting on the pull request"
echo "${planPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${planCommentsURL}" > /dev/null
fi
echo ::set-output name=tf_actions_plan_has_changes::${planHasChanges}
# https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/m-p/38372/highlight/true#M3322
planOutput="${planOutput//'%'/'%25'}"
planOutput="${planOutput//$'\n'/'%0A'}"
planOutput="${planOutput//$'\r'/'%0D'}"
echo "::set-output name=tf_actions_plan_output::${planOutput}"
exit ${planExitCode}
}