diff --git a/Makefile b/Makefile index 14e30386f..6500f3072 100644 --- a/Makefile +++ b/Makefile @@ -23,10 +23,22 @@ UID := $(shell id -u) # Commit hash from git COMMIT=$(shell git rev-parse HEAD) +GITTAG_COMMIT := $(shell git rev-list --tags --max-count=1) +GITTAG := $(shell git describe --abbrev=0 --tags ${GITTAG_COMMIT} 2>/dev/null || true) # Branch from git BRANCH=$(shell git rev-parse --abbrev-ref HEAD) +TAG := $(GITTAG:v%=%) +ifneq ($(COMMIT), $(GITTAG_COMMIT)) + TAG := $(COMMIT) +endif + +ifneq ($(shell git status --porcelain),) + TAG := $(TAG)-dirty +endif + + PROJECT_PACKAGE := github.com/spotahome/redis-operator CODEGEN_IMAGE := quay.io/slok/kube-code-generator:v1.22.0 PORT := 9710 @@ -86,6 +98,18 @@ image: deps-development -f $(APP_DIR)/Dockerfile \ . +.PHONY: image-release +image-release: + docker buildx build \ + --platform linux/amd64,linux/arm64,linux/arm/v7 \ + --push \ + --build-arg VERSION=$(TAG) \ + -t $(REPOSITORY):latest \ + -t $(REPOSITORY):$(COMMIT) \ + -t $(REPOSITORY):$(TAG) \ + -f $(APP_DIR)/Dockerfile \ + . + .PHONY: testing testing: image docker push $(REPOSITORY):$(BRANCH) @@ -102,7 +126,7 @@ publish: docker push $(REPOSITORY):latest .PHONY: release -release: tag image publish +release: tag image-release # Test stuff in dev .PHONY: unit-test diff --git a/docker/app/Dockerfile b/docker/app/Dockerfile index 5774f6faf..5794bfb34 100644 --- a/docker/app/Dockerfile +++ b/docker/app/Dockerfile @@ -1,16 +1,20 @@ -FROM golang:1.17-alpine +FROM --platform=$BUILDPLATFORM golang:1.17-alpine AS build +RUN apk --no-cache add \ + bash -WORKDIR /go/src/github.com/spotahome/redis-operator +WORKDIR /src COPY . . -RUN ./scripts/build.sh + +ARG TARGETOS TARGETARCH VERSION +RUN GOOS=$TARGETOS GOARCH=$TARGETARCH VERSION=$VERSION ./scripts/build.sh FROM alpine:latest RUN apk --no-cache add \ ca-certificates -COPY --from=0 /go/src/github.com/spotahome/redis-operator/bin/linux/redis-operator /usr/local/bin +COPY --from=build /src/bin/redis-operator /usr/local/bin RUN addgroup -g 1000 rf && \ - adduser -D -u 1000 -G rf rf && \ - chown rf:rf /usr/local/bin/redis-operator + adduser -D -u 1000 -G rf rf && \ + chown rf:rf /usr/local/bin/redis-operator USER rf ENTRYPOINT ["/usr/local/bin/redis-operator"] diff --git a/scripts/build.sh b/scripts/build.sh index e6b3ede75..71e930ad2 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,7 +1,24 @@ -#!/bin/sh +#!/usr/bin/env bash -export CGO_ENABLED=0 -export GOOS=linux -export GO111MODULE=on +set -o errexit +set -o nounset -go build -ldflags '-extldflags "-static"' -a -v -o bin/linux/redis-operator ./cmd/redisoperator/ +src=./cmd/redisoperator +out=./bin/redis-operator + +if [[ ! -z ${TARGETOS} ]] && [[ ! -z ${TARGETARCH} ]]; +then + echo "Building ${TARGETOS}/${TARGETARCH} release..." + export GOOS=${TARGETOS} + export GOARCH=${TARGETARCH} + binary_ext=-${TARGETOS}-${TARGETARCH} +else + echo "Building native release..." +fi + +final_out=${out}${binary_ext} +ldf_cmp="-w -extldflags '-static'" +f_ver="-X main.Version=${VERSION:-dev}" + +echo "Building binary at ${out}" +CGO_ENABLED=0 go build -o ${out} --ldflags "${ldf_cmp} ${f_ver}" ${src}