Proper build, supporting multi-arch

This commit is contained in:
Benoit Perroud 2021-09-13 15:24:52 +02:00
parent c2eb56dd2f
commit 86ff719ba9
3 changed files with 49 additions and 15 deletions

View file

@ -1,11 +1,32 @@
FROM alpine
FROM --platform=$BUILDPLATFORM golang as builder
RUN apk add --no-cache ca-certificates
ARG TARGETOS
ARG TARGETARCH
ARG VERSION
ARG BUILD_DATE
COPY prometheus-docker-labels-discovery /prometheus-docker-labels-discovery
COPY . /src
WORKDIR /src
RUN env GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 go mod download && \
export GIT_COMMIT=$(git rev-parse HEAD) && \
export GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true) && \
env GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 \
go build -o prometheus-docker-labels-discovery \
-ldflags "-X github.com/sqooba/go-common/version.GitCommit=${GIT_COMMIT}${GIT_DIRTY} \
-X github.com/sqooba/go-common/version.BuildDate=${BUILD_DATE} \
-X github.com/sqooba/go-common/version.Version=${VERSION}" \
.
FROM --platform=$BUILDPLATFORM gcr.io/distroless/base
COPY --from=builder /src/prometheus-docker-labels-discovery /prometheus-docker-labels-discovery
# Because of access to docker.sock, it's easier to run it as root...
#USER nobody
ENTRYPOINT ["/prometheus-docker-labels-discovery"]
EXPOSE 8080
#HEALTHCHECK --interval=60s --timeout=10s --retries=1 --start-period=30s CMD ["/prometheus-docker-labels-discovery", "--health-check"]

View file

@ -1,11 +1,13 @@
VERSION=v1.0.1
VERSION=v1.0.2
GOOS=linux
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOLINT=golangci-lint run
BUILD_PLATFORM=linux/amd64
PACKAGE_PLATFORM=$(BUILD_PLATFORM),linux/arm64,linux/arm/v7
VERSION_MAJOR=$(shell echo $(VERSION) | cut -f1 -d.)
VERSION_MINOR=$(shell echo $(VERSION) | cut -f2 -d.)
BINARY_NAME=prometheus-docker-labels-discovery
@ -27,21 +29,32 @@ lint:
build:
GOOS=${GOOS} $(GOBUILD) \
-ldflags "-X version.GitCommit=${GIT_COMMIT}${GIT_DIRTY} \
-X version.BuildDate=${BUILD_DATE} \
-X version.Version=${VERSION}" \
-X github.com/sqooba/go-common/version.BuildDate=${BUILD_DATE} \
-X github.com/sqooba/go-common/version.Version=${VERSION}" \
-o ${BINARY_NAME} .
package:
docker build -f Dockerfile \
-t ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION) \
-t ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION_MAJOR).$(VERSION_MINOR) \
-t ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION_MAJOR) \
.
docker buildx build -f Dockerfile \
--platform $(BUILD_PLATFORM) \
--build-arg VERSION=$(VERSION) \
--build-arg BUILD_DATE=$(BUILD_DATE) \
-t ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION) \
-t ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION_MAJOR).$(VERSION_MINOR) \
-t ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION_MAJOR) \
--load --no-cache \
.
test:
go test ./...
release:
docker push ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION)
docker push ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION_MAJOR).$(VERSION_MINOR)
docker push ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION_MAJOR)
docker buildx build -f Dockerfile \
--platform $(PACKAGE_PLATFORM) \
--build-arg VERSION=$(VERSION) \
--build-arg BUILD_DATE=$(BUILD_DATE) \
-t ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION) \
-t ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION_MAJOR).$(VERSION_MINOR) \
-t ${DOCKER_REGISTRY}${GO_PACKAGE}:$(VERSION_MAJOR) \
--push \
.

View file

@ -15,7 +15,7 @@ type promHandler struct {
func newPromFileHandler(config envConfig) (*promHandler, error) {
if !strings.HasSuffix(config.PrometheusConfigFilePath, ".json") {
return nil, fmt.Errorf("file dose not have .json extension")
return nil, fmt.Errorf("file does not have .json extension")
}
err := syscall.Access(config.PrometheusConfigFilePath, syscall.O_RDWR)