Skip to content

Instantly share code, notes, and snippets.

@jgeiger
Created January 28, 2016 15:49
Show Gist options
  • Save jgeiger/dae5bc65860c93881fb3 to your computer and use it in GitHub Desktop.
Save jgeiger/dae5bc65860c93881fb3 to your computer and use it in GitHub Desktop.
Default Makefile for building Go projects including Docker containers
SHELL := /bin/bash
PROJECT := github.com/jgeiger/example
PKGS := $(shell go list ./... | grep -v /vendor)
EXECUTABLE := example
PKG := example
DOCKER_REGISTRY := jgeiger
DOCKER_IMAGE_NAME := example
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null)
.PHONY: build test golint docs $(PROJECT) $(PKGS) vendor
GOVERSION := $(shell go version | grep 1.5)
ifeq "$(GOVERSION)" ""
$(error must be running Go version 1.5)
endif
export GO15VENDOREXPERIMENT=1
all: test build
FGT := $(GOPATH)/bin/fgt
$(FGT):
go get github.com/GeertJohan/fgt
GOLINT := $(GOPATH)/bin/golint
$(GOLINT):
go get github.com/golang/lint/golint
GOVENDOR := $(GOPATH)/bin/govendor
$(GOVENDOR):
go get -u github.com/kardianos/govendor
GO_LDFLAGS := -X $(shell go list ./$(PACKAGE)).GitCommit=$(GIT_COMMIT)
test: $(PKGS)
$(PKGS): $(GOLINT) $(FGT)
@echo "FORMATTING"
@$(FGT) gofmt -l=true $(GOPATH)/src/$@/*.go
@echo "LINTING"
@$(FGT) $(GOLINT) $(GOPATH)/src/$@/*.go
@echo "VETTING"
@go vet -v $@
@echo "TESTING"
@go test -v $@
vendor: $(GOVENDOR)
$(GOVENDOR) add +external
build:
go build -i -ldflags "$(GO_LDFLAGS)" -o bin/$(EXECUTABLE) $(PROJECT)
build-linux:
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -i -ldflags "$(GO_LDFLAGS)" -o bin/$(EXECUTABLE)-linux-amd64 $(PROJECT)
container: build-linux
docker-compose build $(DOCKER_IMAGE_NAME)
production-container: build-linux
docker build --rm -t $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_NAME) .
push-container: production-container
docker push $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_NAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment