@@ -7,15 +7,15 @@ wrap() {
7
7
echo "
8
8
<details><summary>Show Output</summary>
9
9
10
- \`\`\` diff
10
+ \`\`\`
11
11
$1
12
12
\`\`\`
13
13
14
14
</details>
15
15
"
16
16
else
17
17
echo "
18
- \`\`\` diff
18
+ \`\`\`
19
19
$1
20
20
\`\`\`
21
21
"
@@ -26,19 +26,28 @@ set -e
26
26
27
27
cd " ${TF_ACTION_WORKING_DIR:- .} "
28
28
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
31
36
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
34
40
35
41
set +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 )
37
43
SUCCESS=$?
38
44
echo " $OUTPUT "
39
45
set -e
40
46
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
42
51
exit $SUCCESS
43
52
fi
44
53
@@ -47,24 +56,14 @@ COMMENT=""
47
56
if [ $SUCCESS -ne 0 ]; then
48
57
OUTPUT=$( wrap " $OUTPUT " )
49
58
COMMENT=" #### \` terraform apply\` Failed
50
- $OUTPUT "
59
+ $OUTPUT
60
+ *Workflow: \` $GITHUB_WORKFLOW \` , Action: \` $GITHUB_ACTION \` *"
51
61
else
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
-
63
62
# Call wrap to optionally wrap our output in a collapsible markdown section.
64
63
OUTPUT=$( wrap " $OUTPUT " )
65
-
66
64
COMMENT=" #### \` terraform apply\` Success
67
- $OUTPUT "
65
+ $OUTPUT
66
+ *Workflow: \` $GITHUB_WORKFLOW \` , Action: \` $GITHUB_ACTION \` *"
68
67
fi
69
68
70
69
# Post the comment.
0 commit comments