Use Makefile for your daily development tasks

A typical routine of a developer is code, build, unit test, and deploy. Most of the time, developers do all these tasks in their local first and once everything is fine then they move to the next state. This will be a very much repeated process when we work on a microservices architecture.

From my experience, If I can list out a typical list of tasks for a go language-based microservice. Those are

  1. Clean
  2. Format
  3. Lint
  4. Dependencies
  5. Run Unit tests
  6. Build
  7. Push to Dev Quay
  8. Coverage Report

All these steps can be placed in a simple Makefile and then onwards, the same can be used by anyone to quickly take the charge and move on. This kind is one of the best practices when it comes to day-to-day development activity [I am not denying that there are multiple ways of achieving the same]. Here is the typical Makefile from my experience.

The below sample make file is specific to Go language-based environment but I believe the idea can be used to leverage with any other project.

# Enable dependencies and configure the private repo
GO = GO111MODULE=on GOPRIVATE=<your private repo>.cisco.com
EXEFILE = ./bin/<exe file name>
MAIN_GO = <your service main.go file location>

# help messages
help:
    @echo 'Available commands for the <your micro service name>'
    @echo 'Usage'
    @echo '  all - Run all the available commands'
    @echo '  clean - Run the clean up'
    @echo '  setup -  Run the initial setup instructions'
    @echo '  format - Run code format'
    @echo '  staticanalysis - Run the static code analysis'
    @echo '  dependencies - Run the dependencies'
    @echo '  unittest - Run unit tests'
    @echo '  localbuild - Build the executable'

# Run all the available commands in the below order
all: clean setup dependencies format staticanalysis unittest localbuild

clean:
    @echo "Cleaning the previous executable and coverage reports"
    rm -f coveragereport.out report.json
    rm -f ${EXEFILE }

setup:
    @echo "Add any steps that are needed"
    <some installation tasks>
format:
    @echo "Code Formatting"
    ${GO} fmt ./...
staticanalysis:
    @echo ""
    golangci-lint run -c .golangci.yaml
dependencies:
    @echo ' Load Dependencies'
    ${GO} mod tidy
    ${GO} mod download
unittest:
    @echo "Executing Unit tests"
    ${GO} test -v covermode=atomic -count=1 ./... -coverprofile coveragereport.out
    ${GO} test -race test -covermode=atomic -count=1 ./... -json > report.json
localbuild:
    @echo "Loca build"
    ${GO} build -o ${EXEFILE} ${MAIN_GO}
    docker build -t <your tag name> .
    docker tag <your tag name> <your quay repo location>:latest
    docker push <your quay repo location>:latest