From d8835c0d4b9b2c44b4cf848b4ad0e6a91b88e2ec Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 7 Dec 2016 16:40:00 +0800 Subject: [PATCH] Support docker build for production. Signed-off-by: Bo-Yi Wu --- .gitignore | 1 + .travis.yml | 1 - Dockerfile | 8 +++++ Makefile | 94 +++++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 Dockerfile diff --git a/.gitignore b/.gitignore index af9ad9d..a77fff9 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ Dockerfile.tmp .cover *.pid *.db* +coverage.txt diff --git a/.travis.yml b/.travis.yml index ca42af0..0a09308 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,6 @@ before_install: - curl https://glide.sh/get | sh install: - - export GO15VENDOREXPERIMENT=1 - go get github.com/mattn/goveralls - make docker_build - make docker_production diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0889f97 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM centurylink/ca-certs +EXPOSE 8088 + +ADD config/config.yml / +ADD bin/gorush / + +ENTRYPOINT ["/gorush"] +CMD ["-c", "config.yml"] diff --git a/Makefile b/Makefile index b0f2004..8347f87 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,36 @@ .PHONY: all -DEPS := $(wildcard *.go) -BUILD_IMAGE := "gorush-build" -# docker hub project name. -PRODUCTION_IMAGE := "gorush" -DEPLOY_ACCOUNT := "appleboy" -ifeq ($(VERSION),) - VERSION := $(shell git describe --tags --always || git rev-parse --short HEAD) -endif -TARGETS_NOVENDOR := $(shell glide novendor) export PROJECT_PATH = /go/src/github.com/appleboy/gorush +DIST := dist +EXECUTABLE := gorush + +BUILD_IMAGE := "gorush-build" +DEPLOY_ACCOUNT := appleboy +DEPLOY_IMAGE := $(EXECUTABLE) + +TARGETS ?= linux/*,darwin/* +PACKAGES ?= $(shell go list ./... | grep -v /vendor/) +SOURCES ?= $(shell find . -name "*.go" -type f) +TAGS ?= +LDFLAGS += -X 'main.Version=$(VERSION)' + ifneq ($(shell uname), Darwin) EXTLDFLAGS = -extldflags "-static" $(null) else EXTLDFLAGS = endif +ifneq ($(DRONE_TAG),) + VERSION ?= $(DRONE_TAG) +else + ifneq ($(DRONE_BRANCH),) + VERSION ?= $(DRONE_BRANCH) + else + VERSION ?= $(shell git describe --tags --always || git rev-parse --short HEAD) + endif +endif + all: build init: @@ -30,20 +44,43 @@ ifeq ($(ANDROID_TEST_TOKEN),) endif @echo "Already set ANDROID_API_KEY and ANDROID_TEST_TOKEN globale variable." -install: +fmt: + go fmt $(PACKAGES) + +vet: + go vet $(PACKAGES) + +errcheck: + @which errcheck > /dev/null; if [ $$? -ne 0 ]; then \ + go get -u github.com/kisielk/errcheck; \ + fi + errcheck $(PACKAGES) + +lint: + @which golint > /dev/null; if [ $$? -ne 0 ]; then \ + go get -u github.com/golang/lint/golint; \ + fi + for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done; + +dep_install: glide install -update: - glide update +dep_update: + glide up -build_static: - go build -ldflags='${EXTLDFLAGS}-s -w -X main.Version=${VERSION}' -o bin/gorush gorush.go +install: $(wildcard *.go) + go install -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -build: clean +build: $(EXECUTABLE) + +$(EXECUTABLE): $(SOURCES) + go build -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o bin/$@ + +corss_build: clean sh script/build.sh $(VERSION) -test: redis_test boltdb_test memory_test buntdb_test leveldb_test config_test - go test -v -cover ./gorush/... +test: + for PKG in $(PACKAGES); do go test -v -cover -coverprofile $$GOPATH/src/$$PKG/coverage.txt $$PKG || exit 1; done; redis_test: init go test -v -cover ./storage/redis/... @@ -66,6 +103,14 @@ config_test: init html: go tool cover -html=.cover/coverage.txt +docker_binary_build: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o bin/$@ + +docker_image: + docker build -t $(DEPLOY_ACCOUNT)/$(DEPLOY_IMAGE) -f Dockerfile . + +docker_release: docker_binary_build docker_image + docker_build: tar -zcvf build.tar.gz gorush.go gorush config storage Makefile glide.lock glide.yaml sed -e "s/#VERSION#/$(VERSION)/g" docker/Dockerfile.build > docker/Dockerfile.tmp @@ -73,24 +118,23 @@ docker_build: docker run --rm $(BUILD_IMAGE) > gorush.tar.gz docker_production: - docker build -t $(PRODUCTION_IMAGE) -f docker/Dockerfile.dist . + docker build -t $(EXECUTABLE) -f docker/Dockerfile.dist . docker_deploy: ifeq ($(tag),) @echo "Usage: make $@ tag=" @exit 1 endif - docker tag $(PRODUCTION_IMAGE):latest $(DEPLOY_ACCOUNT)/$(PRODUCTION_IMAGE):$(tag) - docker push $(DEPLOY_ACCOUNT)/$(PRODUCTION_IMAGE):$(tag) + docker tag $(EXECUTABLE):latest $(DEPLOY_ACCOUNT)/$(EXECUTABLE):$(tag) + docker push $(DEPLOY_ACCOUNT)/$(EXECUTABLE):$(tag) docker_test: init clean - docker-compose -p ${PRODUCTION_IMAGE} -f docker/docker-compose.testing.yml run gorush - docker-compose -p ${PRODUCTION_IMAGE} -f docker/docker-compose.testing.yml down - -fmt: - @echo $(TARGETS_NOVENDOR) | xargs go fmt + docker-compose -p ${EXECUTABLE} -f docker/docker-compose.testing.yml run gorush + docker-compose -p ${EXECUTABLE} -f docker/docker-compose.testing.yml down clean: + go clean -x -i ./... + find . -name coverage.txt -delete -rm -rf build.tar.gz \ gorush.tar.gz bin/* \ gorush.tar.gz \