This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gogo-release
executable file
·83 lines (69 loc) · 2.28 KB
/
gogo-release
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh
#
# https://github.com/arp242/gogo-release
set -euC
# Valid combinations: https://golang.org/doc/install/source#environment
# Everything from the third argument on are set as env vars; for example:
# linux amd64 CC=foo CXX=bar
matrix="
linux amd64
linux 386
linux arm64
windows amd64
"
# Program to run on the binary after build, usually to compress it. "upx -qqq"
# is recommended for servers and other programs where an extra ~200ms of startup
# time isn't a big deal. It's not recommended for CLI apps and the like.
#
# gzip -f can be used to just compress the uploaded file.
#post_build="upx -qqq"
post_build=""
# Location to put the binaries.
tmp=./dist
mkdir -p "$tmp"
# Project name.
name=$(basename "$(go list)")
# Get the version from commandline or git tag.
tag=${1:-}
if [ -z "$tag" ]; then
commit=$(git log -n1 --format='%H' | head -c10)
tag=$(git tag -l --points-at "$commit")
if [ -z "$tag" ]; then
tag=$commit
# echo >&2 "Need to have a tag on the current commit or give the version as the first argument (i.e. ${0} v1.0.0)"
# exit 1
fi
fi
# Flags to pass to "go build"; you can add a package name or build tags here;
# for example in your local .gogo-release you can append to it like so:
# build_flags="$build_flags -tags mytag ./cmd/myapp"
build_flags="-trimpath -ldflags='-w -s -X main.version=$tag'"
# Disable cgo by default to create static binaries; re-enable in local
# .gogo-release if you don't want this.
export CGO_ENABLED=0
# Pick up configuration to override any of the above.
[ -f "./.gogo-release" ] && . ./.gogo-release
# Compile
IFS="
"
for arch in $matrix; do
(
export GOOS=${arch%% *}
export GOARCH=${arch#* }
env=${GOARCH#* }
export GOARCH=${GOARCH%% *}
[ "$env" != "$GOARCH" ] && IFS=" " && for e in $env; do export "$e"; done
# out="$tmp/$name-$tag-$GOOS-$GOARCH"
out="$tmp/$name-$GOOS-$GOARCH"
[ "$GOOS" = "windows" ] && out="$out.exe"
echo "=== BUILDING $out ==="
eval "time go build -o $out $build_flags"
if [ -n "${post_build:-}" ]; then
printf '\r%60s\rCompressing %s' ' ' "$out"
eval time $post_build "$out" || :
fi
)
done
echo
type gogo_before_exit >/dev/null 2>&1 && gogo_before_exit
exit 0