From 83ba4b225a11ac166d783d75d8735ef885c0324d Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Mon, 5 Jun 2017 17:46:12 -0400 Subject: [PATCH] add versions and binaries Signed-off-by: Jess Frazelle --- .gitignore | 1 + .travis.yml | 21 +++++++ Dockerfile.dev | 3 +- Makefile | 114 +++++++++++++++++++++++++++++-------- README.md | 17 ++++++ VERSION | 1 + main.go | 6 +- registry/tokentransport.go | 2 +- version/version.go | 7 +++ 9 files changed, 143 insertions(+), 29 deletions(-) create mode 100644 VERSION create mode 100644 version/version.go diff --git a/.gitignore b/.gitignore index df38b31a..cc6ce1de 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,4 @@ reg server/server testreg .certs +cross diff --git a/.travis.yml b/.travis.yml index fdab82cc..caa1d86d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,3 +20,24 @@ - test -z "$(golint ./... | grep -v vendor | tee /dev/stderr)" - test -z "$(gofmt -s -l . | grep -v vendor | tee /dev/stderr)" - make dind dtest + - make release + deploy: + provider: releases + api_key: + secure: "xz4uJ+vrF5+u3zucCPdnoXR/a0i8/oUzzDABuKiaB9AFFjrM8obLYo2AgMlP5zj/YHpGgTP51m/sx/qwJKfNvCoR2alBb2taYzJnhCfXzOuviZ0RZbM2LqA72lutdAzZ5eyMPCXcqvOjf6INnCmqQeJjDWo8UzGKSlWP2cqU/Qovs1vzurImME86DjqQ4EDaYlZS3tVc5BtEqmhylT2q0aO7gNJcMunDJpIEwb3vo8bbOoS6heQO2DVFf553lnZTSheEOERiF8r/O3vdMBCIqq7Xr2WIzJ4WGoNqzCk4sVcOZYP1yWa4Je/J09TaM8Uam+SZCG8p2lG+lr9toNv9jDHAA3Z986hAj+1NhRXTbwtRYM/KfL38UegvGfFCRvOAc+3AQhQaw1p2hX599in4zl/IcSVjF6IytJGj+JrCHU1p5Bd9qphFQKlXAXQKZwH+TKt3QTnrUQIUOn0QwcfgbvDUaA2XMsR9f0BWNshILvz79JJZmwXY7C7ufVSKdL+T+9dNn/5N7dMn6fWb7ZruwK3N6gLyVSulMinSYyNIHGiEH3mdoBr020KYD1w1+cfK4Ov6B8vf9k7atzHDPRklm2X0hvda2T0UXOv5+hr+OlvdhpqZKDB2HkVOUQUUfk7cL88u+FpU6pktlhJVLSCl292jWS05I1AYOiHChEFONeE=" + go: 1.7 + file: + - cross/reg-darwin-386 + - cross/reg-freebsd-386 + - cross/reg-linux-386 + - cross/reg-windows-386 + - cross/reg-darwin-amd64 + - cross/reg-freebsd-amd64 + - cross/reg-linux-amd64 + - cross/reg-linux-arm + - cross/reg-linux-arm64 + - cross/reg-solaris-amd64 + - cross/reg-windows-amd64 + skip_cleanup: true + on: + tags: true diff --git a/Dockerfile.dev b/Dockerfile.dev index ba40b965..b30793cd 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -1,4 +1,5 @@ FROM golang:alpine RUN apk add --no-cache \ - build-base + build-base \ + git diff --git a/Makefile b/Makefile index b431db29..7ac5aa2c 100644 --- a/Makefile +++ b/Makefile @@ -1,45 +1,109 @@ # Set an output prefix, which is the local directory if not specified PREFIX?=$(shell pwd) -BUILDTAGS= -.PHONY: clean all fmt vet lint build test install static -.DEFAULT: default +# Setup name variables for the package/tool +NAME := reg +PKG := github.com/jessfraz/$(NAME) -all: clean build fmt lint test vet install +# Set any default go build tags +BUILDTAGS := -build: +# 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) +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 = darwin/amd64 darwin/386 freebsd/amd64 freebsd/386 linux/arm linux/arm64 linux/amd64 linux/386 solaris/amd64 windows/amd64 windows/386 + +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 @echo "+ $@" - @go build -tags "$(BUILDTAGS) cgo" . + go build -tags "$(BUILDTAGS)" ${GO_LDFLAGS} -o $(NAME) . -static: +.PHONY: static +static: ## Builds a static executable @echo "+ $@" - CGO_ENABLED=1 go build -tags "$(BUILDTAGS) cgo static_build" -ldflags "-w -extldflags -static" -o reg . + CGO_ENABLED=0 go build \ + -tags "$(BUILDTAGS) static_build" \ + ${GO_LDFLAGS_STATIC} -o $(NAME) . -fmt: +.PHONY: fmt +fmt: ## Verifies all files have men `gofmt`ed @echo "+ $@" - @gofmt -s -l . | grep -v vendor | tee /dev/stderr + @gofmt -s -l . | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr -lint: +.PHONY: lint +lint: ## Verifies `golint` passes @echo "+ $@" - @golint ./... | grep -v vendor | tee /dev/stderr + @golint ./... | grep -v '.pb.go:' | grep -v vendor | tee /dev/stderr -test: +.PHONY: test +test: ## Runs the go tests @echo "+ $@" @go test -v -tags "$(BUILDTAGS) cgo" $(shell go list ./... | grep -v vendor) -vet: +.PHONY: vet +vet: ## Verifies `go vet` passes @echo "+ $@" - @go vet $(shell go list ./... | grep -v vendor) + @go vet $(shell go list ./... | grep -v vendor) | grep -v '.pb.go:' | tee /dev/stderr -clean: - @echo "+ $@" - @rm -rf reg - @rm -rf $(CURDIR)/.certs - -install: +.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} .; +endef + +.PHONY: cross +cross: *.go 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} .; +endef + +.PHONY: release +release: *.go 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: clean +clean: ## Cleanup any build binaries or packages + @echo "+ $@" + $(RM) $(NAME) + $(RM) -r $(BUILDDIR) + $(RM) -r $(CURDIR)/.certs + # set the graph driver as the current graphdriver if not set DOCKER_GRAPHDRIVER := $(if $(DOCKER_GRAPHDRIVER),$(DOCKER_GRAPHDRIVER),$(shell docker info 2>&1 | grep "Storage Driver" | sed 's/.*: //')) export DOCKER_GRAPHDRIVER @@ -55,7 +119,7 @@ endif .PHONY: dind DIND_CONTAINER=reg-dind DIND_DOCKER_IMAGE=r.j3ss.co/docker:userns -dind: +dind: ## Starts a docker-in-docker container for running the tests with docker build --rm --force-rm -f Dockerfile.dind -t $(DIND_DOCKER_IMAGE) . docker run -d \ -v /var/lib/docker2:/var/lib/docker \ @@ -78,7 +142,7 @@ dind: .PHONY: dtest DOCKER_IMAGE := reg-dev -dtest: +dtest: ## Run the tests in a docker container docker build --rm --force-rm -f Dockerfile.dev -t $(DOCKER_IMAGE) . docker run --rm -i $(DOCKER_FLAGS) \ -v $(CURDIR):/go/src/github.com/jessfraz/reg \ @@ -92,3 +156,7 @@ dtest: -e DOCKER_API_VERSION=1.23 \ $(DOCKER_IMAGE) \ make test + +.PHONY: help +help: + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/README.md b/README.md index d0c68822..23f889bd 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Docker registry v2 command line client. +- [Installation](#installation) - [Usage](#usage) - [Auth](#auth) - [List Repositories and Tags](#list-repositories-and-tags) @@ -13,6 +14,22 @@ Docker registry v2 command line client. - [Vulnerability Reports](#vulnerability-reports) - [Testing](#testing) +## Installation + +#### Binaries + +- **darwin** [386](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-darwin-386) / [amd64](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-darwin-amd64) +- **freebsd** [386](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-freebsd-386) / [amd64](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-freebsd-amd64) +- **linux** [386](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-linux-386) / [amd64](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-linux-amd64) / [arm](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-linux-arm) / [arm64](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-linux-arm64) +- **solaris** [amd64](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-solaris-amd64) +- **windows** [386](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-windows-386) / [amd64](https://github.com/jessfraz/reg/releases/download/v0.3.0/reg-windows-amd64) + +#### Via Go + +```bash +$ go get github.com/jessfraz/reg +``` + ## Usage ```console diff --git a/VERSION b/VERSION new file mode 100644 index 00000000..268b0334 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +v0.3.0 diff --git a/main.go b/main.go index df693feb..16ce95cb 100644 --- a/main.go +++ b/main.go @@ -15,14 +15,12 @@ import ( "github.com/jessfraz/reg/clair" "github.com/jessfraz/reg/registry" "github.com/jessfraz/reg/utils" + "github.com/jessfraz/reg/version" digest "github.com/opencontainers/go-digest" "github.com/urfave/cli" ) const ( - // VERSION is the binary version. - VERSION = "v0.2.0" - dockerConfigPath = ".docker/config.json" ) @@ -71,7 +69,7 @@ func preload(c *cli.Context) (err error) { func main() { app := cli.NewApp() app.Name = "reg" - app.Version = VERSION + app.Version = fmt.Sprintf("version %s, build %s", version.VERSION, version.GITCOMMIT) app.Author = "@jessfraz" app.Email = "no-reply@butts.com" app.Usage = "Docker registry v2 client." diff --git a/registry/tokentransport.go b/registry/tokentransport.go index 7e34497a..d713229d 100644 --- a/registry/tokentransport.go +++ b/registry/tokentransport.go @@ -151,7 +151,7 @@ func (r *Registry) Token(url string) (string, error) { } if authToken.Token == "" { - return "", errors.New("Auth token cannot be empty.") + return "", errors.New("Auth token cannot be empty") } return authToken.Token, nil diff --git a/version/version.go b/version/version.go new file mode 100644 index 00000000..8088a275 --- /dev/null +++ b/version/version.go @@ -0,0 +1,7 @@ +package version + +// VERSION indicates which version of the binary is running. +var VERSION string + +// GITCOMMIT indicates which git hash the binary was built off of +var GITCOMMIT string