From 2afc8965c380b8c966825531eafbeb1ccdfbe362 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Mon, 8 Apr 2019 10:22:25 +0200 Subject: [PATCH] Improve build script * Use single gox command to parallelize build. * Automatically package any archs gox spits out. * Add sha256sums for packages. --- .gitignore | 1 + build-github-binaries.sh | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index fe2a154f..3f346795 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ coverage.out dist/ pkg/ src/ +.build/ .DS_Store .idea *.rdb diff --git a/build-github-binaries.sh b/build-github-binaries.sh index 3f336789..8b5474f9 100755 --- a/build-github-binaries.sh +++ b/build-github-binaries.sh @@ -1,4 +1,11 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash + +set -u -e -o pipefail + +if [[ -z "${DRONE_TAG}" ]] ; then + echo 'ERROR: Missing DRONE_TAG env' + exit 1 +fi echo "Building binaries for Github" echo "" @@ -8,15 +15,23 @@ echo "GO_LDFLAGS: $GO_LDFLAGS" go get github.com/mitchellh/gox go get github.com/tcnksm/ghr +if [[ -f 'go.mod' ]] ; then + go mod tidy +fi -gox -rebuild --osarch="darwin/amd64" -ldflags "$GO_LDFLAGS" -output "dist/redis_exporter" && cd dist && tar -cvzf redis_exporter-$DRONE_TAG.darwin-amd64.tar.gz redis_exporter && rm redis_exporter && cd .. -gox -rebuild --osarch="darwin/386" -ldflags "$GO_LDFLAGS" -output "dist/redis_exporter" && cd dist && tar -cvzf redis_exporter-$DRONE_TAG.darwin-386.tar.gz redis_exporter && rm redis_exporter && cd .. -gox -rebuild --osarch="linux/amd64" -ldflags "$GO_LDFLAGS" -output "dist/redis_exporter" && cd dist && tar -cvzf redis_exporter-$DRONE_TAG.linux-amd64.tar.gz redis_exporter && rm redis_exporter && cd .. -gox -rebuild --osarch="linux/386" -ldflags "$GO_LDFLAGS" -output "dist/redis_exporter" && cd dist && tar -cvzf redis_exporter-$DRONE_TAG.linux-386.tar.gz redis_exporter && rm redis_exporter && cd .. -gox -rebuild --osarch="netbsd/amd64" -ldflags "$GO_LDFLAGS" -output "dist/redis_exporter" && cd dist && tar -cvzf redis_exporter-$DRONE_TAG.netbsd-amd64.tar.gz redis_exporter && rm redis_exporter && cd .. -gox -rebuild --osarch="netbsd/386" -ldflags "$GO_LDFLAGS" -output "dist/redis_exporter" && cd dist && tar -cvzf redis_exporter-$DRONE_TAG.netbsd-386.tar.gz redis_exporter && rm redis_exporter && cd .. -gox -rebuild --osarch="windows/amd64" -ldflags "$GO_LDFLAGS" -output "dist/redis_exporter" && cd dist && zip -9 redis_exporter-$DRONE_TAG.windows-amd64.zip redis_exporter.exe && rm redis_exporter.exe && cd .. -gox -rebuild --osarch="windows/386" -ldflags "$GO_LDFLAGS" -output "dist/redis_exporter" && cd dist && zip -9 redis_exporter-$DRONE_TAG.windows-386.zip redis_exporter.exe && rm redis_exporter.exe && cd .. +gox -rebuild -ldflags "${GO_LDFLAGS}" -output '.build/{{.OS}}-{{.Arch}}/{{.Dir}}' + +mkdir -p dist +for build in $(ls .build); do + echo "Creating archive for ${build}" + if [[ "${build}" =~ ^windows-.*$ ]] ; then + # Make sure to clear out zip files to prevent zip from appending to the archive. + rm "dist/redis_exporter-${DRONE_TAG}.${build}.zip" + cd ".build/${build}" && zip --quiet -9 "../../dist/redis_exporter-${DRONE_TAG}.${build}.zip" 'redis_exporter.exe' && cd ../../ + else + tar -C ".build/${build}" -czf "dist/redis_exporter-${DRONE_TAG}.${build}.tar.gz" 'redis_exporter' + fi +done echo "Upload to Github" @@ -24,6 +39,10 @@ pwd ls -la ls -la dist/ -ghr -u oliver006 -r redis_exporter --replace $DRONE_TAG dist/ +cd dist +sha256sum *.gz *.zip > sha256sums.txt +cd .. + +ghr -u oliver006 -r redis_exporter --replace "${DRONE_TAG}" dist/ echo "Done"