Skip to content

Commit

Permalink
Merge pull request #352 from spotahome/build
Browse files Browse the repository at this point in the history
Add cross architecture docker builds
  • Loading branch information
ese authored Jan 17, 2022
2 parents 2c2ed0a + d888c1e commit e047573
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
26 changes: 25 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
16 changes: 10 additions & 6 deletions docker/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
27 changes: 22 additions & 5 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit e047573

Please sign in to comment.