-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.sh
executable file
·104 lines (85 loc) · 1.79 KB
/
build.sh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
set -e
cd "$(dirname "$0")"
PROGNAME="run-gocd"
rm -f "$PROGNAME"
rm -rf dist
RELEASE="${RELEASE:-X.x.x}"
for arg in $@; do
case $arg in
--verbose)
extra_flags="$extra_flags -v"
shift
;;
--skip-tests)
skip=true
shift
;;
--prod)
multiplatform=true
shift
;;
--release)
RELEASE="$2"
shift
;;
--release=*)
RELEASE="${arg#*=}"
shift
;;
*)
shift
;;
esac
done
RELEASE="${RELEASE}-${GO_PIPELINE_LABEL:-localbuild}"
function ldflags {
local _os="${1:-$(go env GOOS)}"
local _arch="${2:-$(go env GOARCH)}"
echo "-X main.Version=${RELEASE} -X main.GitCommit=${GIT_COMMIT} -X main.Platform=${_arch}-${_os}"
}
go version
echo "Fetching dependencies"
go get -tags netgo -d $extra_flags ./...
if [ "true" = "$skip" ]; then
echo "Skipping tests"
else
go test $extra_flags ./...
fi
if (which git &> /dev/null); then
GIT_COMMIT=$(git rev-list --abbrev-commit -1 HEAD)
else
GIT_COMMIT="unknown"
fi
if [ "true" = "$multiplatform" ]; then
platforms=(
darwin/amd64
darwin/arm64
linux/amd64
windows/amd64
)
echo "Release: ${RELEASE}, Revision: ${GIT_COMMIT}"
for plt in "${platforms[@]}"; do
mkdir -p "dist/${plt}"
arr=(${plt//\// })
_os="${arr[0]}"
_arch="${arr[1]}"
name="$PROGNAME"
build_tags="netgo"
if [ "windows" = "${_os}" ]; then
name="$name.exe"
build_tags=""
fi
echo "Building $plt..."
GOOS="${_os}" go get -tags "${build_tags}" -d $extra_flags ./...
GOOS="${_os}" GOARCH="${_arch}" go build \
-tags "${build_tags}" \
-a \
-o "dist/${plt}/${name}" \
-ldflags "$(ldflags "$_os" "$_arch")"
done
else
go build \
-ldflags "$(ldflags)" \
-o "$PROGNAME"
fi