Skip to content

Commit

Permalink
Clean up @nikovirtala's apply work.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkysow committed May 27, 2019
1 parent 12e2fc9 commit 6eb7500
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
6 changes: 3 additions & 3 deletions apply/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
FROM hashicorp/terraform:0.11.11
FROM hashicorp/terraform:0.12.0

LABEL "com.github.actions.name"="terraform apply"
LABEL "com.github.actions.description"="Run Terraform Apply"
LABEL "com.github.actions.icon"="upload"
LABEL "com.github.actions.icon"="play-circle"
LABEL "com.github.actions.color"="purple"

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"]
43 changes: 21 additions & 22 deletions apply/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ wrap() {
echo "
<details><summary>Show Output</summary>
\`\`\`diff
\`\`\`
$1
\`\`\`
</details>
"
else
echo "
\`\`\`diff
\`\`\`
$1
\`\`\`
"
Expand All @@ -26,19 +26,28 @@ set -e

cd "${TF_ACTION_WORKING_DIR:-.}"

WORKSPACE=${TF_ACTION_WORKSPACE:-default}
terraform workspace select "$WORKSPACE"
if [[ ! -z "$TF_ACTION_TFE_TOKEN" ]]; then
cat > ~/.terraformrc << EOF
credentials "${TF_ACTION_TFE_HOSTNAME:-app.terraform.io}" {
token = "$TF_ACTION_TFE_TOKEN"
}
EOF
fi

# Name the plan file based on selected workspace
PLANFILE=${WORKSPACE}.tfplan
if [[ ! -z "$TF_ACTION_WORKSPACE" ]] && [[ "$TF_ACTION_WORKSPACE" != "default" ]]; then
terraform workspace select "$TF_ACTION_WORKSPACE"
fi

set +e
OUTPUT=$(sh -c "TF_IN_AUTOMATION=true terraform apply -no-color $PLANFILE $*" 2>&1)
OUTPUT=$(sh -c "TF_IN_AUTOMATION=true terraform apply -no-color -auto-approve -input=false $*" 2>&1)
SUCCESS=$?
echo "$OUTPUT"
set -e

if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
# If PR_DATA is null, then this is not a pull request event and so there's
# no where to comment.
PR_DATA=$(cat /github/workflow/event.json | jq -r .pull_request)
if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ] || [ "$PR_DATA" = "null" ]; then
exit $SUCCESS
fi

Expand All @@ -47,24 +56,14 @@ COMMENT=""
if [ $SUCCESS -ne 0 ]; then
OUTPUT=$(wrap "$OUTPUT")
COMMENT="#### \`terraform apply\` Failed
$OUTPUT"
$OUTPUT
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
else
# Remove "Refreshing state..." lines by only keeping output after the
# delimiter (72 dashes) that represents the end of the refresh stage.
# We do this to keep the comment output smaller.
if echo "$OUTPUT" | egrep '^-{72}$'; then
OUTPUT=$(echo "$OUTPUT" | sed -n -r '/-{72}/,/-{72}/{ /-{72}/d; p }')
fi

# Remove whitespace at the beginning of the line for added/modified/deleted
# resources so the diff markdown formatting highlights those lines.
OUTPUT=$(echo "$OUTPUT" | sed -r -e 's/^ \+/\+/g' | sed -r -e 's/^ ~/~/g' | sed -r -e 's/^ -/-/g')

# Call wrap to optionally wrap our output in a collapsible markdown section.
OUTPUT=$(wrap "$OUTPUT")

COMMENT="#### \`terraform apply\` Success
$OUTPUT"
$OUTPUT
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
fi

# Post the comment.
Expand Down
5 changes: 1 addition & 4 deletions plan/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ if [[ ! -z "$TF_ACTION_WORKSPACE" ]] && [[ "$TF_ACTION_WORKSPACE" != "default" ]
terraform workspace select "$TF_ACTION_WORKSPACE"
fi

# Name the plan file based on selected workspace
PLANFILE=${WORKSPACE}.tfplan

set +e
OUTPUT=$(sh -c "TF_IN_AUTOMATION=true terraform plan -no-color -input=false -out=$PLANFILE $*" 2>&1)
OUTPUT=$(sh -c "TF_IN_AUTOMATION=true terraform plan -no-color -input=false $*" 2>&1)
SUCCESS=$?
echo "$OUTPUT"
set -e
Expand Down

0 comments on commit 6eb7500

Please sign in to comment.