11# Set default shell to bash
22SHELL := /bin/bash -o pipefail
33
4- BUILD_TOOLS_VERSION ?= v0.12.0
4+ BUILD_TOOLS_VERSION ?= v0.15.2
55BUILD_TOOLS_DOCKER_REPO ?= mineiros/build-tools
66BUILD_TOOLS_DOCKER_IMAGE ?= ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION}
77
8- # If running in CI (e.g. GitHub Actions)
9- # https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables
8+ # Some CI providers such as GitHub Actions, CircleCI, and TravisCI are setting
9+ # the CI environment variable to a non-empty value by default to indicate that
10+ # the current workflow is running in a Continuous Integration environment.
1011#
11- # To disable TF_IN_AUTOMATION in CI set it to empty
12+ # If TF_IN_AUTOMATION is set to any non-empty value, Terraform adjusts its
13+ # output to avoid suggesting specific commands to run next.
1214# https://www.terraform.io/docs/commands/environment-variables.html#tf_in_automation
1315#
1416# We are using GNU style quiet commands to disable set V to non-empty e.g. V=1
1517# https://www.gnu.org/software/automake/manual/html_node/Debugging-Make-Rules.html
18+ #
1619ifdef CI
17- TF_IN_AUTOMATION ?= 1
18- export TF_IN_AUTOMATION
20+ TF_IN_AUTOMATION ?= yes
21+ export TF_IN_AUTOMATION
1922
20- V ?= 1
23+ V ?= 1
2124endif
2225
2326ifndef NOCOLOR
24- GREEN := $(shell tput -Txterm setaf 2)
25- YELLOW := $(shell tput -Txterm setaf 3)
26- WHITE := $(shell tput -Txterm setaf 7)
27- RESET := $(shell tput -Txterm sgr0)
27+ GREEN := $(shell tput -Txterm setaf 2)
28+ YELLOW := $(shell tput -Txterm setaf 3)
29+ WHITE := $(shell tput -Txterm setaf 7)
30+ RESET := $(shell tput -Txterm sgr0)
2831endif
2932
30- # We are creating docker volumes for /go and /terraform that are unique per
31- # repository to reuse dependencies between different docker run commands.
32- VOLUME_PREFIX ?= mineiros_build_tools
33- VOLUME_SUFFIX ?= $(notdir $(shell git rev-parse --show-toplevel || "build") )
34- DOCKER_RUN_FLAGS += -v ${VOLUME_PREFIX}-terraform-${VOLUME_SUFFIX}:/terraform
35- DOCKER_RUN_FLAGS += -v ${VOLUME_PREFIX}-go-${VOLUME_SUFFIX}:/go
36- DOCKER_RUN_FLAGS += -v ${PWD}:/build
33+ GIT_TOPLEVEl = $(shell git rev-parse --show-toplevel)
34+
35+ # Generic docker run flags
36+ DOCKER_RUN_FLAGS += -v ${GIT_TOPLEVEl}:/build
3737DOCKER_RUN_FLAGS += --rm
3838DOCKER_RUN_FLAGS += -e TF_IN_AUTOMATION
39+ # If TF_VERSION is defined, TFSwitch will switch to the desired version on
40+ # container startup. If TF_VERSION is omitted, the default version installed
41+ # inside the docker image will be used.
42+ DOCKER_RUN_FLAGS += -e TF_VERSION
3943
40- DOCKER_GITHUB_FLAGS += -e GITHUB_TOKEN
41- DOCKER_GITHUB_FLAGS += -e GITHUB_OWNER
44+ # If SSH_AUTH_SOCK is set, we forward the SSH agent of the host system into
45+ # the docker container. This is useful when working with private repositories
46+ # and dependencies that might need to be cloned inside the container (e.g.
47+ # private Terraform modules).
48+ ifdef SSH_AUTH_SOCK
49+ DOCKER_SSH_FLAGS += -e SSH_AUTH_SOCK=/ssh-agent
50+ DOCKER_SSH_FLAGS += -v ${SSH_AUTH_SOCK}:/ssh-agent
51+ endif
4252
43- DOCKER_FLAGS += ${DOCKER_RUN_FLAGS}
44- DOCKER_RUN_CMD = docker run ${DOCKER_FLAGS} ${BUILD_TOOLS_DOCKER_IMAGE}
53+ # If AWS_ACCESS_KEY_ID is defined, we are likely running inside an AWS provider
54+ # module. To enable AWS authentication inside the docker container, we inject
55+ # the relevant environment variables.
56+ ifdef AWS_ACCESS_KEY_ID
57+ DOCKER_AWS_FLAGS += -e AWS_ACCESS_KEY_ID
58+ DOCKER_AWS_FLAGS += -e AWS_SECRET_ACCESS_KEY
59+ DOCKER_AWS_FLAGS += -e AWS_SESSION_TOKEN
60+ endif
61+
62+ # If GOOGLE_CREDENTIALS is defined, we are likely running inside a GCP provider
63+ # module. To enable GCP authentication inside the docker container, we inject
64+ # the relevant environment variables (service-account key file).
65+ ifdef GOOGLE_CREDENTIALS
66+ DOCKER_GCP_FLAGS += -e GOOGLE_CREDENTIALS
67+ DOCKER_GCP_FLAGS += -e TEST_GCP_PROJECT
68+ DOCKER_GCP_FLAGS += -e TEST_GCP_ORG_DOMAIN
69+ endif
70+
71+ # If GITHUB_OWNER is defined, we are likely running inside a GitHub provider
72+ # module. To enable GitHub authentication inside the docker container,
73+ # we inject the relevant environment variables.
74+ ifdef GITHUB_OWNER
75+ DOCKER_GITHUB_FLAGS += -e GITHUB_TOKEN
76+ DOCKER_GITHUB_FLAGS += -e GITHUB_OWNER
77+ endif
4578
4679.PHONY : default
4780default : help
4881
49- # # Run pre-commit hooks in build-tools docker container.
82+ # Not exposed as a callable target by `make help`, since this is a one-time shot to simplify the development of this module.
83+ .PHONY : template/adjust
84+ template/adjust : FILTER = -path ./.git -prune -a -type f -o -type f -not -name Makefile
85+ template/adjust :
86+ @find . $(FILTER ) -exec sed -i -e " s,terraform-module-template,$$ {PWD##*/},g" {} \;
87+
88+ # # Run pre-commit hooks inside a build-tools docker container.
89+ .PHONY : test/docker/pre-commit
90+ test/docker/pre-commit : DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
91+ test/docker/pre-commit : DOCKER_FLAGS += -e NOCOLOR=1
92+ test/docker/pre-commit :
93+ $(call docker-run,make test/pre-commit)
94+
95+ # # Run all Go tests inside a build-tools docker container. This is complementary to running 'go test ./test/...'.
96+ .PHONY : test/docker/unit-tests
97+ test/docker/unit-tests : DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
98+ test/docker/unit-tests : DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS}
99+ test/docker/unit-tests : DOCKER_FLAGS += ${DOCKER_AWS_FLAGS}
100+ test/docker/unit-tests : DOCKER_FLAGS += ${DOCKER_GCP_FLAGS}
101+ test/docker/unit-tests : DOCKER_FLAGS += $(shell env | grep ^TF_VAR_ | cut -d = -f 1 | xargs -i printf ' -e {}')
102+ test/docker/unit-tests : DOCKER_FLAGS += -e TF_DATA_DIR=.terratest
103+ test/docker/unit-tests : DOCKER_FLAGS += -e NOCOLOR=1
104+ test/docker/unit-tests : TEST ?= "TestUnit"
105+ test/docker/unit-tests :
106+ @echo " ${YELLOW} [TEST] ${GREEN} Start Running Go Tests in Docker Container.${RESET} "
107+ $(call docker-run,make test/unit-tests)
108+
109+ # # Run pre-commit hooks.
50110.PHONY : test/pre-commit
51- test/pre-commit : DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS }
111+ test/pre-commit : DOCKER_FLAGS += ${DOCKER_SSH_FLAGS }
52112test/pre-commit :
53- $(call docker-run ,pre-commit run -a)
113+ $(call quiet-command ,pre-commit run -a)
54114
55- # # Run all Go tests inside a build-tools docker container. This is complementary to running 'go test ./test/...' .
56- .PHONY : test/unit-tests
57- test/unit-tests : DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS}
115+ # # Run all unit tests.
116+ .PHONY : test/docker/ unit-tests
117+ test/unit-tests : TEST ?= "TestUnit"
58118test/unit-tests :
59- @echo " ${YELLOW} [TEST] ${GREEN} Start Running Go Tests in Docker Container.${RESET} "
60- $(call go-test,./test/...)
119+ @echo " ${YELLOW} [TEST] ${GREEN} Start Running unit tests.${RESET} "
120+ $(call quiet-command,cd test ; go test -v -count 1 -timeout 45m -parallel 128 -run $(TEST ) )
121+
122+ # # Generate README.md with Terradoc
123+ .PHONY : terradoc
124+ terradoc :
125+ $(call quiet-command,terradoc generate -o README.md README.tfdoc.hcl)
126+
127+ # # Generate shared configuration for tests
128+ .PHONY : terramate
129+ terramate :
130+ $(call quiet-command,terramate generate)
61131
62132# # Clean up cache and temporary files
63133.PHONY : clean
64134clean :
65135 $(call rm-command,.terraform)
136+ $(call rm-command,.terratest)
137+ $(call rm-command,.terraform.lock.hcl)
66138 $(call rm-command,* .tfplan)
67- $(call rm-command,examples/* /.terraform)
68- $(call rm-command,examples/* /* .tfplan)
139+ $(call rm-command,* /* /.terraform)
140+ $(call rm-command,* /* /.terratest)
141+ $(call rm-command,* /* /* .tfplan)
142+ $(call rm-command,* /* /.terraform.lock.hcl)
69143
70144# # Display help for all targets
71145.PHONY : help
@@ -80,13 +154,10 @@ help:
80154 } \
81155 { lastLine = $$ 0 }' $(MAKEFILE_LIST)
82156
83- # # Generate README.md with Terradoc
84- .PHONY : terradoc
85- terradoc :
86- $(call quiet-command,terradoc -o README.md README.tfdoc.hcl)
157+ # Define helper functions
158+ DOCKER_FLAGS += ${DOCKER_RUN_FLAGS}
159+ DOCKER_RUN_CMD = docker run ${DOCKER_FLAGS} ${BUILD_TOOLS_DOCKER_IMAGE}
87160
88- # define helper functions
89161quiet-command = $(if ${V},${1},$(if ${2},@echo ${2} && ${1}, @${1}) )
90162docker-run = $(call quiet-command,${DOCKER_RUN_CMD} ${1} | cat,"${YELLOW}[DOCKER RUN] ${GREEN}${1}${RESET}")
91- go-test = $(call quiet-command,${DOCKER_RUN_CMD} go test -v -count 1 -timeout 45m -parallel 128 ${1} | cat,"${YELLOW}[TEST] ${GREEN}${1}${RESET}")
92163rm-command = $(call quiet-command,rm -rf ${1},"${YELLOW}[CLEAN] ${GREEN}${1}${RESET}")
0 commit comments