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

Commit 49492a0

Browse files
Adding terraform taint actions (#134)
* Adding terraform taint option * Update examples/tainting.md Suggested changes. Co-Authored-By: Matthew Sanabria <[email protected]> * Update examples/tainting.md Suggested changes. Co-Authored-By: Matthew Sanabria <[email protected]> Co-authored-by: Matthew Sanabria <[email protected]>
1 parent 271eb39 commit 49492a0

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ An exit code of `0` is considered a successful execution.
1212

1313
## Usage
1414

15-
The most common workflow is to run `terraform fmt`, `terraform init`, `terraform validate`, and `terraform plan` on all of the Terraform files in the root of the repository when a pull request is opened or updated. A comment will be posted to the pull request depending on the output of the Terraform subcommand being executed. This workflow can be configured by adding the following content to the GitHub Actions workflow YAML file.
15+
The most common workflow is to run `terraform fmt`, `terraform init`, `terraform validate`, `terraform plan`, and `terraform taint` on all of the Terraform files in the root of the repository when a pull request is opened or updated. A comment will be posted to the pull request depending on the output of the Terraform subcommand being executed. This workflow can be configured by adding the following content to the GitHub Actions workflow YAML file.
1616

1717
```yaml
1818
name: 'Terraform GitHub Actions'

examples/tainting.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Terraform Tainting
2+
3+
Resources to taint can be specified using the `args` with option.
4+
5+
```yaml
6+
name: 'Terraform GitHub Actions'
7+
on:
8+
- pull_request
9+
jobs:
10+
terraform:
11+
name: 'Terraform'
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: 'Checkout'
15+
uses: actions/checkout@master
16+
- name: 'Terraform Init'
17+
uses: hashicorp/terraform-github-actions@master
18+
with:
19+
tf_actions_version: 0.12.13
20+
tf_actions_subcommand: 'init'
21+
tf_actions_working_dir: '.'
22+
tf_actions_comment: true
23+
env:
24+
TF_WORKSPACE: dev
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
- name: 'Terraform Taint'
27+
uses: hashicorp/terraform-github-actions@master
28+
with:
29+
tf_actions_version: 0.12.13
30+
tf_actions_subcommand: 'taint'
31+
tf_actions_working_dir: '.'
32+
tf_actions_comment: true
33+
args: 'aws_instance.host'
34+
env:
35+
TF_WORKSPACE: dev
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
```
38+
39+
Multiple resources can be specified by separating with spaces: `args: 'aws_instance.host1 aws_instance.host2'`.

src/main.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function main {
103103
source ${scriptDir}/terraform_apply.sh
104104
source ${scriptDir}/terraform_output.sh
105105
source ${scriptDir}/terraform_import.sh
106+
source ${scriptDir}/terraform_taint.sh
106107

107108
parseInputs
108109
configureCLICredentials
@@ -137,6 +138,10 @@ function main {
137138
installTerraform
138139
terraformImport ${*}
139140
;;
141+
taint)
142+
installTerraform
143+
terraformTaint ${*}
144+
;;
140145
*)
141146
echo "Error: Must provide a valid value for terraform_subcommand"
142147
exit 1

src/terraform_taint.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
function terraformTaint {
4+
# Gather the output of `terraform taint`.
5+
echo "taint: info: tainting Terraform configuration in ${tfWorkingDir}"
6+
#taintOutput=$(terraform taint ${*} 2>&1)
7+
taintOutput=$(for resource in ${*}; do terraform taint -allow-missing $resource; done 2>&1)
8+
taintExitCode=${?}
9+
taintCommentStatus="Failed"
10+
11+
# Exit code of 0 indicates success with no changes. Print the output and exit.
12+
if [ ${taintExitCode} -eq 0 ]; then
13+
taintCommentStatus="Success"
14+
echo "taint: info: successfully tainted Terraform configuration in ${tfWorkingDir}"
15+
echo "${taintOutput}"
16+
echo
17+
exit ${taintExitCode}
18+
fi
19+
20+
# Exit code of !0 indicates failure.
21+
if [ ${taintExitCode} -ne 0 ]; then
22+
echo "taint: error: failed to taint Terraform configuration in ${tfWorkingDir}"
23+
echo "${taintOutput}"
24+
echo
25+
fi
26+
27+
# Comment on the pull request if necessary.
28+
if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ]; then
29+
taintCommentWrapper="#### \`terraform taint\` ${taintCommentStatus}
30+
<details><summary>Show Output</summary>
31+
32+
\`\`\`
33+
${taintOutput}
34+
\`\`\`
35+
36+
</details>
37+
38+
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*"
39+
40+
taintCommentWrapper=$(stripColors "${taintCommentWrapper}")
41+
echo "taint: info: creating JSON"
42+
taintPayload=$(echo "${taintCommentWrapper}" | jq -R --slurp '{body: .}')
43+
taintCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
44+
echo "taint: info: commenting on the pull request"
45+
echo "${taintPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${taintCommentsURL}" > /dev/null
46+
fi
47+
48+
exit ${taintExitCode}
49+
}

0 commit comments

Comments
 (0)