Created
September 17, 2020 06:53
-
-
Save lesovsky/f5e525c46fa9d7288018dbf54831e267 to your computer and use it in GitHub Desktop.
Makefile example for Go service
This file contains 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
DOCKER_ACCOUNT = myaccount | |
SITENAME = mysite | |
APPNAME = service-name | |
COMMIT=$(shell git rev-parse --short HEAD) | |
BRANCH=$(shell git rev-parse --abbrev-ref HEAD) | |
LDFLAGS = -a -installsuffix cgo -ldflags "-X main.appName=${APPNAME} -X main.gitCommit=${COMMIT} -X main.gitBranch=${BRANCH}" | |
DESTDIR ?= | |
.PHONY: help \ | |
clean test test-verbose test-coverage test-coverage-html \ | |
build docker-build docker-push deploy | |
.DEFAULT_GOAL := help | |
help: ## Display this help screen | |
@echo "Makefile available targets:" | |
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " * \033[36m%-15s\033[0m %s\n", $$1, $$2}' | |
clean: ## Clean | |
rm -f ${APPNAME} | |
dep: ## Get the dependencies | |
go mod download | |
lint: ## Lint the source files | |
yamllint -c .yamllint.yml static/assets | |
golangci-lint run --timeout 5m -E golint -e '(method|func) [a-zA-Z]+ should be [a-zA-Z]+' | |
gosec -quiet ./... | |
test: dep ## Run tests | |
go test -race -timeout 300s -coverprofile=.test_coverage.txt ./... && \ | |
go tool cover -func=.test_coverage.txt | tail -n1 | awk '{print "Total test coverage: " $$3}' | |
@rm .test_coverage.txt | |
build: dep ## Build executable | |
CGO_ENABLED=0 GOOS=linux GOARCH=${GOARCH} go build ${LDFLAGS} -o ${APPNAME} ./service/cmd | |
docker-build: ## Build docker image | |
docker build -t ${DOCKER_ACCOUNT}/${SITENAME}-${APPNAME}:${COMMIT} . | |
docker image prune --force --filter label=stage=intermediate | |
docker tag ${DOCKER_ACCOUNT}/${SITENAME}-${APPNAME}:${COMMIT} ${DOCKER_ACCOUNT}/${SITENAME}-${APPNAME}:latest | |
docker-push: ## Push docker image to registry | |
docker push ${DOCKER_ACCOUNT}/${SITENAME}-${APPNAME}:${COMMIT} | |
docker push ${DOCKER_ACCOUNT}/${SITENAME}-${APPNAME}:latest | |
deploy: ## Deploy this application | |
ansible-playbook deployment/ansible/deploy.yml -e env=${ENV} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment