Skip to content

Commit 12e2fc9

Browse files
nikovirtalalkysow
authored andcommitted
Add apply action.
1 parent b0bfe34 commit 12e2fc9

File tree

6 files changed

+103
-3
lines changed

6 files changed

+103
-3
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Terraform GitHub Actions
2-
These official Terraform GitHub Actions allow you to run `terraform fmt`, `validate`
3-
and `plan` on your pull requests to help you review and validate Terraform changes.
2+
These official Terraform GitHub Actions allow you to run `terraform fmt`, `validate`, `plan` and `apply` on your pull requests to help you review, validate and apply Terraform changes.
43

54
## Getting Started
65
To get started, check out our documentation: [https://www.terraform.io/docs/github-actions/getting-started/](https://www.terraform.io/docs/github-actions/getting-started/).
@@ -18,3 +17,7 @@ Runs `terraform validate` and comments back on error.
1817
### Plan Action
1918
Runs `terraform plan` and comments back with the output.
2019
<img src="./assets/plan.png" alt="Terraform Plan Action" width="80%" />
20+
21+
### Apply Action
22+
Runs `terraform apply` and comments back with the output.
23+
<img src="./assets/apply.png" alt="Terraform Apply Action" width="80%" />

apply/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM hashicorp/terraform:0.11.11
2+
3+
LABEL "com.github.actions.name"="terraform apply"
4+
LABEL "com.github.actions.description"="Run Terraform Apply"
5+
LABEL "com.github.actions.icon"="upload"
6+
LABEL "com.github.actions.color"="purple"
7+
8+
LABEL "repository"="https://github.com/hashicorp/terraform-github-actions"
9+
LABEL "homepage"="http://github.com/hashicorp/terraform-github-actions"
10+
LABEL "maintainer"="HashiCorp Terraform Team <[email protected]>"
11+
12+
RUN apk --no-cache add jq
13+
14+
COPY entrypoint.sh /entrypoint.sh
15+
ENTRYPOINT ["/entrypoint.sh"]

apply/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Terraform Apply Action
2+
Runs `terraform apply` and comments back on the pull request with the apply output.
3+
4+
See [https://www.terraform.io/docs/github-actions/actions/apply.html](https://www.terraform.io/docs/github-actions/actions/apply.html).

apply/entrypoint.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/sh
2+
3+
# wrap takes some output and wraps it in a collapsible markdown section if
4+
# it's over $TF_ACTION_WRAP_LINES long.
5+
wrap() {
6+
if [[ $(echo "$1" | wc -l) -gt ${TF_ACTION_WRAP_LINES:-20} ]]; then
7+
echo "
8+
<details><summary>Show Output</summary>
9+
10+
\`\`\`diff
11+
$1
12+
\`\`\`
13+
14+
</details>
15+
"
16+
else
17+
echo "
18+
\`\`\`diff
19+
$1
20+
\`\`\`
21+
"
22+
fi
23+
}
24+
25+
set -e
26+
27+
cd "${TF_ACTION_WORKING_DIR:-.}"
28+
29+
WORKSPACE=${TF_ACTION_WORKSPACE:-default}
30+
terraform workspace select "$WORKSPACE"
31+
32+
# Name the plan file based on selected workspace
33+
PLANFILE=${WORKSPACE}.tfplan
34+
35+
set +e
36+
OUTPUT=$(sh -c "TF_IN_AUTOMATION=true terraform apply -no-color $PLANFILE $*" 2>&1)
37+
SUCCESS=$?
38+
echo "$OUTPUT"
39+
set -e
40+
41+
if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
42+
exit $SUCCESS
43+
fi
44+
45+
# Build the comment we'll post to the PR.
46+
COMMENT=""
47+
if [ $SUCCESS -ne 0 ]; then
48+
OUTPUT=$(wrap "$OUTPUT")
49+
COMMENT="#### \`terraform apply\` Failed
50+
$OUTPUT"
51+
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+
# Call wrap to optionally wrap our output in a collapsible markdown section.
64+
OUTPUT=$(wrap "$OUTPUT")
65+
66+
COMMENT="#### \`terraform apply\` Success
67+
$OUTPUT"
68+
fi
69+
70+
# Post the comment.
71+
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
72+
COMMENTS_URL=$(cat /github/workflow/event.json | jq -r .pull_request.comments_url)
73+
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
74+
75+
exit $SUCCESS

assets/apply.png

32.8 KB
Loading

plan/entrypoint.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ 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+
4144
set +e
42-
OUTPUT=$(sh -c "TF_IN_AUTOMATION=true terraform plan -no-color -input=false $*" 2>&1)
45+
OUTPUT=$(sh -c "TF_IN_AUTOMATION=true terraform plan -no-color -input=false -out=$PLANFILE $*" 2>&1)
4346
SUCCESS=$?
4447
echo "$OUTPUT"
4548
set -e

0 commit comments

Comments
 (0)