This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
148 lines (126 loc) · 4.09 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
NAME := lambda-blueprint
PKG := github.com/spring-media/$(NAME)
VERSION ?= $(shell cat VERSION.txt)+$(shell git rev-parse --short HEAD)
REGION ?= eu-west-1
ENVIRONMENT ?= production
AUTO_DEPLOY ?= false
S3_BUCKET := tf-$(NAME)-deployment-$(REGION)
# Set an output prefix, which is the local directory if not specified
PREFIX?=$(shell pwd)
# Set the build dir, where built cross-compiled binaries will be output
BUILDDIR := $(PREFIX)/bin
FUNCDIR := func
all: clean build fmt lint test staticcheck vet
.PHONY: build
build: ## Builds static executables
@echo "+ $@"
@for dir in `ls $(FUNCDIR)`; do \
CGO_ENABLED=0 go build -o $(BUILDDIR)/$$dir $(PKG)/$(FUNCDIR)/$$dir; \
done
.PHONY: fmt
fmt: ## Verifies all files have men `gofmt`ed
@echo "+ $@"
@gofmt -s -l . | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr
.PHONY: lint
lint: ## Verifies `golint` passes
@echo "+ $@"
@golint ./... | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr
.PHONY: vet
vet: ## Verifies `go vet` passes
@echo "+ $@"
@go vet $(shell go list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr
.PHONY: staticcheck
staticcheck: ## Verifies `staticcheck` passes
@echo "+ $@"
@staticcheck $(shell go list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr
.PHONY: test
test: ## Runs the go tests
@echo "+ $@"
go test -v $(shell go list ./... | grep -v vendor)
.PHONY: cover
cover: ## Runs go test with coverage
@echo "" > coverage.txt
@for d in $(shell go list ./... | grep -v vendor); do \
go test -race -coverprofile=profile.out -covermode=atomic "$$d"; \
if [ -f profile.out ]; then \
cat profile.out >> coverage.txt; \
rm profile.out; \
fi; \
done;
.PHONY: release
release: ## Builds cross-compiled functions
@echo "+ $@ $(VERSION)"
@for dir in `ls $(FUNCDIR)`; do \
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build \
-o $(BUILDDIR)/$$dir -a $(PKG)/$(FUNCDIR)/$$dir; \
done
.PHONY: s3-init
s3-init: ## Creates AWS S3 bucket for deployments
@echo "+ $@"
@aws s3api create-bucket \
--bucket=$(S3_BUCKET) \
--create-bucket-configuration LocationConstraint=$(REGION) \
--region=$(REGION)
.PHONY: s3-destroy
s3-destroy: ## Removes AWS S3 bucket including all deployments
@echo "+ $@"
@echo s3://$(S3_BUCKET)
@aws s3 rb s3://$(S3_BUCKET) --force
.PHONY: package
package: release ## Creates and uploads S3 deployment packages for all functions
@echo "+ $@"
@echo "Uploading release $(VERSION)"
@for dir in `ls $(FUNCDIR)`; do \
zip -j $(BUILDDIR)/$$dir.zip bin/$$dir; \
aws s3 cp $(BUILDDIR)/$$dir.zip s3://$(S3_BUCKET)/$(VERSION)/$$dir.zip --region=$(REGION); \
done
.PHONY: init
init: ## Initialize Terraform working directory and backend configuration
@echo "+ $@"
@cd terraform && terraform init \
-input=false \
-backend-config="key=$(ENVIRONMENT)/$(NAME)/terraform.tfstate"
.PHONY: validate
validate: ## Validates and rewrited the Terraform files to canonical format
@echo "+ $@"
@cd terraform && terraform fmt -check=true
@cd terraform && terraform validate \
-var version=$(VERSION) \
-var region=$(REGION) \
-var s3_bucket=$(S3_BUCKET)
.PHONY: plan
plan: ## Generate and show a Terraform execution plan
@echo "+ $@"
@cd terraform && terraform plan \
-input=false \
-var version=$(VERSION) \
-var region=$(REGION) \
-var s3_bucket=$(S3_BUCKET)
ifeq ($(AUTO_DEPLOY), true)
FORCE=-auto-approve
endif
.PHONY: deploy
deploy: ## Builds or changes infrastructure using Terraform
@echo "+ $@"
@cd terraform && terraform apply $(FORCE) -input=false \
-var version=$(VERSION) \
-var region=$(REGION) \
-var s3_bucket=$(S3_BUCKET)
.PHONY: destroy
destroy: ## Destroy Terraform-managed infrastructure
@echo "+ $@"
@cd terraform && terraform destroy \
-var version=$(VERSION) \
-var region=$(REGION) \
-var s3_bucket=$(S3_BUCKET)
.PHONY: clean
clean: ## Cleanup any build binaries or packages
@echo "+ $@"
$(RM) $(NAME)
$(RM) -r $(BUILDDIR)
@for dir in `ls $(FUNCDIR)`; do \
$(RM) $$dir ; \
done
.PHONY: help
help: ## Display this help screen
@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'