Skip to content

Commit

Permalink
implemented multiarchitecture docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
luisguillenc committed Oct 26, 2020
1 parent 665ff21 commit 3a8abdb
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 11 deletions.
6 changes: 4 additions & 2 deletions Dockerfile.xlget
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
FROM golang:1.14-alpine as build-env
FROM --platform=$BUILDPLATFORM golang:1.14-alpine as build-env

# Arguments for build
ARG arch=amd64
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT

# Install git and certificates
RUN apk update && apk add --no-cache git make ca-certificates && update-ca-certificates
Expand Down
8 changes: 5 additions & 3 deletions Dockerfile.xlistd
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
FROM golang:1.14-alpine as build-env
FROM --platform=$BUILDPLATFORM golang:1.14-alpine as build-env

# Arguments for build
ARG arch=amd64
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT

# Install git and certificates
RUN apk update && apk add --no-cache git make ca-certificates && update-ca-certificates
Expand All @@ -18,7 +20,7 @@ RUN go mod download

## build
COPY . .
RUN make binaries SYSTEM="GOOS=linux GOARCH=${arch}"
RUN make binaries SYSTEM="$(scripts/go-compile-args)"

## create docker
FROM scratch
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ clean:

docker:
@echo "$(WHALE) $@"
docker build -t xlistd -f Dockerfile.xlistd .
docker build -t xlget -f Dockerfile.xlget .
DOCKER_BUILDKIT=1 docker build -t xlistd -f Dockerfile.xlistd .
DOCKER_BUILDKIT=1 docker build -t xlget -f Dockerfile.xlget .

## Targets for Makefile.release
.PHONY: release
Expand Down
44 changes: 40 additions & 4 deletions Makefile.release
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ endif
NAME:=xlist
COMMANDS=xlistd xlistc xlget
VERSION=$(shell git describe --match 'v[0-9]*' --dirty='.m' --always)
LINUX_ARCH:=amd64 arm arm64
LINUX_ARCH:=amd64 arm arm64 ppc64le s390x mips mips64le
FREEBSD_ARCH:=amd64
OPENBSD_ARCH:=amd64
WINDOWS_ARCH:=amd64
DARWIN_ARCH:=amd64
GITHUB_ORG:=luids-io
DOCKER_ORG:=luids
DOCKER_PLATFORMS="linux/amd64,linux/arm64,linux/arm/v6,linux/arm/v7,linux/ppc64le,linux/s390x"

all:
@echo Use the 'release' target to build a release.
@echo "Use the 'release' target to build and 'github-push' or 'docker-push' to publish."

release: build tar installer

Expand Down Expand Up @@ -90,8 +93,41 @@ tar:

.PHONY: installer
installer:
cp deployments/installer/installer.sh release/installer.sh
sed -i 's/RELEASE="RELEASE"/RELEASE="$(VERSION)"/' release/installer.sh
cp deployments/installer/installer_linux.sh release/installer_linux.sh
sed -i 's/RELEASE="RELEASE"/RELEASE="$(VERSION)"/' release/installer_linux.sh

.PHONY: docker-push
docker-push:
@echo Creating docker multiplatform and pushing images
docker buildx build -t $(DOCKER_ORG)/xlistd -t $(DOCKER_ORG)/xlistd:$(VERSION) \
--platform $(DOCKER_PLATFORMS) -f Dockerfile.xlistd --push .
docker buildx build -t $(DOCKER_ORG)/xlget -t $(DOCKER_ORG)/xlget:$(VERSION) \
--platform $(DOCKER_PLATFORMS) -f Dockerfile.xlget --push .

.PHONY: github-push
github-push:
@echo Releasing: $(VERSION)
@$(eval RELEASE:=$(shell curl -s -d '{"tag_name": "v$(VERSION)", "name": "v$(VERSION)"}' -H "Authorization: token ${GITHUB_ACCESS_TOKEN}" "https://api.github.com/repos/$(GITHUB_ORG)/$(NAME)/releases" | grep -m 1 '"id"' | tr -cd '[[:digit:]]'))
@echo ReleaseID: $(RELEASE)
@( cd release; for asset in `ls -A *tgz intaller*`; do \
echo $$asset; \
curl -o /dev/null -X POST \
-H "Content-Type: application/gzip" \
-H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
--data-binary "@$$asset" \
"https://uploads.github.com/repos/$(GITHUB_ORG)/$(NAME)/releases/$(RELEASE)/assets?name=$${asset}" ; \
done )
@( cd release; for asset in `ls -A *tgz installer*`; do \
sha256sum $$asset > $$asset.sha256; \
done )
@( cd release; for asset in `ls -A *sha256`; do \
echo $$asset; \
curl -o /dev/null -X POST \
-H "Content-Type: text/plain" \
-H "Authorization: token ${GITHUB_ACCESS_TOKEN}" \
--data-binary "@$$asset" \
"https://uploads.github.com/repos/$(GITHUB_ORG)/$(NAME)/releases/$(RELEASE)/assets?name=$${asset}" ; \
done )

.PHONY: version
version:
Expand Down
File renamed without changes.
69 changes: 69 additions & 0 deletions scripts/go-compile-args
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env sh

: ${TARGETPLATFORM=}
: ${TARGETOS=}
: ${TARGETARCH=}
: ${TARGETVARIANT=}

GOOS=""
GOARCH=""
GOARM=""

if [ ! -z "$TARGETPLATFORM" ]; then
os="$(echo $TARGETPLATFORM | cut -d"/" -f1)"
arch="$(echo $TARGETPLATFORM | cut -d"/" -f2)"
if [ ! -z "$os" ] && [ ! -z "$arch" ]; then
GOOS="$os"
GOARCH="$arch"
if [ "$arch" == "arm" ]; then
case "$(echo $TARGETPLATFORM | cut -d"/" -f3)" in
"v5")
GOARM="5"
;;
"v6")
GOARM="6"
;;
*)
GOARM="7"
;;
esac
fi
fi
fi

if [ ! -z "$TARGETOS" ]; then
GOOS="$TARGETOS"
fi

if [ ! -z "$TARGETARCH" ]; then
GOARCH="$TARGETARCH"
fi

if [ ! -z "$TARGETVARIANT" ]; then
if [ "$GOARCH" == "arm" ]; then
case "$TARGETVARIANT" in
"v5")
GOARM="5"
;;
"v6")
GOARM="6"
;;
*)
GOARM="7"
;;
esac
fi
fi

OUTPUT=""
if [ ! -z "$GOOS" ]; then
OUTPUT="$OUTPUT GOOS=$GOOS"
fi
if [ ! -z "$GOARCH" ]; then
OUTPUT="$OUTPUT GOARCH=$GOARCH"
fi
if [ ! -z "$GOARM" ]; then
OUTPUT="$OUTPUT GOARM=$GOARM"
fi

echo -n "$OUTPUT"

0 comments on commit 3a8abdb

Please sign in to comment.