11# Set default shell to bash
22SHELL := /bin/bash -o pipefail
33
4- BUILD_TOOLS_VERSION ?= v0.11 .0
4+ BUILD_TOOLS_VERSION ?= v0.12 .0
55BUILD_TOOLS_DOCKER_REPO ?= mineiros/build-tools
66BUILD_TOOLS_DOCKER_IMAGE ?= ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION}
77
8- #
98# Some CI providers such as GitHub Actions, CircleCI, and TravisCI are setting
109# the CI environment variable to a non-empty value by default to indicate that
1110# the current workflow is running in a Continuous Integration environment.
@@ -18,36 +17,51 @@ BUILD_TOOLS_DOCKER_IMAGE ?= ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION}
1817# https://www.gnu.org/software/automake/manual/html_node/Debugging-Make-Rules.html
1918#
2019ifdef CI
21- TF_IN_AUTOMATION ?= yes
22- export TF_IN_AUTOMATION
20+ TF_IN_AUTOMATION ?= yes
21+ export TF_IN_AUTOMATION
2322
24- V ?= 1
23+ V ?= 1
2524endif
2625
2726ifndef NOCOLOR
28- GREEN := $(shell tput -Txterm setaf 2)
29- YELLOW := $(shell tput -Txterm setaf 3)
30- WHITE := $(shell tput -Txterm setaf 7)
31- 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)
3231endif
3332
3433GIT_TOPLEVEl = $(shell git rev-parse --show-toplevel)
3534
35+ # Generic docker run flags
3636DOCKER_RUN_FLAGS += -v ${GIT_TOPLEVEl}:/build
3737DOCKER_RUN_FLAGS += --rm
3838DOCKER_RUN_FLAGS += -e TF_IN_AUTOMATION
3939
40+ # If SSH_AUTH_SOCK is set, we forward the SSH agent of the host system into
41+ # the docker container. This is useful when working with private repositories
42+ # and dependencies that might need to be cloned inside the container (e.g.
43+ # private Terraform modules).
4044ifdef SSH_AUTH_SOCK
4145 DOCKER_SSH_FLAGS += -e SSH_AUTH_SOCK=/ssh-agent
4246 DOCKER_SSH_FLAGS += -v ${SSH_AUTH_SOCK}:/ssh-agent
4347endif
4448
45- DOCKER_AWS_FLAGS += -e AWS_ACCESS_KEY_ID
46- DOCKER_AWS_FLAGS += -e AWS_SECRET_ACCESS_KEY
47- DOCKER_AWS_FLAGS += -e AWS_SESSION_TOKEN
49+ # If AWS_ACCESS_KEY_ID is defined, we are likely running inside an AWS provider
50+ # module. To enable AWS authentication inside the docker container, we inject
51+ # the relevant environment variables.
52+ ifdef AWS_ACCESS_KEY_ID
53+ DOCKER_AWS_FLAGS += -e AWS_ACCESS_KEY_ID
54+ DOCKER_AWS_FLAGS += -e AWS_SECRET_ACCESS_KEY
55+ DOCKER_AWS_FLAGS += -e AWS_SESSION_TOKEN
56+ endif
4857
49- DOCKER_FLAGS += ${DOCKER_RUN_FLAGS}
50- DOCKER_RUN_CMD = docker run ${DOCKER_FLAGS} ${BUILD_TOOLS_DOCKER_IMAGE}
58+ # If GITHUB_OWNER is defined, we are likely running inside a GitHub provider
59+ # module. To enable GitHub authentication inside the docker container,
60+ # we inject the relevant environment variables.
61+ ifdef GITHUB_OWNER
62+ DOCKER_GITHUB_FLAGS += -e GITHUB_TOKEN
63+ DOCKER_GITHUB_FLAGS += -e GITHUB_OWNER
64+ endif
5165
5266.PHONY : default
5367default : help
@@ -67,7 +81,9 @@ test/pre-commit:
6781# # Run all Go tests inside a build-tools docker container. This is complementary to running 'go test ./test/...'.
6882.PHONY : test/unit-tests
6983test/unit-tests : DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
84+ test/unit-tests : DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS}
7085test/unit-tests : DOCKER_FLAGS += ${DOCKER_AWS_FLAGS}
86+ test/unit-tests : DOCKER_FLAGS += -e TF_DATA_DIR=.terratest
7187test/unit-tests : TEST ?= "TestUnit"
7288test/unit-tests :
7389 @echo " ${YELLOW} [TEST] ${GREEN} Start Running Go Tests in Docker Container.${RESET} "
@@ -96,7 +112,10 @@ help:
96112 } \
97113 { lastLine = $$ 0 }' $(MAKEFILE_LIST)
98114
99- # define helper functions
115+ # Define helper functions
116+ DOCKER_FLAGS += ${DOCKER_RUN_FLAGS}
117+ DOCKER_RUN_CMD = docker run ${DOCKER_FLAGS} ${BUILD_TOOLS_DOCKER_IMAGE}
118+
100119quiet-command = $(if ${V},${1},$(if ${2},@echo ${2} && ${1}, @${1}) )
101120docker-run = $(call quiet-command,${DOCKER_RUN_CMD} ${1} | cat,"${YELLOW}[DOCKER RUN] ${GREEN}${1}${RESET}")
102121go-test = $(call quiet-command,${DOCKER_RUN_CMD} go test -v -count 1 -timeout 45m -parallel 128 ${1} | cat,"${YELLOW}[TEST] ${GREEN}${1}${RESET}")
0 commit comments