Skip to content

Commit

Permalink
Refactoring build process
Browse files Browse the repository at this point in the history
  • Loading branch information
aerostitch committed Oct 29, 2018
1 parent 10bb538 commit bc0a70a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 36 deletions.
28 changes: 15 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
70 changes: 47 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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); \
Expand All @@ -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

0 comments on commit bc0a70a

Please sign in to comment.