Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Last active December 4, 2022 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckylittle/55c4125d983fb3c4010cb3e1f5945ead to your computer and use it in GitHub Desktop.
Save luckylittle/55c4125d983fb3c4010cb3e1f5945ead to your computer and use it in GitHub Desktop.
Automatically build multi-platform, compressed binaries for Go in the current directory.
#!/usr/bin/env bash
# MIT License
# Copyright (c) 2020 Lucian Maly, <lucian@redhat.com>
# Raspberry Pi sometimes need more env to be built correctly, depending on the ARM version:
# `GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0 go build` worked for my Raspberry [ARMv7 Processor rev 5 v7l]
cyan=`tput setaf 14`
beige=`tput setaf 215`
white=`tput setaf 15`
black=`tput setaf 0`
reset=`tput sgr0`
echo $"${cyan} ´.-::::::-.´"
echo $"${cyan} .:-::::::::::::::-:."
echo $"${cyan} ´_::${white}: :: :${cyan}::_´"
echo $"${cyan} .:${white}( ^ :: ^ )${cyan}:."
echo $"${cyan} ´::${white}: ${beige}(${black}..${beige})${white} :${cyan}::."
echo $"${cyan} ´:::::::${white}UU${cyan}:::::::´"
echo $"${cyan} .::::::::::::::::."
echo $"${beige} O${cyan}::::::::::::::::${beige}O"
echo $"${cyan} -::::::::::::::::-"
echo $"${cyan} ´::::::::::::::::´"
echo $"${cyan} .::::::::::::::."
echo $"${beige} oO${cyan}:::::::${beige}Oo"
echo "${reset}"
package=$1
if [[ -z "$package" ]]; then
echo "Usage: $0 <package-name-in-the-\$GOPATH/src/>"
echo "Automatically build multi-platform, compressed binaries for Go in the current directory."
echo "Example: $ $0 github.com/luckylittle/example"
echo "Requirements: \$GOPATH, go, git, zip"
exit 1
fi
package_split=(${package//\// })
package_name=${package_split[-1]}
platforms=("linux/amd64" "linux/386" "linux/arm64" "linux/arm" "windows/amd64" "windows/386" "darwin/amd64")
gittag='v'$(git -C $GOPATH/src/$package --no-pager tag)
md5='```'
md5+="\n#MD5SUMS:\n"
for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name=$package_name'-'$gittag'-'$GOOS'-'$GOARCH
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi
env GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=0 go build -o $output_name $package
if [ $GOOS = "windows" ]; then
zipfile="${output_name%%.exe}"
else zipfile=$output_name
fi
md5+="$(md5sum $output_name)\n"
zip -rm $zipfile'.zip' $output_name
if [ $? -ne 0 ]; then
echo 'An error has occurred!'
exit 1
fi
done
md5+="Generated by https://gist.github.com/luckylittle/55c4125d983fb3c4010cb3e1f5945ead\n"
md5+='```'
echo -e $md5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment