Skip to content

Commit

Permalink
WIP: Improve Makefile and Dockerfile
Browse files Browse the repository at this point in the history
Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr>
  • Loading branch information
sylr committed Feb 21, 2024
1 parent 94f69a0 commit cd9b378
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
/dist
/litestream
21 changes: 16 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
FROM golang:1.21.3 as builder
FROM --platform=${TARGETPLATFORM} golang:1.21.7-alpine3.19 as builder

WORKDIR /src/litestream

# Download dependencies first to cache them.
COPY go.mod go.sum ./
RUN go mod download

RUN apk add --no-cache make git gcc musl-dev

COPY . .

ARG LITESTREAM_VERSION=latest
ARG GIT_VERSION=latest
ARG GO_BUILD_EXTLDFLAGS=-static

RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
go build -ldflags "-s -w -X 'main.Version=${LITESTREAM_VERSION}' -extldflags '-static'" -tags osusergo,netgo,sqlite_omit_load_extension -o /usr/local/bin/litestream ./cmd/litestream
make build GIT_VERSION=${GIT_VERSION} GO_BUILD_EXTLDFLAGS=${GO_BUILD_EXTLDFLAGS}

# ------------------------------------------------------------------------------

FROM --platform=${TARGETPLATFORM} alpine:3.19.1

COPY --from=builder /src/litestream/litestream /usr/local/bin/litestream

FROM alpine:3.17.2
COPY --from=builder /usr/local/bin/litestream /usr/local/bin/litestream
ENTRYPOINT ["/usr/local/bin/litestream"]
CMD []
59 changes: 49 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@

GIT_UPDATE_INDEX := $(shell git update-index --refresh > /dev/null 2>&1)
GIT_REVISION ?= $(shell git rev-parse HEAD)
GIT_VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)

GO ?= go
GO_BUILD_TAGS ?= osusergo,netgo,sqlite_omit_load_extension
GO_BUILD_EXTLDFLAGS ?=
GO_BUILD_LDFLAGS ?= "-s -w -X 'main.Version=$(GIT_VERSION)' -extldflags '$(GO_BUILD_EXTLDFLAGS)'"

DOCKER_IMAGE_NAME ?= litestream/litestream
DOCKER_IMAGE_TAG ?= $(shell echo $(GIT_VERSION) | tr '+' '_')
DOCKER_BUILD_LABELS = --label org.opencontainers.image.title=hermes
DOCKER_BUILD_LABELS += --label org.opencontainers.image.description="Fully-replicated database with no pain and little cost."
DOCKER_BUILD_LABELS += --label org.opencontainers.image.url="https://litestream.io/"
DOCKER_BUILD_LABELS += --label org.opencontainers.image.source="https://github.com/benbjohnson/litestream"
DOCKER_BUILD_LABELS += --label org.opencontainers.image.revision=$(GIT_REVISION)
DOCKER_BUILD_LABELS += --label org.opencontainers.image.version=$(GIT_VERSION)
DOCKER_BUILD_LABELS += --label org.opencontainers.image.created=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
DOCKER_BUILD_ARGS = --build-arg GIT_VERSION=$(GIT_VERSION)
DOCKER_BUILD_ARGS += --build-arg GO_BUILD_EXTLDFLAGS="-static"
DOCKER_BUILD_PLATFORMS ?= linux/amd64,linux/arm,linux/arm64

default:

docker:
docker build -t litestream .
build:
CGO_ENABLED=1 $(GO) build -ldflags $(GO_BUILD_LDFLAGS) -tags $(GO_BUILD_TAGS) ./cmd/litestream

docker-build:
docker buildx build $(DOCKER_BUILD_ARGS) \
--platform=$(DOCKER_BUILD_PLATFORMS) \
$(DOCKER_BUILD_LABELS) \
-t $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) \
--load \
.

docker-push:
docker buildx build $(DOCKER_BUILD_ARGS) \
--platform=$(DOCKER_BUILD_PLATFORMS) \
$(DOCKER_BUILD_LABELS) \
-t $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) \
--push \
.

dist-linux:
mkdir -p dist
Expand All @@ -16,20 +55,20 @@ dist-linux-arm64:
docker run --rm -v "${PWD}":/usr/src/litestream -w /usr/src/litestream -e CGO_ENABLED=1 -e CC=aarch64-linux-gnu-gcc -e GOOS=linux -e GOARCH=arm64 golang-xc:1.16 go build -v -o dist/litestream-linux-arm64 ./cmd/litestream

dist-macos:
ifndef LITESTREAM_VERSION
$(error LITESTREAM_VERSION is undefined)
ifndef GIT_VERSION
$(error GIT_VERSION is undefined)
endif
mkdir -p dist

GOOS=darwin GOARCH=amd64 CC="gcc -target amd64-apple-macos11" CGO_ENABLED=1 go build -v -ldflags "-s -w -X 'main.Version=${LITESTREAM_VERSION}'" -o dist/litestream ./cmd/litestream
GOOS=darwin GOARCH=amd64 CC="gcc -target amd64-apple-macos11" CGO_ENABLED=1 go build -v -ldflags "-s -w -X 'main.Version=$(GIT_VERSION)'" -o dist/litestream ./cmd/litestream
gon etc/gon.hcl
mv dist/litestream.zip dist/litestream-${LITESTREAM_VERSION}-darwin-amd64.zip
openssl dgst -sha256 dist/litestream-${LITESTREAM_VERSION}-darwin-amd64.zip
mv dist/litestream.zip dist/litestream-$(GIT_VERSION)-darwin-amd64.zip
openssl dgst -sha256 dist/litestream-$(GIT_VERSION)-darwin-amd64.zip

GOOS=darwin GOARCH=arm64 CC="gcc -target arm64-apple-macos11" CGO_ENABLED=1 go build -v -ldflags "-s -w -X 'main.Version=${LITESTREAM_VERSION}'" -o dist/litestream ./cmd/litestream
GOOS=darwin GOARCH=arm64 CC="gcc -target arm64-apple-macos11" CGO_ENABLED=1 go build -v -ldflags "-s -w -X 'main.Version=$(GIT_VERSION)'" -o dist/litestream ./cmd/litestream
gon etc/gon.hcl
mv dist/litestream.zip dist/litestream-${LITESTREAM_VERSION}-darwin-arm64.zip
openssl dgst -sha256 dist/litestream-${LITESTREAM_VERSION}-darwin-arm64.zip
mv dist/litestream.zip dist/litestream-$(GIT_VERSION)-darwin-arm64.zip
openssl dgst -sha256 dist/litestream-$(GIT_VERSION)-darwin-arm64.zip

clean:
rm -rf dist
Expand Down

0 comments on commit cd9b378

Please sign in to comment.