-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
163 lines (141 loc) · 6.01 KB
/
Makefile
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
# Set default shell to bash
SHELL := /bin/bash -o pipefail
BUILD_TOOLS_VERSION ?= v0.15.2
BUILD_TOOLS_DOCKER_REPO ?= mineiros/build-tools
BUILD_TOOLS_DOCKER_IMAGE ?= ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION}
# Some CI providers such as GitHub Actions, CircleCI, and TravisCI are setting
# the CI environment variable to a non-empty value by default to indicate that
# the current workflow is running in a Continuous Integration environment.
#
# If TF_IN_AUTOMATION is set to any non-empty value, Terraform adjusts its
# output to avoid suggesting specific commands to run next.
# https://www.terraform.io/docs/commands/environment-variables.html#tf_in_automation
#
# We are using GNU style quiet commands to disable set V to non-empty e.g. V=1
# https://www.gnu.org/software/automake/manual/html_node/Debugging-Make-Rules.html
#
ifdef CI
TF_IN_AUTOMATION ?= yes
export TF_IN_AUTOMATION
V ?= 1
endif
ifndef NOCOLOR
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
endif
GIT_TOPLEVEl = $(shell git rev-parse --show-toplevel)
# Generic docker run flags
DOCKER_RUN_FLAGS += -v ${GIT_TOPLEVEl}:/build
DOCKER_RUN_FLAGS += --rm
DOCKER_RUN_FLAGS += -e TF_IN_AUTOMATION
# If TF_VERSION is defined, TFSwitch will switch to the desired version on
# container startup. If TF_VERSION is omitted, the default version installed
# inside the docker image will be used.
DOCKER_RUN_FLAGS += -e TF_VERSION
# If SSH_AUTH_SOCK is set, we forward the SSH agent of the host system into
# the docker container. This is useful when working with private repositories
# and dependencies that might need to be cloned inside the container (e.g.
# private Terraform modules).
ifdef SSH_AUTH_SOCK
DOCKER_SSH_FLAGS += -e SSH_AUTH_SOCK=/ssh-agent
DOCKER_SSH_FLAGS += -v ${SSH_AUTH_SOCK}:/ssh-agent
endif
# If AWS_ACCESS_KEY_ID is defined, we are likely running inside an AWS provider
# module. To enable AWS authentication inside the docker container, we inject
# the relevant environment variables.
ifdef AWS_ACCESS_KEY_ID
DOCKER_AWS_FLAGS += -e AWS_ACCESS_KEY_ID
DOCKER_AWS_FLAGS += -e AWS_SECRET_ACCESS_KEY
DOCKER_AWS_FLAGS += -e AWS_SESSION_TOKEN
endif
# If GOOGLE_CREDENTIALS is defined, we are likely running inside a GCP provider
# module. To enable GCP authentication inside the docker container, we inject
# the relevant environment variables (service-account key file).
ifdef GOOGLE_CREDENTIALS
DOCKER_GCP_FLAGS += -e GOOGLE_CREDENTIALS
DOCKER_GCP_FLAGS += -e TEST_GCP_PROJECT
DOCKER_GCP_FLAGS += -e TEST_GCP_ORG_DOMAIN
endif
# If GITHUB_OWNER is defined, we are likely running inside a GitHub provider
# module. To enable GitHub authentication inside the docker container,
# we inject the relevant environment variables.
ifdef GITHUB_OWNER
DOCKER_GITHUB_FLAGS += -e GITHUB_TOKEN
DOCKER_GITHUB_FLAGS += -e GITHUB_OWNER
endif
.PHONY: default
default: help
# Not exposed as a callable target by `make help`, since this is a one-time shot to simplify the development of this module.
.PHONY: template/adjust
template/adjust: FILTER = -path ./.git -prune -a -type f -o -type f -not -name Makefile
template/adjust:
@find . $(FILTER) -exec sed -i -e "s,terraform-module-template,$${PWD##*/},g" {} \;
## Run pre-commit hooks inside a build-tools docker container.
.PHONY: test/docker/pre-commit
test/docker/pre-commit: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
test/docker/pre-commit: DOCKER_FLAGS += -e NOCOLOR=1
test/docker/pre-commit:
$(call docker-run,make test/pre-commit)
## Run all Go tests inside a build-tools docker container. This is complementary to running 'go test ./test/...'.
.PHONY: test/docker/unit-tests
test/docker/unit-tests: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
test/docker/unit-tests: DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS}
test/docker/unit-tests: DOCKER_FLAGS += ${DOCKER_AWS_FLAGS}
test/docker/unit-tests: DOCKER_FLAGS += ${DOCKER_GCP_FLAGS}
test/docker/unit-tests: DOCKER_FLAGS += $(shell env | grep ^TF_VAR_ | cut -d = -f 1 | xargs -i printf ' -e {}')
test/docker/unit-tests: DOCKER_FLAGS += -e TF_DATA_DIR=.terratest
test/docker/unit-tests: DOCKER_FLAGS += -e NOCOLOR=1
test/docker/unit-tests: TEST ?= "TestUnit"
test/docker/unit-tests:
@echo "${YELLOW}[TEST] ${GREEN}Start Running Go Tests in Docker Container.${RESET}"
$(call docker-run,make test/unit-tests)
## Run pre-commit hooks.
.PHONY: test/pre-commit
test/pre-commit: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
test/pre-commit:
$(call quiet-command,pre-commit run -a)
## Run all unit tests.
.PHONY: test/docker/unit-tests
test/unit-tests: TEST ?= "TestUnit"
test/unit-tests:
@echo "${YELLOW}[TEST] ${GREEN}Start Running unit tests.${RESET}"
$(call quiet-command,cd test ; go test -v -count 1 -timeout 45m -parallel 128 -run $(TEST))
## Generate README.md with Terradoc
.PHONY: terradoc
terradoc:
$(call quiet-command,terradoc generate -o README.md README.tfdoc.hcl)
## Generate shared configuration for tests
.PHONY: terramate
terramate:
$(call quiet-command,terramate generate)
## Clean up cache and temporary files
.PHONY: clean
clean:
$(call rm-command,.terraform)
$(call rm-command,.terratest)
$(call rm-command,.terraform.lock.hcl)
$(call rm-command,*.tfplan)
$(call rm-command,*/*/.terraform)
$(call rm-command,*/*/.terratest)
$(call rm-command,*/*/*.tfplan)
$(call rm-command,*/*/.terraform.lock.hcl)
## Display help for all targets
.PHONY: help
help:
@awk '/^.PHONY: / { \
msg = match(lastLine, /^## /); \
if (msg) { \
cmd = substr($$0, 9, 100); \
msg = substr(lastLine, 4, 1000); \
printf " ${GREEN}%-30s${RESET} %s\n", cmd, msg; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
# Define helper functions
DOCKER_FLAGS += ${DOCKER_RUN_FLAGS}
DOCKER_RUN_CMD = docker run ${DOCKER_FLAGS} ${BUILD_TOOLS_DOCKER_IMAGE}
quiet-command = $(if ${V},${1},$(if ${2},@echo ${2} && ${1}, @${1}))
docker-run = $(call quiet-command,${DOCKER_RUN_CMD} ${1} | cat,"${YELLOW}[DOCKER RUN] ${GREEN}${1}${RESET}")
rm-command = $(call quiet-command,rm -rf ${1},"${YELLOW}[CLEAN] ${GREEN}${1}${RESET}")