Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #201 from cloudflare/3-stage-build
Browse files Browse the repository at this point in the history
3 stage build
  • Loading branch information
prymitive committed Dec 5, 2017
2 parents c6ee74a + e11dde8 commit d9b8093
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
cache:
directories:
- node_modules
# install defaults to "npm install", which is done via make
install: []
script: make test-js

- stage: Lint Go code
Expand All @@ -33,6 +35,8 @@ jobs:
cache:
directories:
- node_modules
# install defaults to "npm install", which is done via make
install: []
script: make lint-js

- stage: Build Docker image
Expand Down
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
FROM golang:1.9.2-alpine3.6 as unsee-builder
COPY . /go/src/github.com/cloudflare/unsee
FROM node:8-alpine as nodejs-builder
RUN apk add --update make git
COPY . /unsee
RUN make -C /unsee webpack

FROM golang:1.9.2-alpine3.6 as go-builder
COPY --from=nodejs-builder /unsee /go/src/github.com/cloudflare/unsee
ARG VERSION
RUN apk add --update make git nodejs nodejs-npm
RUN apk add --update make git
RUN CGO_ENABLED=0 make -C /go/src/github.com/cloudflare/unsee VERSION="${VERSION:-dev}" unsee

FROM gcr.io/distroless/base
COPY --from=unsee-builder /go/src/github.com/cloudflare/unsee/unsee /unsee
COPY --from=go-builder /go/src/github.com/cloudflare/unsee/unsee /unsee
EXPOSE 8080
CMD ["/unsee"]
69 changes: 32 additions & 37 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,60 @@ endif

.DEFAULT_GOAL := $(NAME)

.build/go-bindata:
.build/deps-build-go.ok:
@mkdir -p .build
go get -u github.com/golang/dep/cmd/dep
go get -u github.com/jteeuwen/go-bindata/...
touch $@

.build/go-bindata-assetfs:
@mkdir -p .build
go get -u github.com/elazarl/go-bindata-assetfs/...
touch $@

.build/golint:
.build/deps-lint-go.ok:
@mkdir -p .build
go get -u github.com/golang/lint/golint
touch $@

.build/npm.install: package.json package-lock.json
.build/deps-build-node.ok: package.json package-lock.json
@mkdir -p .build
npm install
touch $@

.build/deps.ok: .build/go-bindata .build/go-bindata-assetfs .build/golint .build/npm.install
.build/artifacts-bindata_assetfs.%:
@mkdir -p .build
rm -f .build/artifacts-bindata_assetfs.*
touch $@

.build/bindata_assetfs.%:
.build/artifacts-webpack.ok: .build/deps-build-node.ok $(ASSET_SOURCES) webpack.config.js
@mkdir -p .build
rm -f .build/bindata_assetfs.*
$(CURDIR)/node_modules/.bin/webpack
touch $@

bindata_assetfs.go: .build/deps.ok .build/bindata_assetfs.$(GO_BINDATA_MODE) $(ASSET_SOURCES) webpack.config.js .build/vendor.ok
$(CURDIR)/node_modules/.bin/webpack
bindata_assetfs.go: .build/deps-build-go.ok .build/artifacts-bindata_assetfs.$(GO_BINDATA_MODE) .build/vendor.ok .build/artifacts-webpack.ok
go-bindata-assetfs $(GO_BINDATA_FLAGS) -prefix assets -nometadata assets/templates/... assets/static/dist/...

$(NAME): .build/deps.ok .build/vendor.ok bindata_assetfs.go $(SOURCES)
$(NAME): .build/deps-build-go.ok .build/vendor.ok bindata_assetfs.go $(SOURCES)
go build -ldflags "-X main.version=$(VERSION)"

.build/vendor.ok: .build/deps-build-go.ok Gopkg.lock Gopkg.toml
dep ensure
dep prune
touch $@

.PHONY: vendor
vendor: .build/deps-build-go.ok
dep ensure
dep prune

.PHONY: vendor-update
vendor-update: .build/deps-build-go.ok
dep ensure -update
dep prune

.PHONY: webpack
webpack: .build/artifacts-webpack.ok

.PHONY: clean
clean:
rm -fr .build $(NAME)
rm -fr .build bindata_assetfs.go $(NAME)

.PHONY: run
run: $(NAME)
Expand Down Expand Up @@ -91,19 +106,19 @@ run-docker: docker-image
$(NAME):$(VERSION)

.PHONY: lint-go
lint-go: .build/golint
lint-go: .build/deps-lint-go.ok
golint ./... | (egrep -v "^vendor/|^bindata_assetfs.go" || true)

.PHONY: lint-js
lint-js: .build/npm.install
lint-js: .build/deps-build-node.ok
$(CURDIR)/node_modules/.bin/eslint --quiet assets/static/*.js

.PHONY: lint
lint: lint-go lint-js

# Creates mock bindata_assetfs.go with source assets rather than webpack generated ones
.PHONY: mock-assets
mock-assets: .build/go-bindata .build/go-bindata-assetfs
mock-assets: .build/deps-build-go.ok
mkdir -p $(CURDIR)/assets/static/dist/templates
cp $(CURDIR)/assets/static/*.* $(CURDIR)/assets/static/dist/
touch $(CURDIR)/assets/static/dist/templates/loader_unsee.html
Expand All @@ -123,23 +138,3 @@ test-js:

.PHONY: test
test: lint test-go test-js

.build/dep.ok:
go get -u github.com/golang/dep/cmd/dep
@mkdir -p .build
touch $@

.build/vendor.ok: Gopkg.lock Gopkg.toml .build/dep.ok
dep ensure
dep prune
touch $@

.PHONY: vendor
vendor: .build/dep.ok
dep ensure
dep prune

.PHONY: vendor-update
vendor-update: .build/dep.ok
dep ensure -update
dep prune

0 comments on commit d9b8093

Please sign in to comment.