Skip to content

Commit 9abe306

Browse files
authored
Merge pull request hashicorp#22 from hashicorp/apply
Implement apply action
2 parents b0bfe34 + 92dc8b7 commit 9abe306

File tree

10 files changed

+161
-3
lines changed

10 files changed

+161
-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.12.0
2+
3+
LABEL "com.github.actions.name"="terraform apply"
4+
LABEL "com.github.actions.description"="Run Terraform Apply"
5+
LABEL "com.github.actions.icon"="play-circle"
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 curl
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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
\`\`\`
11+
$1
12+
\`\`\`
13+
14+
</details>
15+
"
16+
else
17+
echo "
18+
\`\`\`
19+
$1
20+
\`\`\`
21+
"
22+
fi
23+
}
24+
25+
set -e
26+
27+
cd "${TF_ACTION_WORKING_DIR:-.}"
28+
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
36+
37+
if [[ ! -z "$TF_ACTION_WORKSPACE" ]] && [[ "$TF_ACTION_WORKSPACE" != "default" ]]; then
38+
terraform workspace select "$TF_ACTION_WORKSPACE"
39+
fi
40+
41+
set +e
42+
OUTPUT=$(sh -c "TF_IN_AUTOMATION=true terraform apply -no-color -auto-approve -input=false $*" 2>&1)
43+
SUCCESS=$?
44+
echo "$OUTPUT"
45+
set -e
46+
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
51+
exit $SUCCESS
52+
fi
53+
54+
# Build the comment we'll post to the PR.
55+
COMMENT=""
56+
if [ $SUCCESS -ne 0 ]; then
57+
OUTPUT=$(wrap "$OUTPUT")
58+
COMMENT="#### \`terraform apply\` Failed
59+
$OUTPUT
60+
61+
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
62+
else
63+
# Call wrap to optionally wrap our output in a collapsible markdown section.
64+
OUTPUT=$(wrap "$OUTPUT")
65+
COMMENT="#### \`terraform apply\` Success
66+
$OUTPUT
67+
68+
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
69+
fi
70+
71+
# Post the comment.
72+
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
73+
COMMENTS_URL=$(cat /github/workflow/event.json | jq -r .pull_request.comments_url)
74+
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
75+
76+
exit $SUCCESS

assets/apply.png

32.8 KB
Loading

base-branch-filter/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM alpine:latest
2+
3+
LABEL "com.github.actions.name"="base branch filter"
4+
LABEL "com.github.actions.description"="Filters pull request events based on their base branch."
5+
LABEL "com.github.actions.icon"="filter"
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"]

base-branch-filter/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Base Branch Filter
2+
Filters pull request events depending on the base branch
3+
The base branch is the branch that the pull request will be merged into.
4+
5+
To use, set `args` to a regular expression that
6+
will be matched against the destination branch.
7+
8+
Note: This action only works on pull request events.
9+
10+
## Example
11+
Filter on pull requests that have been merged into `master`:
12+
```hcl
13+
workflow "example" {
14+
resolves = "base-branch-filter"
15+
# Must be used on pull_request events.
16+
on = "pull_request"
17+
}
18+
19+
# First we use another filter to filter to only merged events.
20+
action "merged-prs-filter" {
21+
uses = "actions/bin/filter@master"
22+
args = "merged true"
23+
}
24+
25+
# Then we use this filter to ensure the branch matches "master".
26+
action "base-branch-filter" {
27+
uses = "hashicorp/terraform-github-actions/base-branc-filter@master"
28+
# We set args to our regex.
29+
args = "^master$"
30+
needs = "merged-prs-filter"
31+
}
32+
```

base-branch-filter/entrypoint.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
regex="$*"
6+
base_branch=$(jq -r .pull_request.base.ref "$GITHUB_EVENT_PATH")
7+
8+
if "$actual" | grep -q "$regex"; then
9+
echo "base branch \"$base_branch\" does not match \"$regex\""
10+
exit 78
11+
fi

init/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM hashicorp/terraform:0.12.0
22

33
LABEL "com.github.actions.name"="terraform init"
44
LABEL "com.github.actions.description"="Run terraform init"
5-
LABEL "com.github.actions.icon"="play-circle"
5+
LABEL "com.github.actions.icon"="download"
66
LABEL "com.github.actions.color"="purple"
77

88
LABEL "repository"="https://github.com/hashicorp/terraform-github-actions"

plan/entrypoint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ if [ $SUCCESS -ne 0 ]; then
5454
OUTPUT=$(wrap "$OUTPUT")
5555
COMMENT="#### \`terraform plan\` Failed
5656
$OUTPUT
57+
5758
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
5859
else
5960
# Remove "Refreshing state..." lines by only keeping output after the
@@ -72,6 +73,7 @@ else
7273

7374
COMMENT="#### \`terraform plan\` Success
7475
$OUTPUT
76+
7577
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
7678
fi
7779

0 commit comments

Comments
 (0)