Build multi-architecture Docker images.

This commit is contained in:
David Newhall II 2019-07-06 12:53:07 -07:00
parent e310ada1fd
commit 9962f30e73
6 changed files with 87 additions and 26 deletions

View File

@ -36,7 +36,7 @@ VERSION_PATH:=github.com/$(GHUSER)/$(BINARY)/$(shell echo $(BINARY) | tr -d -- -
all: man build
# Prepare a release. Called in Travis CI.
release: clean vendor test macos arm windows linux_packages
release: clean vendor test macos arm arm64 windows linux_packages
# Prepareing a release!
mkdir -p $@
mv $(BINARY).*.linux $(BINARY).*.macos $@/
@ -89,10 +89,12 @@ $(BINARY).i386.linux:
# Building linux binary.
GOOS=linux GOARCH=386 go build -o $@ -ldflags "-w -s -X $(VERSION_PATH)"
arm: $(BINARY).arm.linux
$(BINARY).arm.linux:
arm: arm64 armhf
arm64: $(BINARY).arm64.linux
$(BINARY).arm64.linux:
# Building linux binary.
GOOS=linux GOARCH=arm GOARM=5 go build -o $@ -ldflags "-w -s -X $(VERSION_PATH)"
GOOS=linux GOARCH=arm64 go build -o $@ -ldflags "-w -s -X $(VERSION_PATH)"
armhf: $(BINARY).armhf.linux
$(BINARY).armhf.linux:
@ -186,11 +188,11 @@ $(BINARY)_$(VERSION)-$(ITERATION)_i386.deb: package_build_linux_386 check_fpm
--config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \
--chdir $<
rpmarm: $(BINARY)-$(RPMVERSION)-$(ITERATION).arm.rpm
$(BINARY)-$(RPMVERSION)-$(ITERATION).arm.rpm: package_build_linux_arm check_fpm
@echo "Building 32-bit ARM 'rpm' package for $(BINARY) version '$(RPMVERSION)-$(ITERATION)'."
rpmarm: $(BINARY)-$(RPMVERSION)-$(ITERATION).arm64.rpm
$(BINARY)-$(RPMVERSION)-$(ITERATION).arm64.rpm: package_build_linux_arm64 check_fpm
@echo "Building 64-bit ARM8 'rpm' package for $(BINARY) version '$(RPMVERSION)-$(ITERATION)'."
fpm -s dir -t rpm \
--architecture arm \
--architecture arm64 \
--name $(BINARY) \
--rpm-os linux \
--version $(RPMVERSION) \
@ -204,11 +206,11 @@ $(BINARY)-$(RPMVERSION)-$(ITERATION).arm.rpm: package_build_linux_arm check_fpm
--config-files "/etc/$(BINARY)/$(CONFIG_FILE)" \
--chdir $<
debarm: $(BINARY)_$(VERSION)-$(ITERATION)_arm.deb
$(BINARY)_$(VERSION)-$(ITERATION)_arm.deb: package_build_linux_arm check_fpm
@echo "Building 32-bit ARM 'deb' package for $(BINARY) version '$(VERSION)-$(ITERATION)'."
debarm: $(BINARY)_$(VERSION)-$(ITERATION)_arm64.deb
$(BINARY)_$(VERSION)-$(ITERATION)_arm64.deb: package_build_linux_arm64 check_fpm
@echo "Building 64-bit ARM8 'deb' package for $(BINARY) version '$(VERSION)-$(ITERATION)'."
fpm -s dir -t deb \
--architecture arm \
--architecture arm64 \
--name $(BINARY) \
--deb-no-default-config-files \
--version $(VERSION) \
@ -224,7 +226,7 @@ $(BINARY)_$(VERSION)-$(ITERATION)_arm.deb: package_build_linux_arm check_fpm
rpmarmhf: $(BINARY)-$(RPMVERSION)-$(ITERATION).armhf.rpm
$(BINARY)-$(RPMVERSION)-$(ITERATION).armhf.rpm: package_build_linux_armhf check_fpm
@echo "Building 32-bit ARM HF 'rpm' package for $(BINARY) version '$(RPMVERSION)-$(ITERATION)'."
@echo "Building 32-bit ARM6/7 HF 'rpm' package for $(BINARY) version '$(RPMVERSION)-$(ITERATION)'."
fpm -s dir -t rpm \
--architecture armhf \
--name $(BINARY) \
@ -242,7 +244,7 @@ $(BINARY)-$(RPMVERSION)-$(ITERATION).armhf.rpm: package_build_linux_armhf check_
debarmhf: $(BINARY)_$(VERSION)-$(ITERATION)_armhf.deb
$(BINARY)_$(VERSION)-$(ITERATION)_armhf.deb: package_build_linux_armhf check_fpm
@echo "Building 32-bit ARM HF 'deb' package for $(BINARY) version '$(VERSION)-$(ITERATION)'."
@echo "Building 32-bit ARM6/7 HF 'deb' package for $(BINARY) version '$(VERSION)-$(ITERATION)'."
fpm -s dir -t deb \
--architecture armhf \
--name $(BINARY) \
@ -280,10 +282,10 @@ package_build_linux_386: package_build_linux linux386
cp -r $</* $@/
cp $(BINARY).i386.linux $@/usr/bin/$(BINARY)
package_build_linux_arm: package_build_linux arm
package_build_linux_arm64: package_build_linux arm64
mkdir -p $@
cp -r $</* $@/
cp $(BINARY).arm.linux $@/usr/bin/$(BINARY)
cp $(BINARY).arm64.linux $@/usr/bin/$(BINARY)
package_build_linux_armhf: package_build_linux armhf
mkdir -p $@

View File

@ -1,20 +1,21 @@
#
# building static go binary with golang container
# building static go binary with Debian golang container
#
ARG ARCH=amd64
FROM golang:stretch as builder
ARG ARCH
RUN mkdir -p $GOPATH/pkg/mod $GOPATH/bin
RUN mkdir -p $GOPATH/pkg/mod $GOPATH/bin $GOPATH/src/github.com/davidnewhall
RUN apt-get update \
&& apt-get install -y curl \
&& curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh \
&& mkdir -p $GOPATH/src/github.com/davidnewhall
&& curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
COPY . $GOPATH/src/github.com/davidnewhall/unifi-poller
WORKDIR $GOPATH/src/github.com/davidnewhall/unifi-poller
RUN dep ensure \
&& CGO_ENABLED=0 make linux
RUN dep ensure --vendor-only \
&& CGO_ENABLED=0 make unifi-poller.${ARCH}.linux
#
# creating container for run
@ -25,10 +26,10 @@ RUN dep ensure \
# by using "-e UNIFI_PASSWORD=your-secret-pasword" you can avoid this configuration in the config file
#
FROM scratch
ARG ARCH
COPY --from=builder /go/src/github.com/davidnewhall/unifi-poller/unifi-poller.amd64.linux /unifi-poller
COPY --from=builder /go/src/github.com/davidnewhall/unifi-poller/unifi-poller.${ARCH}.linux /unifi-poller
COPY --from=builder /go/src/github.com/davidnewhall/unifi-poller/examples/up.conf.example /etc/unifi-poller/up.conf
VOLUME [ "/etc/unifi-poller"]
VOLUME [ "/etc/unifi-poller" ]
ENTRYPOINT [ "/unifi-poller" ]

View File

@ -0,0 +1,3 @@
# Docker Build Hooks
The files in this folder are used by Docker Cloud to automate image builds.

11
init/docker/hooks/build Executable file
View File

@ -0,0 +1,11 @@
#!/bin/bash
# This always run local to the Dockerfile folder.
set -e -o pipefail
for build in $BUILDS; do
os=$(echo $build | cut -d: -f1)
name=$(echo $build | cut -d: -f2)
echo "Building Image ${IMAGE_NAME}_${os}_${name}"
docker build --build-arg "ARCH=${name}" --tag "${IMAGE_NAME}_${os}_${name}" --file Dockerfile ../..
done

7
init/docker/hooks/pre_build Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
# https://www.smockle.com/blog/2019/04/22/migrating-from-travis-ci-to-docker-hub-automated-builds/
apt-get -y update
apt-get -y --only-upgrade install docker-ee
docker run --rm --privileged multiarch/qemu-user-static:register --reset

37
init/docker/hooks/push Executable file
View File

@ -0,0 +1,37 @@
#!/bin/bash
# This post build hook creates multi-architecture docker manifests.
# It's all a bit complicated for some reason.
set -e -o pipefail
VERSION=$(git tag -l --merged | tail -n1 | tr -d v)
SHORTVER=$(echo $VERSION | cut -d. -f1,2)
if [ "$BUILDS" != "" ] && [ "$VERSION" != "" ]; then
TAGS=latest
fi
# Push the extra custom images that were created.
for build in $BUILDS; do
os=$(echo $build | cut -d: -f1)
name=$(echo $build | cut -d: -f2)
echo "Pushing Image ${IMAGE_NAME}_${os}_${name}"
docker push ${IMAGE_NAME}_${os}_${name}
IMAGES="${IMAGES} ${IMAGE_NAME}_${os}_${name}"
done
echo "Annotating Images: ${IMAGES}"
[ "$VERSION" != "$SOURCE_BRANCH" ] || TAGS="latest $VERSION $SHORTVER stable"
echo "Version: $VERSION, Source: $SOURCE_BRANCH, Building tags: ${TAGS}"
for tag in $TAGS; do
docker manifest create --amend ${DOCKER_REPO}:${tag} $IMAGES
for build in $BUILDS; do
os=$(echo $build | cut -d: -f1)
name=$(echo $build | cut -d: -f2)
arch=$(echo $build | cut -d: -f3)
vari=$(echo $build | cut -d: -f4)
docker manifest annotate ${DOCKER_REPO}:${tag} ${IMAGE_NAME}_${os}_${name} --os ${os} --arch ${arch} --variant "${vari}"
done
echo "Pushing Manifest ${DOCKER_REPO}:${tag}"
docker manifest push ${DOCKER_REPO}:${tag}
done