Skip to content

Commit 6eb7500

Browse files
committed
Clean up @nikovirtala's apply work.
1 parent 12e2fc9 commit 6eb7500

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

apply/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
FROM hashicorp/terraform:0.11.11
1+
FROM hashicorp/terraform:0.12.0
22

33
LABEL "com.github.actions.name"="terraform apply"
44
LABEL "com.github.actions.description"="Run Terraform Apply"
5-
LABEL "com.github.actions.icon"="upload"
5+
LABEL "com.github.actions.icon"="play-circle"
66
LABEL "com.github.actions.color"="purple"
77

88
LABEL "repository"="https://github.com/hashicorp/terraform-github-actions"
99
LABEL "homepage"="http://github.com/hashicorp/terraform-github-actions"
1010
LABEL "maintainer"="HashiCorp Terraform Team <[email protected]>"
1111

12-
RUN apk --no-cache add jq
12+
RUN apk --no-cache add jq curl
1313

1414
COPY entrypoint.sh /entrypoint.sh
1515
ENTRYPOINT ["/entrypoint.sh"]

apply/entrypoint.sh

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ wrap() {
77
echo "
88
<details><summary>Show Output</summary>
99
10-
\`\`\`diff
10+
\`\`\`
1111
$1
1212
\`\`\`
1313
1414
</details>
1515
"
1616
else
1717
echo "
18-
\`\`\`diff
18+
\`\`\`
1919
$1
2020
\`\`\`
2121
"
@@ -26,19 +26,28 @@ set -e
2626

2727
cd "${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

3541
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)
3743
SUCCESS=$?
3844
echo "$OUTPUT"
3945
set -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
4352
fi
4453

@@ -47,24 +56,14 @@ COMMENT=""
4756
if [ $SUCCESS -ne 0 ]; then
4857
OUTPUT=$(wrap "$OUTPUT")
4958
COMMENT="#### \`terraform apply\` Failed
50-
$OUTPUT"
59+
$OUTPUT
60+
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
5161
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-
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\`*"
6867
fi
6968

7069
# Post the comment.

plan/entrypoint.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ if [[ ! -z "$TF_ACTION_WORKSPACE" ]] && [[ "$TF_ACTION_WORKSPACE" != "default" ]
3838
terraform workspace select "$TF_ACTION_WORKSPACE"
3939
fi
4040

41-
# Name the plan file based on selected workspace
42-
PLANFILE=${WORKSPACE}.tfplan
43-
4441
set +e
45-
OUTPUT=$(sh -c "TF_IN_AUTOMATION=true terraform plan -no-color -input=false -out=$PLANFILE $*" 2>&1)
42+
OUTPUT=$(sh -c "TF_IN_AUTOMATION=true terraform plan -no-color -input=false $*" 2>&1)
4643
SUCCESS=$?
4744
echo "$OUTPUT"
4845
set -e

0 commit comments

Comments
 (0)