-
Notifications
You must be signed in to change notification settings - Fork 2
88 lines (83 loc) · 3.98 KB
/
lint.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
name: Entur/Terraform/Lint
on:
workflow_call:
inputs:
directory:
description: "Path to Terraform Directory"
type: string
default: "terraform"
version:
description: "Terraform version"
type: string
default: "latest"
timeout_minutes:
description: "Job timeout in minutes"
type: number
default: 5
env:
GHA_TERRAFORM_LINT_DIRECTORY: ${{ inputs.directory }}
GHA_TERRAFORM_LINT_VERSION: ${{ inputs.version }}
jobs:
lint:
name: Terraform Lint
runs-on: ubuntu-24.04
timeout-minutes: ${{ inputs.timeout_minutes }}
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.GHA_TERRAFORM_LINT_VERSION }}
terraform_wrapper: false
- uses: terraform-linters/setup-tflint@v4
with:
tflint_wrapper: true
- run: |
LINT=TESTS
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NEUTRAL='\033[0m'
printf "%bChecking Terraform files for formatting issues: %b\n" "${YELLOW}" "${NEUTRAL}"
printf "#--------------------------------------------------------------------------------\n"
if ! terraform -chdir=${GHA_TERRAFORM_LINT_DIRECTORY} fmt -check -recursive; then
printf "#--------------------------------------------------------------------------------\n"
printf "%bERROR%b: Terraform files are not formatted correctly\n" "${RED}" "${NEUTRAL}"
printf "Please run 'terraform fmt -recursive' to fix the formatting issues in the files listed above.\n"
printf "PS: You can most likely find a terraform plugin or mode for your editor that can format on save, or as you type. If none is provided, you can use the 'terraform-ls' language server."
exit 1
else
printf "Formating: %bOK%b\n\n" "${GREEN}" "${NEUTRAL}"
fi
printf "%bRunning TFLint: %b\n" "${YELLOW}" "${NEUTRAL}"
printf "#--------------------------------------------------------------------------------\n"
tflint --init
if ! tflint --chdir=${GHA_TERRAFORM_LINT_DIRECTORY} --recursive; then
printf "#--------------------------------------------------------------------------------\n"
printf "%bERROR%b: TFLint found issues in the Terraform configuration\n" "${RED}" "${NEUTRAL}"
printf "Please check the error messages above and fix the issue\n\n"
exit 1
else
printf "TFLint: %bOK%b\n\n" "${GREEN}" "${NEUTRAL}"
fi
printf "%bInitializing Terraform Configuration to check for provider plugins issues: %b\n" "${YELLOW}" "${NEUTRAL}"
printf "#--------------------------------------------------------------------------------\n"
if ! terraform -chdir=${GHA_TERRAFORM_LINT_DIRECTORY} init -backend=false; then
printf "#----------------------------------------\n"
printf "\n%bERROR%b: Terraform configuration could not be initialized\n" "${RED}" "${NEUTRAL}"
printf "Please check the error messages above and fix the issue\n\n"
exit 1
else
printf "Initialization: %bOK%b\n\n" "${GREEN}" "${NEUTRAL}"
fi
printf "%bValidating Terraform Configuration: %b\n" "${YELLOW}" "${NEUTRAL}"
printf "#--------------------------------------------------------------------------------\n"
if ! terraform -chdir=${GHA_TERRAFORM_LINT_DIRECTORY} validate; then
printf "#--------------------------------------------------------------------------------\n"
printf "\n%bERROR%b: Terraform configuration is not valid\n" "${RED}" "${NEUTRAL}"
printf "Please check the error messages above and fix the issue\n\n"
exit 1
else
printf "Validation: %bOK%b\n\n" "${GREEN}" "${NEUTRAL}"
fi