Created
February 21, 2021 22:21
-
-
Save soerenmartius/f3cdcb89631f908b9a70197b78b089ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set default shell to bash | |
SHELL := /bin/bash -o pipefail | |
BUILD_TOOLS_VERSION ?= v0.5.3 | |
BUILD_TOOLS_DOCKER_REPO = mineiros/build-tools | |
BUILD_TOOLS_DOCKER_IMAGE ?= ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION} | |
TERRAFORM_PLANFILE ?= out.tfplan | |
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 | |
DOCKER_RUN_FLAGS += --rm | |
DOCKER_RUN_FLAGS += -v ${PWD}:/app/src | |
DOCKER_RUN_FLAGS += -e TF_IN_AUTOMATION | |
DOCKER_RUN_FLAGS += -e USER_UID=$(shell id -u) | |
DOCKER_AWS_FLAGS += -e AWS_ACCESS_KEY_ID | |
DOCKER_AWS_FLAGS += -e AWS_SECRET_ACCESS_KEY | |
DOCKER_AWS_FLAGS += -e AWS_SESSION_TOKEN | |
DOCKER_SSH_FLAGS += -e SSH_AUTH_SOCK=/ssh-agent | |
DOCKER_SSH_FLAGS += -v ${SSH_AUTH_SOCK}:/ssh-agent | |
DOCKER_GITHUB_FLAGS += -e GITHUB_TOKEN | |
DOCKER_GITHUB_FLAGS += -e GITHUB_ORGANIZATION | |
DOCKER_FLAGS += ${DOCKER_RUN_FLAGS} | |
DOCKER_RUN_CMD = docker run ${DOCKER_FLAGS} ${BUILD_TOOLS_DOCKER_IMAGE} | |
.PHONY: default | |
default: help | |
## Run the pre-commit hooks inside build-tools docker | |
.PHONY: test/pre-commit | |
test/pre-commit: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS} | |
test/pre-commit: DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS} | |
test/pre-commit: | |
$(call docker-run,pre-commit run -a) | |
## Run terraform init inside build-tools docker | |
.PHONY: terraform/init | |
terraform/init: DOCKER_FLAGS += ${DOCKER_AWS_FLAGS} | |
terraform/init: DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS} | |
terraform/init: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS} | |
terraform/init: | |
$(call docker-run,terraform init -input=false) | |
## Run terraform plan inside build-tools docker | |
.PHONY: terraform/plan | |
terraform/plan: DOCKER_FLAGS += ${DOCKER_AWS_FLAGS} | |
terraform/plan: DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS} | |
terraform/plan: | |
$(call docker-run,terraform plan -input=false -out=${TERRAFORM_PLANFILE}) | |
## Run terraform apply inside build-tools docker | |
.PHONY: terraform/apply | |
terraform/apply: DOCKER_FLAGS += ${DOCKER_AWS_FLAGS} | |
terraform/apply: DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS} | |
terraform/apply: | |
$(call docker-run,terraform apply -input=false -auto-approve ${TERRAFORM_PLANFILE}) | |
## remove .terraform and *.tfplan | |
.PHONY: clean | |
clean: | |
$(call rm-command,.terraform) | |
$(call rm-command,*.tfplan) | |
## 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) | |
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}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment