|
1 |
| -SHELL := /bin/bash |
| 1 | +# Set default shell to bash |
| 2 | +SHELL := /bin/bash -o pipefail |
2 | 3 |
|
3 |
| -MOUNT_TARGET_DIRECTORY = /app/src |
4 |
| -BUILD_TOOLS_DOCKER_REPO = mineiros/build-tools |
| 4 | +BUILD_TOOLS_VERSION ?= v0.4.0 |
| 5 | +BUILD_TOOLS_DOCKER_REPO ?= mineiros/build-tools |
| 6 | +BUILD_TOOLS_DOCKER_IMAGE ?= ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION} |
5 | 7 |
|
6 |
| -# Set default value for environment variable if there aren't set already |
7 |
| -ifndef BUILD_TOOLS_VERSION |
8 |
| - BUILD_TOOLS_VERSION := latest |
| 8 | +# if running in CI (e.g. Semaphore CI) |
| 9 | +# https://docs.semaphoreci.com/ci-cd-environment/environment-variables/#ci |
| 10 | +# |
| 11 | +# to disable TF_IN_AUTOMATION in CI set it to empty |
| 12 | +# https://www.terraform.io/docs/commands/environment-variables.html#tf_in_automation |
| 13 | +# |
| 14 | +# we are using GNU style quiet commands to disable set V to non-empty e.g. V=1 |
| 15 | +# https://www.gnu.org/software/automake/manual/html_node/Debugging-Make-Rules.html |
| 16 | +# |
| 17 | +ifdef CI |
| 18 | + TF_IN_AUTOMATION ?= yes |
| 19 | + export TF_IN_AUTOMATION |
| 20 | + |
| 21 | + V ?= 1 |
9 | 22 | endif
|
10 | 23 |
|
11 |
| -ifndef BUILD_TOOLS_DOCKER_IMAGE |
12 |
| - BUILD_TOOLS_DOCKER_IMAGE := ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION} |
| 24 | +ifndef NOCOLOR |
| 25 | + GREEN := $(shell tput -Txterm setaf 2) |
| 26 | + YELLOW := $(shell tput -Txterm setaf 3) |
| 27 | + WHITE := $(shell tput -Txterm setaf 7) |
| 28 | + RESET := $(shell tput -Txterm sgr0) |
13 | 29 | endif
|
14 | 30 |
|
15 |
| -USER_UID := $(shell id -u) |
16 |
| -USER_GID := $(shell id -g) |
| 31 | +DOCKER_RUN_FLAGS += --rm |
| 32 | +DOCKER_RUN_FLAGS += -v ${PWD}:/app/src |
| 33 | +DOCKER_RUN_FLAGS += -e TF_IN_AUTOMATION |
| 34 | +DOCKER_RUN_FLAGS += -e USER_UID=$(shell id -u) |
| 35 | + |
| 36 | +DOCKER_SSH_FLAGS += -e SSH_AUTH_SOCK=/ssh-agent |
| 37 | +DOCKER_SSH_FLAGS += -v ${SSH_AUTH_SOCK}:/ssh-agent |
| 38 | + |
| 39 | +DOCKER_AWS_FLAGS += -e AWS_ACCESS_KEY_ID |
| 40 | +DOCKER_AWS_FLAGS += -e AWS_SECRET_ACCESS_KEY |
| 41 | +DOCKER_AWS_FLAGS += -e AWS_SESSION_TOKEN |
17 | 42 |
|
18 |
| -GREEN := $(shell tput -Txterm setaf 2) |
19 |
| -YELLOW := $(shell tput -Txterm setaf 3) |
20 |
| -WHITE := $(shell tput -Txterm setaf 7) |
21 |
| -RESET := $(shell tput -Txterm sgr0) |
| 43 | +DOCKER_FLAGS += ${DOCKER_RUN_FLAGS} |
| 44 | +DOCKER_RUN_CMD = docker run ${DOCKER_FLAGS} ${BUILD_TOOLS_DOCKER_IMAGE} |
22 | 45 |
|
23 |
| -.DEFAULT_GOAL := help |
| 46 | +.PHONY: default |
| 47 | +default: help |
24 | 48 |
|
25 |
| -.PHONY: test/pre-commit-hooks |
26 |
| -## Mounts the working directory inside a docker container and runs the pre-commit hooks |
27 |
| -test/pre-commit-hooks: |
28 |
| - @echo "${GREEN}Start running the pre-commit hooks with docker${RESET}" |
29 |
| - @docker run --rm \ |
30 |
| - -u ${USER_UID}:${USER_GID} \ |
31 |
| - -v ${PWD}:${MOUNT_TARGET_DIRECTORY} \ |
32 |
| - ${BUILD_TOOLS_DOCKER_IMAGE} \ |
33 |
| - sh -c "pre-commit run -a" |
| 49 | +## Run pre-commit hooks in build-tools docker container. |
| 50 | +.PHONY: test/pre-commit |
| 51 | +test/pre-commit: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS} |
| 52 | +test/pre-commit: |
| 53 | + $(call docker-run,pre-commit run -a) |
34 | 54 |
|
| 55 | +## Run go tests hooks in build-tools docker container. |
35 | 56 | .PHONY: test/unit-tests
|
36 |
| -## Mounts the working directory inside a new container and runs the Go tests. Requires $AWS_ACCESS_KEY_ID and $AWS_SECRET_ACCESS_KEY to be set |
37 |
| -# test/unit-tests: |
38 |
| -# @echo "${GREEN}Start running the unit tests with docker${RESET}" |
39 |
| -# @docker run --rm \ |
40 |
| -# -e AWS_ACCESS_KEY_ID \ |
41 |
| -# -e AWS_SECRET_ACCESS_KEY \ |
42 |
| -# -u ${USER_UID}:${USER_GID} \ |
43 |
| -# -v ${PWD}:${MOUNT_TARGET_DIRECTORY} \ |
44 |
| -# ${BUILD_TOOLS_DOCKER_IMAGE} \ |
45 |
| -# go test -v -timeout 45m -parallel 128 test/example_test.go |
| 57 | +test/unit-tests: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS} |
| 58 | +test/unit-tests: DOCKER_FLAGS += ${DOCKER_AWS_FLAGS} |
| 59 | +test/unit-tests: |
| 60 | + @echo "${YELLOW}No tests defined.${RESET}" |
| 61 | +# $(call go-test,tests) |
| 62 | + |
| 63 | +## Clean up cache and temporary files |
| 64 | +.PHONY: clean |
| 65 | +clean: |
| 66 | + $(call rm-command,.terraform) |
46 | 67 |
|
47 |
| -.PHONY: help |
48 | 68 | ## Display help for all targets
|
| 69 | +.PHONY: help |
49 | 70 | help:
|
50 |
| - @awk '/^[a-zA-Z_0-9%:\\\/-]+:/ { \ |
51 |
| - msg = match(lastLine, /^## (.*)/); \ |
| 71 | + @awk '/^.PHONY: / { \ |
| 72 | + msg = match(lastLine, /^## /); \ |
52 | 73 | if (msg) { \
|
53 |
| - cmd = $$1; \ |
54 |
| - msg = substr(lastLine, RSTART + 3, RLENGTH); \ |
55 |
| - gsub("\\\\", "", cmd); \ |
56 |
| - gsub(":+$$", "", cmd); \ |
57 |
| - printf " \x1b[32;01m%-35s\x1b[0m %s\n", cmd, msg; \ |
| 74 | + cmd = substr($$0, 9, 100); \ |
| 75 | + msg = substr(lastLine, 4, 1000); \ |
| 76 | + printf " ${GREEN}%-30s${RESET} %s\n", cmd, msg; \ |
58 | 77 | } \
|
59 | 78 | } \
|
60 |
| - { lastLine = $$0 }' $(MAKEFILE_LIST) | sort -u |
| 79 | + { lastLine = $$0 }' $(MAKEFILE_LIST) |
| 80 | + |
| 81 | +# define helper functions |
| 82 | +quiet-command = $(if ${V},${1},$(if ${2},@echo ${2} && ${1}, @${1})) |
| 83 | + |
| 84 | +docker-run = $(call quiet-command,${DOCKER_RUN_CMD} ${1} | cat,"${YELLOW}[DOCKER RUN] ${GREEN}${1}${RESET}") |
| 85 | +go-test = $(call quiet-command,${DOCKER_RUN_CMD} go test -v -timeout 45m -parallel 128 ${1} | cat,"${YELLOW}[TEST] ${GREEN}${1}${RESET}") |
| 86 | + |
| 87 | +rm-command = $(call quiet-command,rm -rf ${1},"${YELLOW}[CLEAN] ${GREEN}${1}${RESET}") |
0 commit comments