|
| 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