Skip to content

Instantly share code, notes, and snippets.

@ekarisky
Last active June 9, 2020 07:34
Show Gist options
  • Select an option

  • Save ekarisky/5ec5140cbc01c237a4956718dad6cca6 to your computer and use it in GitHub Desktop.

Select an option

Save ekarisky/5ec5140cbc01c237a4956718dad6cca6 to your computer and use it in GitHub Desktop.
Build and release in github repository
#!/usr/bin/env bash
export TAG=
export COMMIT_SHA=
export GITHUB_TOKEN=
export GITHUB_USER=
export GITHUB_REPO=
export GO_BINARY_NAME=
set -u -e -o pipefail
if [[ -z "${TAG}" ]]; then
echo 'ERROR: Missing TAG env'
exit 1
fi
echo "Building binaries for Github"
echo ""
export CGO_ENABLED=0
export GO_LDFLAGS="-s -w -extldflags \"-static\" -X main.BuildVersion=$TAG -X main.BuildCommitSha=$COMMIT_SHA -X main.BuildDate=$(date +%F-%T)"
echo "GO_LDFLAGS: $GO_LDFLAGS \n"
echo "go get gox & ghr"
go get github.com/mitchellh/gox
go get github.com/tcnksm/ghr
if [[ -f 'go.mod' ]]; then
go mod tidy
fi
gox -verbose -os="darwin linux windows freebsd netbsd openbsd" -arch="386 amd64" -rebuild -ldflags "${GO_LDFLAGS}" -output ".build/${GO_BINARY_NAME}-${TAG}.{{.OS}}-{{.Arch}}/${GO_BINARY_NAME}"
gox -verbose -os="linux freebsd netbsd" -arch="arm" -rebuild -ldflags "${GO_LDFLAGS}" -output ".build/${GO_BINARY_NAME}-${TAG}.{{.OS}}-{{.Arch}}/${GO_BINARY_NAME}"
gox -verbose -os="linux" -arch="arm64 mips64 mips64le ppc64 ppc64le s390x" -rebuild -ldflags "${GO_LDFLAGS}" -output ".build/${GO_BINARY_NAME}-${TAG}.{{.OS}}-{{.Arch}}/${GO_BINARY_NAME}"
mkdir -p dist
for build in $(ls .build); do
echo "Creating archive for ${build}"
cp LICENSE README.md ".build/${build}/"
if [[ "${build}" =~ windows-.*$ ]]; then
# Make sure to clear out zip files to prevent zip from appending to the archive.
rm "dist/${build}.zip" || true
cd ".build/" && zip -r --quiet -9 "../dist/${build}.zip" "${build}" && cd ../
else
tar -C ".build/" -czf "dist/${build}.tar.gz" "${build}"
fi
done
cd dist
sha256sum *.gz *.zip >sha256sums.txt
ls -la
cd ..
echo "Upload to Github"
ghr -parallel 1 -u ${GITHUB_USER} -r ${GITHUB_REPO} --replace "${TAG}" dist/
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment