Browse Source

build: Set version when building with Docker

Signed-off-by: Christopher Crone <[email protected]>
Christopher Crone 5 years ago
parent
commit
539464ed3d
3 changed files with 19 additions and 3 deletions
  1. 9 1
      Dockerfile
  2. 8 1
      Makefile
  3. 2 1
      builder.Makefile

+ 9 - 1
Dockerfile

@@ -36,9 +36,11 @@ FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION} AS lint-base
 FROM base AS lint
 ENV CGO_ENABLED=0
 COPY --from=lint-base /usr/bin/golangci-lint /usr/bin/golangci-lint
+ARG GIT_TAG
 RUN --mount=target=. \
     --mount=type=cache,target=/root/.cache/go-build \
     --mount=type=cache,target=/root/.cache/golangci-lint \
+    GIT_TAG=${GIT_TAG} \
     make -f builder.Makefile lint
 
 FROM base AS make-cli
@@ -46,18 +48,22 @@ ENV CGO_ENABLED=0
 ARG TARGETOS
 ARG TARGETARCH
 ARG BUILD_TAGS
+ARG GIT_TAG
 RUN --mount=target=. \
     --mount=type=cache,target=/root/.cache/go-build \
     GOOS=${TARGETOS} \
     GOARCH=${TARGETARCH} \
     BUILD_TAGS=${BUILD_TAGS} \
+    GIT_TAG=${GIT_TAG} \
     make BINARY=/out/docker -f builder.Makefile cli
 
 FROM base AS make-cross
 ARG BUILD_TAGS
+ARG GIT_TAG
 RUN --mount=target=. \
     --mount=type=cache,target=/root/.cache/go-build \
     BUILD_TAGS=${BUILD_TAGS} \
+    GIT_TAG=${GIT_TAG} \
     make BINARY=/out/docker  -f builder.Makefile cross
 
 FROM scratch AS protos
@@ -70,9 +76,11 @@ FROM scratch AS cross
 COPY --from=make-cross /out/* .
 
 FROM base as test
-ARG BUILD_TAGS
 ENV CGO_ENABLED=0
+ARG BUILD_TAGS
+ARG GIT_TAG
 RUN --mount=target=. \
     --mount=type=cache,target=/root/.cache/go-build \
     BUILD_TAGS=${BUILD_TAGS} \
+    GIT_TAG=${GIT_TAG} \
     make -f builder.Makefile test

+ 8 - 1
Makefile

@@ -22,6 +22,8 @@ ifeq ($(UNAME_S),Darwin)
 	MOBY_DOCKER=/Applications/Docker.app/Contents/Resources/bin/docker
 endif
 
+GIT_TAG?=$(shell git describe --tags --match "v[0-9]*")
+
 all: cli
 
 protos: ## Generate go code from .proto files
@@ -32,6 +34,7 @@ cli: ## Compile the cli
 	@docker build . --target cli \
 	--platform local \
 	--build-arg BUILD_TAGS=example,local \
+	--build-arg GIT_TAG=$(GIT_TAG) \
 	--output ./bin
 
 e2e-local: ## Run End to end local tests
@@ -46,18 +49,22 @@ e2e-aci: ## Run End to end ACI tests (requires azure login)
 cross: ## Compile the CLI for linux, darwin and windows
 	@docker build . --target cross \
 	--build-arg BUILD_TAGS \
+	--build-arg GIT_TAG=$(GIT_TAG) \
 	--output ./bin \
 
 test: ## Run unit tests
 	@docker build . \
 	--build-arg BUILD_TAGS=example,local \
+	--build-arg GIT_TAG=$(GIT_TAG) \
 	--target test
 
 cache-clear: ## Clear the builder cache
 	@docker builder prune --force --filter type=exec.cachemount --filter=unused-for=24h
 
 lint: ## run linter(s)
-	@docker build . --target lint
+	@docker build . \
+	--build-arg GIT_TAG=$(GIT_TAG) \
+	--target lint
 
 serve: cli ## start server
 	@./bin/docker serve --address unix:///tmp/backend.sock

+ 2 - 1
builder.Makefile

@@ -23,7 +23,8 @@ ifeq ($(GOOS),windows)
 endif
 
 STATIC_FLAGS=CGO_ENABLED=0
-GIT_TAG=$(shell git describe --tags --match "v[0-9]*")
+
+GIT_TAG?=$(shell git describe --tags --match "v[0-9]*")
 
 LDFLAGS="-s -w -X main.version=${GIT_TAG}"
 GO_BUILD=$(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS)