Makefile for Go Apps
Makefile to automate and preserve routine Go Application activities
Makefile to Automate Go Applicaton Activity.
A typical go app will have a set of activities we do
- build
- run
- test
- ….
we shoudld not keep this information in anywhere seperate, it needs to be presevered in a central location for others to use.
Makefile
.PHONY: help
help:
@echo 'Usage:'
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
## serve: start Hugo Server
serve:
hugo server -D --disableFastRender
# Build production site
## build: build hugo site
build: clean
hugo --gc --minify
# Clear build artifacts
## clear : clear all crap files
clean:
rm -rf public resources
# Example Deployment (Modify this matching your hosting, e.g., GitHub Pages, AWS, or Rsync)
## deploy : deploy application
deploy: build
@echo "🚀 Deploying to production..."
# Example for GitHub Pages or custom script:
# git add public && git commit -m "Build site" && git push origin main
List down repeated activty
- Build
- Run
- Running test cases
- Auditing Application
Create commands for each activity.
- Build
## build: build hugo site
build:
@echo 'Building app...'
go build -o .
- test
@echo 'Run Unit tests...'
go test ./...
place them in Makefile for rapid reusability.
# Change these variables as necessary.
main_package_path = ./cmd/example
binary_name = example
# ==================================================================================== #
# HELPERS
# ==================================================================================== #
## help: print this help message
.PHONY: help
help:
@echo 'Usage:'
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
# ==================================================================================== #
# QUALITY CONTROL
# ==================================================================================== #
## audit: run quality control checks
.PHONY: audit
audit: test
go mod tidy -diff
go mod verify
go vet ./...
go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000 ./...
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
go run github.com/golangci/golanggci-lint/cmd/golangci-lint@latest run -v ./...
## test: run all tests
.PHONY: test
test:
go test -v -race -buildvcs ./...
## test/cover: run all tests and display coverage
.PHONY: test/cover
test/cover:
go test -v -race -buildvcs -coverprofile=/tmp/coverage.out ./...
go tool cover -html=/tmp/coverage.out
## upgradeable: list direct dependencies that have upgrades available
.PHONY: upgradeable
upgradeable:
@echo 'Upgrade Go dependecies...'
go get -U ./...
go mod tidy
go mod verify
## gen-test-cover: Generate Test Coverage
.PHONY: upgradeable
upgradeable:
@echo 'Generating Test Coverage...'
go test -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
go tool cover -html=./coverage.out -o coverage.html
# ==================================================================================== #
# DEVELOPMENT
# ==================================================================================== #
## tidy: tidy modfiles and modernize and format .go files
.PHONY: tidy
tidy:
@echo 'Tidy the app ...'
go mod tidy -v
go fix ./...
go fmt ./...
## build: build the application
.PHONY: build
build:
# Include additional build steps, like TypeScript, SCSS or Tailwind compilation here...
go build -o=/tmp/bin/${binary_name} ${main_package_path}
## run: run the application
.PHONY: run
run: build
/tmp/bin/${binary_name}
## run/live: run the application with reloading on file changes
.PHONY: run/live
run/live:
go run github.com/cosmtrek/air@v1.43.0 \
--build.cmd "make build" --build.bin "/tmp/bin/${binary_name}" --build.delay "100" \
--build.exclude_dir "" \
--build.include_ext "go, tpl, tmpl, html, css, scss, js, ts, sql, jpeg, jpg, gif, png, bmp, svg, webp, ico" \
--misc.clean_on_exit "true"
# ==================================================================================== #
# OPERATIONS
# ==================================================================================== #
## push: push changes to the remote Git repository
.PHONY: push
push: confirm audit no-dirty
git push
## production/deploy: deploy the application to production
.PHONY: production/deploy
production/deploy: confirm audit no-dirty
GOOS=linux GOARCH=amd64 go build -ldflags='-s' -o=/tmp/bin/linux_amd64/${binary_name} ${main_package_path}
upx -5 /tmp/bin/linux_amd64/${binary_name}
# Include additional deployment steps here...
audit: run quality control checks
.PHONY: audit
audit: test
go mod tidy -diff
go mod verify
go vet ./...
go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000 ./...
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
go run github.com/golangci/golanggci-lint/cmd/golangci-lint@latest run -v ./...