Skip to content

Commit

Permalink
Use a Makefile to build the project
Browse files Browse the repository at this point in the history
Shameless copy of the amazing one found in https://github.com/jessfraz/amicontained
Binaries are uploaded by Travis CI when building tag.
  • Loading branch information
jcgay committed Oct 14, 2017
1 parent 7448639 commit 0e4b452
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ crashlytics-build.properties
fabric.properties

.autoenv.zsh

# Cross Platform builds
cross/
29 changes: 27 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
language: go
sudo: false
go:
- tip
- 1.9.x
before_install:
- go get github.com/mattn/goveralls
- go get github.com/golang/lint/golint
script:
- $HOME/gopath/bin/goveralls -service=travis-ci

- make all release
before_deploy:
- make prepare-bintray
deploy:
- provider: releases
api_key:
secure: M2iP8Bb3ICWHftyqOEKDfLLq68sOZYB3ALA1nCi7g7N95Ebot5szTNCcZt7ORpdXuFdrsZwKWrv9g0rM4L4dwn2ndjQ9WZf3/LSpfb7EeMK1TKgcHnTAAekRwkJPEvb7ZpMIBDHxoAU719QoyA64YodVkMUm1vqGHuv1h4hx4IpaanJtLITqPPhTxW3kCTa0K9sRcnwbm03ptvLBRcDiQJ0mukjaMesKg65P4S5sP1Nqh/HN9BkdQEI7UjftfGdoTlI+uRYF1yHc+a550dQfo09osCPnlmuK/SwUXp7HpSPP0nHHNs1VYwfheVes0ru28ZoxebVH+9u8RwjNsEWGIT6vNDaBJdeVl86T7ehgJLYhkPg5GJL6+4fTKD3AyjtfH2gmahsjIpaNdGC9WdH6OmWzc+cQyr+vNfLdlmze+sQR3PwnCxZErJzggIvpuNVsaCP3d4W1J5JYdZ9M2uGDz3CF49betD1Z+PcbJm9WXBlr2c6SiH2UOCprMc/9Lgrfn9cm6Q41HMyC7KHmPS/QIS2TgEn2vnC4yTudxXvpqOZhhGTslNXup0YJhaRdTswjFg7Isa+/TY7kXYFQAw37pWphljzXers665y0hUvOs4hF9MPrKRLZ9xNIA5QU7THskOrwvC5wJ2VBIiazbBe9UaxkAbXsr4zq/1jN6vkrGiE=
go: 1.9.x
file_glob: true
file: cross/*
skip_cleanup: true
on:
repo: jcgay/protector
tags: true
- provider: bintray
user: jcgay
key:
secure: f9fLVTrDNWakP6D0JFK2YwqDOIO5tXbv4b4GkTQlBjSL2+FrLU+0AJXqm/6nDOFbPprgzocHxm4F61Bg89uKLRvwqKpmM55vV+kxfo5ilZFlbyD36ryoUdMTyCEFrwjCX/EKiMNnzYuo+WfnKDtm7Y99ccWFt0AMfRg3lGDgVd4LXhAUWYcqetsyW9IIYwzMO1w/mUEawFB6QyfJzzh32b0axBg6aYVv5UKdDora4Rn4ltr0gGSjfZqbUbp3aQgvWZZMzkCQyhC0GeX6nmEBEwHvuhxoXFvzt8a3eKGLhpU605Tr9bdAwag5GLxvQ4nYSbG059HJ1NvSppZWQrIbvK+PwiLoC1uR/XYs8aAI6qZKxN47mlPqqOnAdmkYn70EBchv+93YrVVpn3XCjPmS+Rzkp56lUlKTnu9Sm84Y/eXab7MffzxsIJiX2d4QhqA2NdmRoLCbrEavT8NFTA9VBivX8tkj5CeGTtlgpL2VTWg8HCwgOK0sjCb00afZqACfviSV4bM+7J/TePJjFqJwTrJ15KRKShdjSa8l2PzeuQV6YlOijLWd7aLNNAemVtYBDtST0FPM2oXWaiQFcUjXGrPudONMnrBuISuetew9iEV7gxx5qEvm1GHq9Uyi1SEfvh/Ly1ZsQBmvVAeMmjr0Ief3rmcqjjvU+ieA6V2qbFc=
go: 1.9.x
file: bintray/descriptor.json
skip_cleanup: true
on:
repo: jcgay/protector
tags: true
151 changes: 122 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,122 @@
OS = darwin freebsd linux openbsd windows
ARCHS = 386 arm amd64 arm64

all: build release

build: deps
go build

release: clean deps
@for arch in $(ARCHS);\
do \
for os in $(OS);\
do \
echo "Building $$os-$$arch"; \
mkdir -p build/protector-$$os-$$arch/; \
GOOS=$$os GOARCH=$$arch go build -o build/protector-$$os-$$arch/protector; \
tar cz -C build -f build/protector-$$os-$$arch.tar.gz protector-$$os-$$arch; \
done \
done

test: deps
go test ./...

deps:
go get -d -v -t ./...

clean:
rm -rf build
rm -f protector
# Prepare a GNU sed for macOS users (brew install gnu-sed)
sed = $(shell { command -v gsed || command -v sed; } 2>/dev/null)

# Set an output prefix, which is the local directory if not specified
PREFIX?=$(shell pwd)

# Setup name variables for the package/tool
NAME := protector
PKG := github.com/jcgay/$(NAME)

# Set any default go build tags
BUILDTAGS :=

# Set the build dir, where built cross-compiled binaries will be output
BUILDDIR := ${PREFIX}/cross

# Populate version variables
# Add to compile time flags
VERSION := $(shell cat version/VERSION)
GITCOMMIT := $(shell git rev-parse --short HEAD)
GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no)
ifneq ($(GITUNTRACKEDCHANGES),)
GITCOMMIT := $(GITCOMMIT)-dirty
endif
CTIMEVAR=-X $(PKG)/version.GITCOMMIT=$(GITCOMMIT) -X $(PKG)/version.VERSION=$(VERSION)
GO_LDFLAGS=-ldflags "-w $(CTIMEVAR)"
GO_LDFLAGS_STATIC=-ldflags "-w $(CTIMEVAR) -extldflags -static"

# List the GOOS and GOARCH to build
GOOSARCHES = linux/amd64 linux/386 darwin/amd64 windows/amd64

TODAY = $(shell date +"%Y-%m-%d")

all: clean build fmt lint test vet install ## Runs a clean, build, fmt, lint, test, vet and install

.PHONY: build
build: $(NAME) ## Builds a dynamic executable or package

$(NAME): *.go version/VERSION
@echo "+ $@"
go build -tags "$(BUILDTAGS)" ${GO_LDFLAGS} -o $(NAME) .

.PHONY: static
static: ## Builds a static executable
@echo "+ $@"
CGO_ENABLED=0 go build \
-tags "$(BUILDTAGS) static_build" \
${GO_LDFLAGS_STATIC} -o $(NAME) .

.PHONY: fmt
fmt: ## Verifies all files have men `gofmt`ed
@echo "+ $@"
@gofmt -s -l . | grep -v vendor | tee /dev/stderr

.PHONY: lint
lint: ## Verifies `golint` passes
@echo "+ $@"
@golint ./... | grep -v vendor | tee /dev/stderr

.PHONY: test
test: ## Runs the go tests
@echo "+ $@"
@go test -v -tags "$(BUILDTAGS)" $(shell go list ./... | grep -v vendor)

.PHONY: vet
vet: ## Verifies `go vet` passes
@echo "+ $@"
@go vet $(shell go list ./... | grep -v vendor) | tee /dev/stderr

.PHONY: install
install: ## Installs the executable or package
@echo "+ $@"
@go install .

define buildpretty
mkdir -p $(BUILDDIR)/$(1)/$(2);
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 go build \
-o $(BUILDDIR)/$(1)/$(2)/$(NAME) \
-a -tags "$(BUILDTAGS) static_build netgo" \
-installsuffix netgo ${GO_LDFLAGS_STATIC} .;
md5sum $(BUILDDIR)/$(1)/$(2)/$(NAME) > $(BUILDDIR)/$(1)/$(2)/$(NAME).md5;
sha256sum $(BUILDDIR)/$(1)/$(2)/$(NAME) > $(BUILDDIR)/$(1)/$(2)/$(NAME).sha256;
endef

.PHONY: cross
cross: *.go version/VERSION ## Builds the cross-compiled binaries, creating a clean directory structure (eg. GOOS/GOARCH/binary)
@echo "+ $@"
$(foreach GOOSARCH,$(GOOSARCHES), $(call buildpretty,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH))))

define buildrelease
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 go build \
-o $(BUILDDIR)/$(NAME)-$(1)-$(2) \
-a -tags "$(BUILDTAGS) static_build netgo" \
-installsuffix netgo ${GO_LDFLAGS_STATIC} .;
md5sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).md5;
sha256sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).sha256;
endef

.PHONY: release
release: *.go version/VERSION ## Builds the cross-compiled binaries, naming them in such a way for release (eg. binary-GOOS-GOARCH)
@echo "+ $@"
$(foreach GOOSARCH,$(GOOSARCHES), $(call buildrelease,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH))))

.PHONY: tag
tag: ## Create a new git tag to prepare to build a release
git tag -sa $(VERSION) -m "$(VERSION)"
@echo "Run git push origin $(VERSION) to push your new tag to GitHub and trigger a travis build."

.PHONY: prepare-bintray
prepare-bintray: ## Update bintray/descriptor.json with current version
$(sed) -i s/%VERSION%/$(VERSION)/g bintray/descriptor.json
$(sed) -i s/%DATE%/$(TODAY)/g bintray/descriptor.json

.PHONY: clean
clean: ## Cleanup any build binaries or packages
@echo "+ $@"
$(RM) $(NAME)
$(RM) -r $(BUILDDIR)

.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,18 @@ protector - v0.1.0-SNAPSHOT

[![Build Status](https://travis-ci.org/jcgay/protector.svg?branch=master)](https://travis-ci.org/jcgay/protector)
[![Code Report](https://goreportcard.com/badge/github.com/jcgay/protector)](https://goreportcard.com/report/github.com/jcgay/protector)
[![Coverage Status](https://coveralls.io/repos/github/jcgay/protector/badge.svg?branch=master)](https://coveralls.io/github/jcgay/protector?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/jcgay/protector/badge.svg?branch=master)](https://coveralls.io/github/jcgay/protector?branch=master)

### Release

make release

### List available tasks

make help

### macOS

To run `md5sum` and `sha256sum`, I use zsh aliases:

make <task> SHELL=/bin/zsh
31 changes: 31 additions & 0 deletions bintray/descriptor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"package": {
"name": "protector",
"repo": "tools",
"subject": "jcgay",
"desc": "Protect GitHub branches",
"website_url": "https://github.com/jcgay/protector",
"issue_tracker_url": "https://github.com/jcgay/protector/issues",
"vcs_url": "https://github.com/jcgay/protector.git",
"github_use_tag_release_notes": true,
"github_release_notes_file": "CHANGELOG.md",
"licenses": ["MIT"],
"labels": ["github", "protection", "go"],
"public_download_numbers": false,
"public_stats": false
},

"version": {
"name": "%VERSION%",
"desc": "%VERSION%",
"released": "%DATE%",
"vcs_tag": "%VERSION%",
"gpgSign": false
},

"files":
[
{"includePattern": "cross/(.*)", "uploadPattern": "%VERSION%/$1"}
],
"publish": true
}
9 changes: 4 additions & 5 deletions protector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"fmt"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
currentVersion "github.com/jcgay/protector/version"
"os"
"regexp"
"sync"
)

const (
banner = "protector - v%s\n"
// VERSION injected at compile time by goxc (see https://github.com/laher/goxc/wiki/versioning#version-number-interpolation)
VERSION = "0.1.0-SNAPSHOT"
banner = "protector - %s (%s)\n"
)

var (
Expand Down Expand Up @@ -58,14 +57,14 @@ func main() {
flag.Var(&branches, "branches", "branches to include (as regexp)")

flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(banner, VERSION))
fmt.Fprint(os.Stderr, fmt.Sprintf(banner, currentVersion.VERSION, currentVersion.GITCOMMIT))
flag.PrintDefaults()
}

flag.Parse()

if version {
fmt.Printf("v%s", VERSION)
fmt.Printf("%s (%s)", currentVersion.VERSION, currentVersion.GITCOMMIT)
os.Exit(0)
}

Expand Down
1 change: 1 addition & 0 deletions version/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.0.0
7 changes: 7 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package version

// VERSION indicates which version of the binary is running.
var VERSION = "unknown-snapshot"

// GITCOMMIT indicates which git hash the binary was built off of
var GITCOMMIT = "unknown-commit"

0 comments on commit 0e4b452

Please sign in to comment.