Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #21 from hashicorp/012
Browse files Browse the repository at this point in the history
Adds support for terraform v0.12.0
  • Loading branch information
lkysow committed May 27, 2019
2 parents b1f327c + b966ff2 commit b0bfe34
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions fmt/Dockerfile
@@ -1,4 +1,4 @@
FROM hashicorp/terraform:0.11.13
FROM hashicorp/terraform:0.12.0

LABEL "com.github.actions.name"="terraform fmt"
LABEL "com.github.actions.description"="Validate terraform files are formatted"
Expand All @@ -9,7 +9,7 @@ LABEL "repository"="https://github.com/hashicorp/terraform-github-actions"
LABEL "homepage"="http://github.com/hashicorp/terraform-github-actions"
LABEL "maintainer"="HashiCorp Terraform Team <terraform@hashicorp.com>"

RUN apk --no-cache add jq
RUN apk --no-cache add jq curl

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
35 changes: 23 additions & 12 deletions fmt/entrypoint.sh
Expand Up @@ -3,9 +3,9 @@ set -e
cd "${TF_ACTION_WORKING_DIR:-.}"

set +e
UNFMT_FILES=$(sh -c "terraform fmt -check=true -write=false $*" 2>&1)
OUTPUT=$(sh -c "terraform fmt -no-color -check -list -recursive $*" 2>&1)
SUCCESS=$?
echo "$UNFMT_FILES"
echo "$OUTPUT"
set -e

if [ $SUCCESS -eq 0 ]; then
Expand All @@ -16,27 +16,38 @@ if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
exit $SUCCESS
fi

# Iterate through each unformatted file and build up a comment.
FMT_OUTPUT=""
for file in $UNFMT_FILES; do
FILE_DIFF=$(terraform fmt -write=false -diff=true "$file" | sed -n '/@@.*/,//{/@@.*/d;p}')
FMT_OUTPUT="$FMT_OUTPUT
if [ $SUCCESS -eq 2 ]; then
# If it exits with 2, then there was a parse error and the command won't have
# printed out the files that have failed. In this case we comment back with the
# whole parse error.
COMMENT="\`\`\`
$OUTPUT
\`\`\`
"
else
# Otherwise the output will contain a list of unformatted filenames.
# Iterate through each file and build up a comment containing the diff
# of each file.
COMMENT=""
for file in $OUTPUT; do
FILE_DIFF=$(terraform fmt -no-color -write=false -diff "$file" | sed -n '/@@.*/,//{/@@.*/d;p}')
COMMENT="$COMMENT
<details><summary><code>$file</code></summary>
\`\`\`diff
$FILE_DIFF
\`\`\`
</details>
"
done
done
fi

COMMENT="#### \`terraform fmt\` Failed
$FMT_OUTPUT
COMMENT_WRAPPER="#### \`terraform fmt\` Failed
$COMMENT
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*
"
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT_WRAPPER" '.body = $body')
COMMENTS_URL=$(cat /github/workflow/event.json | jq -r .pull_request.comments_url)
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null

exit $SUCCESS

4 changes: 2 additions & 2 deletions init/Dockerfile
@@ -1,4 +1,4 @@
FROM hashicorp/terraform:0.11.13
FROM hashicorp/terraform:0.12.0

LABEL "com.github.actions.name"="terraform init"
LABEL "com.github.actions.description"="Run terraform init"
Expand All @@ -9,7 +9,7 @@ LABEL "repository"="https://github.com/hashicorp/terraform-github-actions"
LABEL "homepage"="http://github.com/hashicorp/terraform-github-actions"
LABEL "maintainer"="HashiCorp Terraform Team <terraform@hashicorp.com>"

RUN apk --no-cache add jq
RUN apk --no-cache add jq curl

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
4 changes: 2 additions & 2 deletions plan/Dockerfile
@@ -1,4 +1,4 @@
FROM hashicorp/terraform:0.11.13
FROM hashicorp/terraform:0.12.0

LABEL "com.github.actions.name"="terraform plan"
LABEL "com.github.actions.description"="Run terraform plan"
Expand All @@ -9,7 +9,7 @@ LABEL "repository"="https://github.com/hashicorp/terraform-github-actions"
LABEL "homepage"="http://github.com/hashicorp/terraform-github-actions"
LABEL "maintainer"="HashiCorp Terraform Team <terraform@hashicorp.com>"

RUN apk --no-cache add jq
RUN apk --no-cache add jq curl

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
4 changes: 2 additions & 2 deletions validate/Dockerfile
@@ -1,4 +1,4 @@
FROM hashicorp/terraform:0.11.13
FROM hashicorp/terraform:0.12.0

LABEL "com.github.actions.name"="terraform validate"
LABEL "com.github.actions.description"="Validate the terraform files in a directory"
Expand All @@ -9,7 +9,7 @@ LABEL "repository"="https://github.com/hashicorp/terraform-github-actions"
LABEL "homepage"="http://github.com/hashicorp/terraform-github-actions"
LABEL "maintainer"="HashiCorp Terraform Team <terraform@hashicorp.com>"

RUN apk --no-cache add jq
RUN apk --no-cache add jq curl

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

0 comments on commit b0bfe34

Please sign in to comment.