@@ -7,15 +7,15 @@ wrap() {
77 echo "
88<details><summary>Show Output</summary>
99
10- \`\`\` diff
10+ \`\`\`
1111$1
1212\`\`\`
1313
1414</details>
1515"
1616else
1717 echo "
18- \`\`\` diff
18+ \`\`\`
1919$1
2020\`\`\`
2121"
@@ -26,19 +26,28 @@ set -e
2626
2727cd " ${TF_ACTION_WORKING_DIR:- .} "
2828
29- WORKSPACE=${TF_ACTION_WORKSPACE:- default}
30- terraform workspace select "$WORKSPACE"
29+ if [[ ! -z " $TF_ACTION_TFE_TOKEN " ]]; then
30+ cat > ~ /.terraformrc << EOF
31+ credentials "${TF_ACTION_TFE_HOSTNAME:- app.terraform.io} " {
32+ token = "$TF_ACTION_TFE_TOKEN "
33+ }
34+ EOF
35+ fi
3136
32- # Name the plan file based on selected workspace
33- PLANFILE=${WORKSPACE} .tfplan
37+ if [[ ! -z " $TF_ACTION_WORKSPACE " ]] && [[ " $TF_ACTION_WORKSPACE " != " default" ]]; then
38+ terraform workspace select "$TF_ACTION_WORKSPACE"
39+ fi
3440
3541set +e
36- OUTPUT=$( sh -c " TF_IN_AUTOMATION=true terraform apply -no-color $PLANFILE $* " 2>&1 )
42+ OUTPUT=$( sh -c " TF_IN_AUTOMATION=true terraform apply -no-color -auto-approve -input=false $* " 2>&1 )
3743SUCCESS=$?
3844echo " $OUTPUT "
3945set -e
4046
41- if [ " $TF_ACTION_COMMENT " = " 1" ] || [ " $TF_ACTION_COMMENT " = " false" ]; then
47+ # If PR_DATA is null, then this is not a pull request event and so there's
48+ # no where to comment.
49+ PR_DATA=$( cat /github/workflow/event.json | jq -r .pull_request)
50+ if [ " $TF_ACTION_COMMENT " = " 1" ] || [ " $TF_ACTION_COMMENT " = " false" ] || [ " $PR_DATA " = " null" ]; then
4251 exit $SUCCESS
4352fi
4453
@@ -47,24 +56,14 @@ COMMENT=""
4756if [ $SUCCESS -ne 0 ]; then
4857 OUTPUT=$( wrap " $OUTPUT " )
4958 COMMENT=" #### \` terraform apply\` Failed
50- $OUTPUT "
59+ $OUTPUT
60+ *Workflow: \` $GITHUB_WORKFLOW \` , Action: \` $GITHUB_ACTION \` *"
5161else
52- # Remove "Refreshing state..." lines by only keeping output after the
53- # delimiter (72 dashes) that represents the end of the refresh stage.
54- # We do this to keep the comment output smaller.
55- if echo " $OUTPUT " | egrep ' ^-{72}$' ; then
56- OUTPUT=$( echo " $OUTPUT " | sed -n -r ' /-{72}/,/-{72}/{ /-{72}/d; p }' )
57- fi
58-
59- # Remove whitespace at the beginning of the line for added/modified/deleted
60- # resources so the diff markdown formatting highlights those lines.
61- OUTPUT=$( echo " $OUTPUT " | sed -r -e ' s/^ \+/\+/g' | sed -r -e ' s/^ ~/~/g' | sed -r -e ' s/^ -/-/g' )
62-
6362 # Call wrap to optionally wrap our output in a collapsible markdown section.
6463 OUTPUT=$( wrap " $OUTPUT " )
65-
6664 COMMENT=" #### \` terraform apply\` Success
67- $OUTPUT "
65+ $OUTPUT
66+ *Workflow: \` $GITHUB_WORKFLOW \` , Action: \` $GITHUB_ACTION \` *"
6867fi
6968
7069# Post the comment.
0 commit comments