Skip to content
Snippets Groups Projects
Unverified Commit ef0db021 authored by Andrew Newdigate's avatar Andrew Newdigate
Browse files

fix: run terraform init prior to tflint

parent 75260596
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -187,6 +187,7 @@ Runs [`tflint`](https://github.com/terraform-linters/tflint) across all director
Setup process:
 
1. Ensure that a [`.tflint.hcl`](https://github.com/terraform-linters/tflint/blob/master/docs/user-guide/config.md) file exists in the root of the project.
1. Ensure that the `GL_ASDF_TERRAFORM_VERSION` version for terraform is configured.
1. Ensure that the `GL_ASDF_TFLINT_VERSION` version for tflint is configured.
1. Directories can be excluded from tflint using the `TFLINT_EXCLUDE_REGEX` variable. See the example below.
1. The task will generate a [junit test output file](https://docs.gitlab.com/ee/ci/unit_test_reports.html) for any failed linter checks.
Loading
Loading
@@ -198,6 +199,7 @@ stages:
# Not needed if .gitlab-ci-asdf-versions.yml is included...
variables:
GL_ASDF_TFLINT_VERSION: ...
GL_ASDF_TERRAFORM_VERSION: ...
# To exclude any directories, set this variable
TFLINT_EXCLUDE_REGEX: './exclude1|./exclude2'
 
Loading
Loading
Loading
Loading
@@ -2,18 +2,26 @@ tflint:
stage: validate
needs: []
image:
name: ghcr.io/terraform-linters/tflint:v${GL_ASDF_TFLINT_VERSION}
name: hashicorp/terraform:${GL_ASDF_TERRAFORM_VERSION}
entrypoint: ['/bin/sh', '-c']
before_script:
- apk add --no-cache curl bash unzip
- export TFLINT_VERSION=v${GL_ASDF_TFLINT_VERSION}
- curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
script:
- mkdir -p "tflint-reports/"
# Find all TF files and lint the directories of those files
- tflint --init -c .tflint.hcl
- find . -type f -name '*.tf' -exec dirname {} \;|sort -u|grep -v "${TFLINT_EXCLUDE_REGEX:-__none__}" | while read -r dir; do
junit_file="$(echo "$dir"|sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g).xml";
tflint -c ".tflint.hcl" --init "${dir}";
tflint -c ".tflint.hcl" "${dir}" -f compact || touch tflint-reports/failed;
tflint -c ".tflint.hcl" "${dir}" -f junit > "tflint-reports/${junit_file}" || touch tflint-reports/failed;
junit_file="$(echo "$dir"|sed -r 's/[^a-zA-Z0-9]+/-/g' | sed -r s/^-+\|-+$//g).xml";
echo "${dir} -------------------------------------------------------";
cd "${CI_PROJECT_DIR}/${dir}" || exit 1;
terraform init -backend=false -reconfigure;
tflint -c "${CI_PROJECT_DIR}/.tflint.hcl" . -f compact || touch "${CI_PROJECT_DIR}/tflint-reports/failed";
tflint -c "${CI_PROJECT_DIR}/.tflint.hcl" . -f junit > "${CI_PROJECT_DIR}/tflint-reports/${junit_file}" || touch "${CI_PROJECT_DIR}/tflint-reports/failed";
done;
if [ -f "tflint-reports/failed" ]; then exit 1; fi
# tflint-reports/failed indicates a linter failure
- if [ -f "${CI_PROJECT_DIR}/tflint-reports/failed" ]; then exit 1; fi
artifacts:
when: always
reports:
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment