Skip to content

Commit

Permalink
Slim the go binary
Browse files Browse the repository at this point in the history
  • Loading branch information
nandak522 committed May 26, 2023
1 parent d8544e9 commit 37f8df7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ COPY go.* /app/
COPY templates /app/templates/
COPY static /app/static/
RUN cd /app && \
go build -a -o server
go build -ldflags="-s -w" -a -o server

FROM ubuntu:23.04
COPY --from=builder /etc/passwd /etc/passwd
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
## Run server
```sh
go build -o server && ./server

# If you are really conscious about the size. You can run this instead:
go build -ldflags="-s -w" -a -o server && ./server
```

---
Expand Down
7 changes: 3 additions & 4 deletions generate-binaries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ set -x -eou pipefail
echo "Proceeding with generating the binaries..."

platforms=("linux/arm64" "linux/amd64" "darwin/amd64")
VERSION=`grep -E "MAJOR|MINOR|PATCH" version.go | cut -d '"' -f 2 | xargs echo -n | tr -s " " "."`
VERSION=$(grep -E "MAJOR|MINOR|PATCH" version.go | cut -d '"' -f 2 | xargs echo -n | tr -s " " ".")

for platform in "${platforms[@]}"
do
for platform in "${platforms[@]}"; do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name=hello-universe'-v'$VERSION'-'$GOOS'-'$GOARCH

env GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name .
env GOOS=$GOOS GOARCH=$GOARCH go build -ldflags="-s -w" -a -o $output_name .
if [ $? -ne 0 ]; then
echo 'An error has occurred! Aborting the script execution...'
exit 1
Expand Down

0 comments on commit 37f8df7

Please sign in to comment.