-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto_build.sh
52 lines (42 loc) · 1.1 KB
/
auto_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
#!/bin/bash
# Args supplied by the user
package=$1
# Default to package main
if [[ -z "$package" ]]; then
package=main
fi
# Create the build directory
mkdir -p build
# Extract the pkg name
package_split=(${package//\// })
package_name=${package_split[-1]}
# Supported platforms
platforms=("windows/amd64" "linux/amd64" "darwin/amd64")
for platform in "${platforms[@]}"
do
# Assign the correct OS and ARCH
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
# Final pkg name
output_name=$package_name'-'$GOOS'-'$GOARCH
# Assign the appropriate extension based on the platform
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi
if [ $GOOS = "linux" ]; then
output_name+='.bin'
fi
if [ $GOOS = "darwin" ]; then
output_name+='.bin'
fi
# Set the env vars and build the binary
env GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=0 go build -ldflags="-s -w" -o ./build/$output_name main.go
# Exit if the build failed
if [ $? -ne 0 ]; then
echo 'An error has occurred! Aborting the script execution...'
exit 1
else
echo "Built ./build/${output_name}"
fi
done