diff --git a/Dockerfile b/Dockerfile index b0fa380..94d4989 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,17 @@ -FROM golang:alpine - -RUN apk update \ - && apk add --no-cache git \ - && go get github.com/Masterminds/glide \ - && go install github.com/Masterminds/glide - +FROM golang:alpine as base +# the base image is only used for compilation +ARG BUILD_VERSION=0.0.1-test +ENV CGO_ENABLED=0 +ENV GOOS=linux +ENV GOARCH=amd64 WORKDIR /go/src/github.com/VEVO/awsRetagger -COPY . ./ - -RUN rm -f glide.lock \ - && glide install --strip-vendor -RUN go-wrapper install +RUN apk add --no-cache git build-base +COPY . . +RUN make go-build -CMD ["go-wrapper", "run"] +# Getting a small image with only the binary +FROM scratch +COPY --from=base /go/src/github.com/VEVO/awsRetagger/awsRetagger /awsRetagger +# This is needed when you do HTTPS requests +COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +ENTRYPOINT ["/awsRetagger"] diff --git a/Makefile b/Makefile index fbc3bf5..0c89d20 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,52 @@ -GOFILES_NOVENDOR=$(shell find . -type f -name '*.go' -not -path "./vendor/*") +# setting some defaults if those variables are empty +OWNER=vevo +APP_NAME=awsRetagger +IMAGE_NAME=$(OWNER)/$(APP_NAME) +GO_REVISION?=$(shell git rev-parse HEAD) +GO_TO_REVISION?=$(GO_REVISION) +GO_FROM_REVISION?=$(shell git rev-parse refs/remotes/origin/master) +GIT_TAG=$(IMAGE_NAME):$(GO_REVISION) +BUILD_VERSION?=1.0.$(shell date +%Y%m%d) +BUILD_TAG=$(IMAGE_NAME):$(BUILD_VERSION) +LATEST_TAG=$(IMAGE_NAME):latest + +docker-lint: + docker run -it --rm -v "${PWD}/Dockerfile":/Dockerfile:ro redcoolbeans/dockerlint + +docker-login: + @docker login -u "$(DOCKER_USER)" -p "$(DOCKER_PASS)" + +docker-build: + docker build --build-arg BUILD_VERSION=$(BUILD_VERSION) -t $(GIT_TAG) . -default: dep lint test +docker-tag: + docker tag $(GIT_TAG) $(BUILD_TAG) + docker tag $(GIT_TAG) $(LATEST_TAG) -dep: +docker-push: docker-login + docker push $(GIT_TAG) + docker push $(BUILD_TAG) + docker push $(LATEST_TAG) + +go-dep: @if [ -f "glide.yaml" ] ; then \ go get github.com/Masterminds/glide \ && go install github.com/Masterminds/glide \ && glide install --strip-vendor; \ + elif [ -f "Godeps/Godeps.json" ] ; then \ + go get github.com/tools/godep \ + && godep restore; \ else \ - go get -v ./...; \ + go get -d -t -v ./...; \ fi -fmt: +GOFILES_NOVENDOR=$(shell find . -type f -name '*.go' -not -path "./vendor/*") + +go-fmt: @[ $$(gofmt -l $(GOFILES_NOVENDOR) | wc -l) -gt 0 ] && echo "Code differs from gofmt's style" && exit 1 || true -lint: fmt - @go get github.com/golang/lint/golint; \ +go-lint: go-fmt + @go get -u golang.org/x/lint/golint; \ if [ -f "glide.yaml" ] ; then \ golint -set_exit_status $$(glide novendor); \ go vet -v $$(glide novendor); \ @@ -24,26 +55,19 @@ lint: fmt go vet -v ./...; \ fi -gocov: - @go get github.com/axw/gocov/gocov \ - && go install github.com/axw/gocov/gocov; \ - if [ -f "glide.yaml" ] ; then \ - gocov test $$(glide novendor) | gocov report; \ - else \ - gocov test ./... | gocov report; \ - fi - # gocov test $$(glide novendor) >/tmp/gocovtest.json ; gocov annotate /tmp/gocovtest.json MyFunc - -test: +go-test: @if [ -f "glide.yaml" ] ; then \ go test $$(glide novendor); \ else \ go test -v ./...; \ fi -build: dep lint test - go clean -v - go build -v +go-build: go-dep go-lint go-test + @go build -v -a -ldflags "-X main.version=$(BUILD_VERSION)" + +go-compile: + @docker run --rm -v "$${PWD}":/go/src/github.com/VEVO/$(APP_NAME) -w /go/src/github.com/VEVO/$(APP_NAME) -e GOARCH=amd64 -e GOOS=linux -e CGO_ENABLED=0 -e BUILD_VERSION=$(BUILD_VERSION) golang:alpine make go-build + +build: docker-lint docker-build docker-tag docker-push -install: dep - go install +# vim: ft=make