From fd93fc99c90b3eeab00bf5118730d212a1c7d98f Mon Sep 17 00:00:00 2001 From: Garrison Davis Date: Wed, 23 Nov 2022 16:30:18 -0700 Subject: [PATCH] Use SOURCE_DATE_EPOCH to make reproducible builds Reproducible builds are something we should be doing, and we are there as far as making them in CI is concerned with this change. The changes to the Dockerfile/Makefile do nothing if the SOURCE_DATE_EPOCH environment variable is not set before `make build` happens, or if the build arg is not passed in to docker. --- .gitlab/.gitlab-ci.yml | 7 +++++-- Dockerfile | 3 +++ Makefile | 8 +++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.gitlab/.gitlab-ci.yml b/.gitlab/.gitlab-ci.yml index e761d6386..b9b605d07 100644 --- a/.gitlab/.gitlab-ci.yml +++ b/.gitlab/.gitlab-ci.yml @@ -195,6 +195,7 @@ build featurebase: - tar -xvf lattice.tar.gz - go install github.com/rakyll/statik@v0.1.7 - $GOPATH/bin/statik -src=lattice + - export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) - GOOS="linux" GOARCH="amd64" make build FLAGS="-o featurebase_linux_amd64" - GOOS="linux" GOARCH="arm64" make build FLAGS="-o featurebase_linux_arm64" - GOOS="darwin" GOARCH="amd64" make build FLAGS="-o featurebase_darwin_amd64" @@ -218,7 +219,8 @@ build amd container fb: - echo "${DOCKER_DEPLOY_TOKEN}" | docker login -u ${DOCKER_DEPLOY_USER} --password-stdin ${CI_REGISTRY} script: - tag=${CI_REGISTRY_IMAGE}/featurebase:linux-amd64-${CI_COMMIT_REF_SLUG} - - docker build --build-arg GO_VERSION=$GOVERSION --build-arg ARCH=amd64 -t $tag -f .gitlab/Dockerfile . + - export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) + - docker build --build-arg GO_VERSION=$GOVERSION --build-arg ARCH=amd64 --build-arg SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH -t $tag -f .gitlab/Dockerfile . - docker push $tag - echo Created docker featurebase image with tag "$tag" needs: @@ -477,7 +479,8 @@ build arm container fb: - echo "${DOCKER_DEPLOY_TOKEN}" | docker login -u ${DOCKER_DEPLOY_USER} --password-stdin ${CI_REGISTRY} script: - tag=${CI_REGISTRY_IMAGE}/featurebase:linux-arm64-${CI_COMMIT_REF_SLUG} - - docker build --build-arg GO_VERSION=$GOVERSION --build-arg ARCH=arm64 -t $tag -f .gitlab/Dockerfile . + - export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) + - docker build --build-arg GO_VERSION=$GOVERSION --build-arg ARCH=arm64 --build-arg SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH -t $tag -f .gitlab/Dockerfile . - docker push $tag - echo Created docker featurebase image with tag "$tag" diff --git a/Dockerfile b/Dockerfile index 0ca6ef677..cf84b9403 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,8 @@ RUN yarn build FROM golang:${GO_VERSION} as pilosa-builder ARG MAKE_FLAGS +ARG SOURCE_DATE_EPOCH + WORKDIR /pilosa RUN go install github.com/rakyll/statik@v0.1.7 @@ -28,6 +30,7 @@ COPY . ./ COPY --from=lattice-builder /lattice/build /lattice RUN /go/bin/statik -src=/lattice -dest=/pilosa +ENV SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH RUN make build FLAGS="-o build/featurebase" ${MAKE_FLAGS} ##################### diff --git a/Makefile b/Makefile index bdbc36e76..e9576189f 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,12 @@ GO=go GOOS=$(shell $(GO) env GOOS) GOARCH=$(shell $(GO) env GOARCH) VERSION_ID=$(if $(TRIAL_DEADLINE),trial-$(TRIAL_DEADLINE)-,)$(VERSION)-$(GOOS)-$(GOARCH) -BUILD_TIME := $(shell date -u +%FT%T%z) +DATE_FMT="+%FT%T%z" +ifdef SOURCE_DATE_EPOCH + BUILD_TIME ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u "$(DATE_FMT)") +else + BUILD_TIME ?= $(shell date -u "$(DATE_FMT)") +endif SHARD_WIDTH = 20 COMMIT := $(shell git describe --exact-match >/dev/null 2>&1 || git rev-parse --short HEAD) LDFLAGS="-X github.com/molecula/featurebase/v3.Version=$(VERSION) -X github.com/molecula/featurebase/v3.BuildTime=$(BUILD_TIME) -X github.com/molecula/featurebase/v3.Variant=$(VARIANT) -X github.com/molecula/featurebase/v3.Commit=$(COMMIT) -X github.com/molecula/featurebase/v3.TrialDeadline=$(TRIAL_DEADLINE)" @@ -173,6 +178,7 @@ docker-build: vendor docker build \ --build-arg GO_VERSION=$(GO_VERSION) \ --build-arg MAKE_FLAGS="TRIAL_DEADLINE=$(TRIAL_DEADLINE) GOOS=$(GOOS) GOARCH=$(GOARCH)" \ + --build-arg SOURCE_DATE_EPOCH=$(SOURCE_DATE_EPOCH) \ --target pilosa-builder \ --tag featurebase:build . docker create --name featurebase-build featurebase:build