Merge pull request #364 from chris-crone/set-version-docker

Set version when building with Docker
This commit is contained in:
Chris Crone 2020-07-08 11:51:00 +02:00 коммит произвёл GitHub
Родитель 33cbee03b8 539464ed3d
Коммит b881f16085
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 19 добавлений и 3 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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)