Skip to content

Commit bcb37bd

Browse files
committed
Adds GitHub action for linting documentation
1 parent 5d7f6b4 commit bcb37bd

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

.actrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Needed for testing our workflows
2+
-P ubuntu-latest=nektos/act-environments-ubuntu:18.04

.github/workflows/README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,4 @@ steps:
1717
1818
The tool [`act`](https://github.com/nektos/act) can be used to test GitHub workflows locally. The default container [intentionally does not have feature parity](https://github.com/nektos/act#default-runners-are-intentionally-incomplete) with the containers used in GitHub due to the size of a full container.
1919

20-
A fully-featured container can be used by specifying a different container.
21-
22-
```console
23-
act -P ubuntu-latest=nektos/act-environments-ubuntu:18.04
24-
```
20+
The file `./actrc` configures `act` to use a fully-featured container.
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
name: Website Checks
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- 'release/**'
7+
pull_request:
8+
paths:
9+
- .github/workflows/validate-terraform.yml
10+
- website/docs/**
11+
12+
jobs:
13+
validate-code:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: actions/cache@v2
18+
with:
19+
path: ~/go/pkg/mod
20+
key: ${{ runner.os }}-go-pkg-mod-${{ hashFiles('go.sum') }}
21+
- uses: actions/setup-go@v2
22+
with:
23+
go-version: "1.14"
24+
- name: install terrafmt
25+
run: |
26+
# go get github.com/katbyte/terrafmt
27+
git clone --branch json-output --single-branch https://github.com/gdavison/terrafmt terrafmt
28+
cd terrafmt
29+
go install
30+
- name: install tflint
31+
env:
32+
TFLINT_VERSION: "v0.18.0"
33+
run: |
34+
curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | sh
35+
- name: lint code
36+
shell: bash
37+
run: |
38+
# Configure the rules for tflint.
39+
# The *_invalid_* rules disabled here prevent evaluation of expressions.
40+
# Do not disable *_invalid_name rules, since these are good checks for e.g. "%s" formatting verbs
41+
# being carried over from test cases.
42+
shared_rules=(
43+
"--enable-rule=terraform_comment_syntax"
44+
"--disable-rule=aws_cognito_user_pool_domain_invalid_domain"
45+
"--disable-rule=aws_db_instance_default_parameter_group"
46+
"--disable-rule=aws_elasticache_cluster_default_parameter_group"
47+
"--disable-rule=aws_iam_saml_provider_invalid_saml_metadata_document"
48+
"--disable-rule=aws_iam_server_certificate_invalid_certificate_body"
49+
"--disable-rule=aws_iam_server_certificate_invalid_private_key"
50+
"--disable-rule=aws_transfer_ssh_key_invalid_body"
51+
"--disable-rule=aws_worklink_website_certificate_authority_association_invalid_certificate"
52+
)
53+
for filename in $(find ./website -type f \( -name '*.md' -o -name '*.markdown' \) | sort -u); do
54+
exit_code=0
55+
block_number=0
56+
57+
rules=("${shared_rules[@]}")
58+
if [[ "$filename" == "./website/docs/guides/version-2-upgrade.html.md" ]]; then
59+
# ./website/docs/guides/version-2-upgrade.html.md should still include pre-0.12 syntax,
60+
# since v1.0 does not support Terraform 0.12.
61+
rules+=(
62+
"--disable-rule=terraform_deprecated_interpolation"
63+
"--disable-rule=terraform_deprecated_index"
64+
)
65+
else
66+
rules+=(
67+
"--enable-rule=terraform_deprecated_interpolation"
68+
"--enable-rule=terraform_deprecated_index"
69+
)
70+
fi
71+
72+
while IFS= read -r block ; do
73+
((block_number+=1))
74+
start_line=$(echo "$block" | jq '.start_line')
75+
end_line=$(echo "$block" | jq '.end_line')
76+
text=$(echo "$block" | jq --raw-output '.text')
77+
78+
td=$(mktemp -d)
79+
tf="$td/main.tf"
80+
81+
echo "$text" > "$tf"
82+
83+
# We need to capture the output and error code here. We don't want to exit on the first error
84+
set +e
85+
tflint_output=$(tflint "${rules[@]}" "$tf" 2>&1)
86+
tflint_exitcode=$?
87+
set -e
88+
89+
if [ $tflint_exitcode -ne 0 ]; then
90+
echo "ERROR: File \"$filename\", block #$block_number (lines $start_line-$end_line):"
91+
echo "$tflint_output"
92+
echo
93+
exit_code=1
94+
fi
95+
done < <( terrafmt blocks --json "$filename" | jq --compact-output '.blocks[]?' )
96+
done
97+
98+
exit $exit_code

0 commit comments

Comments
 (0)