-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
183 lines (134 loc) · 3.38 KB
/
Makefile
File metadata and controls
183 lines (134 loc) · 3.38 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env make -f
# Get the set versions of Terraform and OpenTofu.
TERRAFORM_VERSION:=$(shell cat .terraform-version)
TOFU_VERSION:=$(shell cat .terraform-version)
# Select the engine: opentofu or terraform.
# make validate TF_ENGINE=opentofu
# This will switch the engine for all actions, including testing.
TF_ENGINE:=terraform
ifeq ($(TF_ENGINE), terraform)
TF_BINARY:=terraform
TF_VERSION:=$(TERRAFORM_VERSION)
else ifeq ($(TF_ENGINE), opentofu)
TF_BINARY:=tofu
TF_VERSION:=$(TOFU_VERSION)
endif
ifeq ($(CI), )
TFENV_COMMAND:=tenv use $(TF_BINARY) $(TF_VERSION)
else
TFENV_COMMAND:=echo "skipping tenv use in CI"
endif
# Packages to install based on different package managers.
BREW_PACKAGES := cosign tenv terraform-docs tflint checkov trivy pre-commit golang
CHOCOLATEY_PACKAGES := cosign tenv terraform-docs tflint trivy golang
APT_PACKAGES :=
APK_PACKAGES := cosign
# Autogenerated based off on the system itself.
# Github Actions installs homebrew on linux machines, so we check for apt first.
INSTALLER_PATH := $(shell { command -v apt || command -v brew || command -v choco ; } 2>/dev/null)
INSTALLER := $(shell { basename $(INSTALLER_PATH) ; } 2>/dev/null)
MODULE_DIRECTORY_NAME := $(shell { pwd | rev | cut -d"/" -f 1 | rev ; } 2>/dev/null)
TERRATEST_FILES:=$(wildcard tests/*_test.go)
# Empty variables primarily used to allow users to pass in their own options.
CHECKOV_OPTS:=
GO_TEST_OPTS:=
# General
all:
chores: documentation formatting
test: test_documentation test_lint test_security test_validation test_formatting
#
# Install
#
install: install_$(INSTALLER)
install_brew:
brew tap tofuutils/tap
brew install $(BREW_PACKAGES)
install_choco:
choco install $(CHOCOLATEY_PACKAGES)
# checkov
#
# Testing Workspace Setup
#
.terraform:
$(TF_BINARY) init -backend=false #ANNO This command creates the .terraform directory.
#
# Terraform Formatting
#
.PHONY: formatting
formatting:
$(TF_BINARY) fmt -recursive .
.PHONY: test_formatting
test_formatting:
$(TF_BINARY) fmt -check -recursive .
#
# Terraform Docs
#
.PHONY: documentation
documentation:
terraform-docs -c .terraform-docs.yml .
.PHONY: test_documentation
test_documentation:
terraform-docs -c .terraform-docs.yml --output-check .
#
# Linting
#
.PHONY: fix_tflint
fix_tflint:
tflint --init
tflint --fix
.PHONY: test_tflint
test_tflint:
tflint --init
tflint
#
# Security
#
.PHONY: test_security
test_security: test_checkov test_trivy
.PHONY: test_checkov
test_checkov:
checkov --directory . $(CHECKOV_OPTS)
.PHONY: test_trivy
test_trivy:
trivy config .
#
# Terratest
#
tests/go.mod:
cd tests && \
go mod init "testing_terraform"
tests/go.sum: tests/go.mod $(TERRATEST_FILES)
cd tests && \
go mod tidy
.PHONY: terratest
terratest: tests/go.sum
cd tests && \
TERRATEST_BINARY=$(TF_BINARY) go test -v -timeout 60m $(GO_TEST_OPTS)
#
# Terraform Test Framework
#
TERRAFORM_EXAMPLES:=$(wildcard examples/*)
.PHONY: $(TERRAFORM_EXAMPLES)
$(TERRAFORM_EXAMPLES):
@echo "Testing $@"
cd $@ && \
$(TFENV_COMMAND) && \
$(TF_BINARY) init -backend=false && \
$(TF_BINARY) test $(TF_TEST_OPTS)
.PHONY: terraform_test
terraform_test: $(TERRAFORM_EXAMPLES)
@echo "Testing Root Module"
$(TFENV_COMMAND) && \
$(TF_BINARY) test $(TF_TEST_OPTS)
#
# Validation
#
.PHONY: test_validation
test_validation: .terraform
$(TF_BINARY) validate
#
# Local Tools
#
.PHONY: precommit_install
precommit_install:
pre-commit install