diff --git a/.gitignore b/.gitignore index 53ba083f..7581b7d3 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,7 @@ /unifi-poller.*.macos /unifi-poller.*.linux /unifi-poller.rb -*.sha256 +checksums.sha256.txt /vendor .DS_Store *~ diff --git a/Makefile b/Makefile index 92ea085c..5b7dad95 100644 --- a/Makefile +++ b/Makefile @@ -36,10 +36,10 @@ 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 arm64 windows linux_packages +release: clean vendor test macos arm windows linux_packages # Prepareing a release! mkdir -p $@ - mv $(BINARY).*.linux $(BINARY).*.macos $@/ + mv $(BINARY).*.{macos,linux} $@/ gzip -9r $@/ for i in $(BINARY)*.exe; do zip -9qm $@/$$i.zip $$i;done mv *.rpm *.deb $@/ @@ -49,7 +49,7 @@ release: clean vendor test macos arm arm64 windows linux_packages # Delete all build assets. clean: # Cleaning up. - rm -f $(BINARY) $(BINARY).*.{macos,linux,exe}{,.gz} $(BINARY).1{,.gz} $(BINARY).rb + rm -f $(BINARY) $(BINARY).*.{macos,linux,exe}{,.gz,.zip} $(BINARY).1{,.gz} $(BINARY).rb rm -f $(BINARY){_,-}*.{deb,rpm} v*.tar.gz.sha256 rm -f cmd/$(BINARY)/README{,.html} README{,.html} ./$(BINARY)_manual.html rm -rf package_build_* release diff --git a/init/docker/Dockerfile b/init/docker/Dockerfile index 4e9862e3..c92a1770 100644 --- a/init/docker/Dockerfile +++ b/init/docker/Dockerfile @@ -1,10 +1,10 @@ # # building static go binary with Debian golang container # -ARG ARCH=amd64 FROM golang:stretch as builder -ARG ARCH +ARG ARCH=amd64 +ARG OS=linux RUN mkdir -p $GOPATH/pkg/mod $GOPATH/bin $GOPATH/src/github.com/davidnewhall RUN apt-get update \ @@ -15,7 +15,8 @@ COPY . $GOPATH/src/github.com/davidnewhall/unifi-poller WORKDIR $GOPATH/src/github.com/davidnewhall/unifi-poller RUN dep ensure --vendor-only \ - && CGO_ENABLED=0 make unifi-poller.${ARCH}.linux + && CGO_ENABLED=0 make unifi-poller.${ARCH}.${OS} + && mv unifi-poller.${ARCH}.${OS} unifi-poller # # creating container for run @@ -25,10 +26,8 @@ RUN dep ensure --vendor-only \ # # 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.${ARCH}.linux /unifi-poller +COPY --from=builder /go/src/github.com/davidnewhall/unifi-poller/unifi-poller /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" ] diff --git a/init/docker/hooks/README.md b/init/docker/hooks/README.md index 1ac7b524..1646737c 100644 --- a/init/docker/hooks/README.md +++ b/init/docker/hooks/README.md @@ -1,3 +1,20 @@ # Docker Build Hooks The files in this folder are used by Docker Cloud to automate image builds. + +If you want to build, maintain and push multi-architecture Docker images, you may +follow the example provided here. All of the hooks are generic, and will work with +any build. Two environment variables must be passed in from Docker Cloud config. +`BUILDS` must be set to the builds you're trying to perform. This repo is currently +set to `linux:armhf:arm: linux:arm64:arm64:armv8 linux:amd64:amd64: linux:i386:386:`. +The format is `os:name:arch:variant`. `os` and `name` are passed into the Dockerfile. +`os`, `arch` and `variant` are passed into `docker manifest annotate`. This does not +yet work with an OS other than `linux`. + +Keep the build simple; see screenshot. This only supports one build tag, but it creates many more. + +![UniFi Poller Docker Cloud Build Rules](https://raw.githubusercontent.com/wiki/davidnewhall/unifi-poller/images/unifi-poller-build-rules.png "UniFi Poller Docker Cloud Build Rules") + +The fancy source tag is `/^v((\d+\.\d+)(?:\.\d+)?)$/` and it allows you to capture +the minor version without patch-level in `{\2}`. I no longer use `{\2}` in my build. +[See how it works here](https://regex101.com/r/fzt6ki/1). diff --git a/init/docker/hooks/build b/init/docker/hooks/build index 5e9e635e..136b6851 100755 --- a/init/docker/hooks/build +++ b/init/docker/hooks/build @@ -1,11 +1,20 @@ #!/bin/bash -# This always run local to the Dockerfile folder. +# This always run local to the Dockerfile folder, so the path is ../.. + set -e -o pipefail +# The Docker Cloud config must pass in the BUILDS env variable. +# See README.md (in this dir) and the screenshot for more info. + +# Build each configured image from Docker Cloud. for build in $BUILDS; do + # os:name:arch:variant 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 ../.. + docker build \ + --build-arg "ARCH=${name}" \ + --tag "${IMAGE_NAME}_${os}_${name}" \ + --file Dockerfile ../.. done diff --git a/init/docker/hooks/pre_build b/init/docker/hooks/pre_build index 6450a611..c072eb70 100755 --- a/init/docker/hooks/pre_build +++ b/init/docker/hooks/pre_build @@ -2,6 +2,11 @@ # https://www.smockle.com/blog/2019/04/22/migrating-from-travis-ci-to-docker-hub-automated-builds/ +# This upgrades the docker client on the Docker Cloud server to a version +# that contains the `docker manifest` command. To use `docker manifest` +# set `DOCKER_CLI_EXPERIMENTAL=enabled` in your build environment. +# See README.md (in this dir) and the screenshot for more info. + apt-get -y update apt-get -y --only-upgrade install docker-ee docker run --rm --privileged multiarch/qemu-user-static:register --reset diff --git a/init/docker/hooks/push b/init/docker/hooks/push index 5ce0f64f..c1aa530d 100755 --- a/init/docker/hooks/push +++ b/init/docker/hooks/push @@ -4,8 +4,8 @@ # 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) +VERSION=$(git tag -l --merged | tail -n1 | tr -d v) # 1.2.3 +SHORTVER=$(echo $VERSION | cut -d. -f1,2) # 1.2 if [ "$BUILDS" != "" ] && [ "$VERSION" != "" ]; then TAGS=latest fi @@ -20,16 +20,20 @@ for build in $BUILDS; do done echo "Annotating Images: ${IMAGES}" -[ "$VERSION" != "$SOURCE_BRANCH" ] || TAGS="latest $VERSION $SHORTVER stable" +# Build all the Docker tags if the github branch is a release and not master. +[ "$SOURCE_BRANCH" == "master" ] || TAGS="latest $VERSION $SHORTVER stable" echo "Version: $VERSION, Source: $SOURCE_BRANCH, Building tags: ${TAGS}" +# Create multi-architecture manifests for each tag with all the built images. for tag in $TAGS; do docker manifest create --amend ${DOCKER_REPO}:${tag} $IMAGES for build in $BUILDS; do + # os:name:arch:variant, ie linux:amd64:amd64: (no variant is ok) os=$(echo $build | cut -d: -f1) name=$(echo $build | cut -d: -f2) arch=$(echo $build | cut -d: -f3) vari=$(echo $build | cut -d: -f4) + # Annotating updates the manifest to describe each images' capabilities. docker manifest annotate ${DOCKER_REPO}:${tag} ${IMAGE_NAME}_${os}_${name} --os ${os} --arch ${arch} --variant "${vari}" done echo "Pushing Manifest ${DOCKER_REPO}:${tag}"