-
Notifications
You must be signed in to change notification settings - Fork 3
/
build-script.sh
executable file
·133 lines (101 loc) · 3.79 KB
/
build-script.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# This script will generate binaries for the multiple OS/architectures, and a file
# containing SHA256 checksums for the binaries
# Directory where the binaries will be generated - created if does not exist
bin_path="./bin"
# File containing checksums - just the file name, file will be saved inside `bin_path`
checksums="checksums.txt"
# Basename for the generated binaries - name of the project for example
basename="{{ cookiecutter.project_name.strip() }}"
# Release version - can be blank if needed; if blank, will be populated by command-line
# arguments passed while running this script (if any).
version=""
if [[ "${version}" == "" ]]; then
# If empty, populate `version` with the first argument passed to the script
version="${1}"
fi
# Name pattern used for files being generated - will be appended with build system info
file_name="${basename}_${version}_"
# The final file path
full_path="${bin_path}/${file_name}"
# Remove existing binaries (if any)
rm -f "${full_path}"** # force-flag ensures no error message if no file is found
# Ensure `bin_path` exists, created if does not exist, ignored otherwise
mkdir -p "${bin_path}"
echo -e "\n\tGenerating binaries\n"
# Build linux targets
echo -e "\nBuilding for Linux"
echo -n " -> Targeting x86_64" # `-n` skips newline at the end
env GOOS=linux GOARCH=amd64 go build -o "${full_path}linux_x86_64"
echo -e ": Done"
echo -n " -> Targeting x86"
env GOOS=linux GOARCH=386 go build -o "${full_path}linux_x86"
echo -e ": Done"
echo -n " -> Targeting ARM"
env GOOS=linux GOARCH=arm go build -o "${full_path}linux_arm"
echo -e ": Done"
echo -n " -> Targeting ARM64"
env GOOS=linux GOARCH=arm64 go build -o "${full_path}linux_arm64"
echo -e ": Done"
# Build windows targets
echo -e "\n\nBuilding for Windows"
echo -n " -> Targeting x86_64"
env GOOS=windows GOARCH=amd64 go build -o "${full_path}windows_x86_64.exe"
echo -e ": Done"
echo -n " -> Targeting x86"
env GOOS=windows GOARCH=386 go build -o "${full_path}windows_x86.exe"
echo -e ": Done"
# Build mac targets
echo -e "\n\nBuilding for MacOS"
echo -n " -> Targeting x86_64"
env GOOS=darwin GOARCH=amd64 go build -o "${full_path}darwin_x86_64"
echo -e ": Done"
echo -n " -> Targeting ARM64"
env GOOS=darwin GOARCH=arm64 go build -o "${full_path}darwin_arm64"
echo -e ": Done"
echo -e "\nBinaries Generated Successfully!"
echo -ne "\nGenerating Checksums"
# Iterate through each file in output directory matching the pattern
flag=true
for file in "${full_path}"**; do
if [[ "${flag}" == true ]]; then
# Overwrite the file with this line
sha256sum "$file" >"${checksums}"
flag=false # mark flag as off
else
sha256sum "$file" >>"${checksums}"
fi
done
echo -e ": Done"
echo -ne "Archiving Binaries"
cd "${bin_path}" || exit # Exit if `cd` fails
# Zip the windows executables
if ! command -v zip >/dev/null; then
echo -e "\tZip not found, unable to generate archives for Windows executables"
else
# Iterating through each executable file - to generate individual `.zip` files
for file in "${file_name}"**".exe"; do
zip_name="${file//.exe/}" # form name of the zip file - remove `.exe`
# Create a zip container for the file, the `-m` flag deletes the original file
zip "${zip_name}.zip" "${file}" -q -m
done
fi
# Run GZip on Linux/Mac binaries
if ! command -v gzip >/dev/null; then
echo -e "\tGZip not found, unable to generate archives"
else
for file in "${file_name}"**; do
if [[ "${file}" == "${file_name}"**".zip" ]]; then
# Skip iteration over zip files
continue
fi
gzip "${file}" -q
done
fi
# Add these checksums to the text file as well
echo "" >>"${checksums}" # append new line
for file in "${file_name}"**; do
sha256sum "$file" >>"${checksums}"
done
echo -e ": Done"
echo -e "\nExecution completed successfully \nBinaries stored in: \"${bin_path}\" \n"