|
| 1 | +version: 2 |
| 2 | + |
| 3 | +jobs: |
| 4 | + build: |
| 5 | + docker: |
| 6 | + - image: hashicorp/terraform:0.11.3 |
| 7 | + entrypoint: /bin/sh |
| 8 | + steps: |
| 9 | + - checkout |
| 10 | + - run: |
| 11 | + name: "Validate tf files (terraform validate)" |
| 12 | + command: | |
| 13 | + find . -type f -name "*.tf" -exec dirname {} \;|sort -u | while read m; do (terraform validate -check-variables=false "$m" && echo "√ $m") || exit 1 ; done |
| 14 | + - run: |
| 15 | + name: "Check: Terraform formatting (terraform fmt)" |
| 16 | + command: | |
| 17 | + if [ `terraform fmt --list=true -diff=true -write=false | tee format-issues | wc -c` -ne 0 ]; then |
| 18 | + echo "Some terraform files need be formatted, run 'terraform fmt' to fix" |
| 19 | + echo "Formatting issues:" |
| 20 | + cat format-issues |
| 21 | + exit 1 |
| 22 | + fi |
| 23 | + - run: |
| 24 | + name: "Install: tflint" |
| 25 | + command: | |
| 26 | + apk add jq wget |
| 27 | + # Get latest version of tflint |
| 28 | + pkg_arch=linux_amd64 |
| 29 | + dl_url=$(curl -s https://api.github.com/repos/wata727/tflint/releases/latest | jq -r ".assets[] | select(.name | test(\"${pkg_arch}\")) | .browser_download_url") |
| 30 | + wget ${dl_url} |
| 31 | + unzip tflint_linux_amd64.zip |
| 32 | + mkdir -p /usr/local/tflint/bin |
| 33 | + # Setup PATH for later run steps - ONLY for Bash and not in Bash |
| 34 | + #echo 'export PATH=/usr/local/tflint/bin:$PATH' >> $BASH_ENV |
| 35 | + echo "Installing tflint..." |
| 36 | + install tflint /usr/local/tflint/bin |
| 37 | + echo "Configuring tflint..." |
| 38 | + tf_ver=$(terraform version | awk 'FNR <= 1' | cut -dv -f2) |
| 39 | + echo -e "\tConfig for terraform version: ${tf_ver}" |
| 40 | + if [ -f '.tflint.hcl' ]; then |
| 41 | + sed -i "/terraform_version =/s/\".*\"/\"${tf_ver}\"/" .tflint.hcl |
| 42 | + else |
| 43 | + { |
| 44 | + echo -e "config {\nterraform_version = \"${tf_ver}\"\ndeep_check = true\nignore_module = {" |
| 45 | + for module in $(grep -h '[^a-zA-Z]source[ =]' *.tf | sed -r 's/.*=\s+//' | sort -u); do |
| 46 | + # if not ^"../ |
| 47 | + echo "${module} = true" |
| 48 | + done |
| 49 | + echo "}}" |
| 50 | + } > .tflint.hcl |
| 51 | + fi |
| 52 | + echo "tflint configuration:" |
| 53 | + cat .tflint.hcl |
| 54 | + - run: |
| 55 | + # Not supporting modules from registry ?? v0.5.4 |
| 56 | + # For now, must ignore in config file |
| 57 | + name: "Check: tflint" |
| 58 | + command: | |
| 59 | + #echo "Initializing terraform..." |
| 60 | + #terraform init -input=false |
| 61 | + echo "Running tflint..." |
| 62 | + /usr/local/tflint/bin/tflint --version |
| 63 | + /usr/local/tflint/bin/tflint |
| 64 | +
|
| 65 | +workflows: |
| 66 | + version: 2 |
| 67 | + build: |
| 68 | + jobs: |
| 69 | + - build |
0 commit comments