Skip to content

Commit e63a8bc

Browse files
authored
Merge pull request #46 from coderbirju/make-binaries-static
Make binaries static
2 parents 80fdae9 + 233d424 commit e63a8bc

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

.github/workflows/release-automation.yaml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
uses: "WyriHaximus/github-action-get-previous-tag@04e8485ecb6487243907e330d522ff60f02283ce" # v1.4.0
2323
generate-artifacts:
2424
needs: get-latest-tag
25-
runs-on: ubuntu-latest
25+
runs-on: ubuntu-20.04
2626
env:
2727
# Set during setup.
2828
RELEASE_TAG: ${{ needs.get-latest-tag.outputs.tag }}
@@ -43,22 +43,29 @@ jobs:
4343
export release_tag=${{ env.RELEASE_TAG }}
4444
export release_version=${release_tag/v/} # Remove v from tag name
4545
echo "DYNAMIC_BINARY_NAME=finch-daemon-${release_version}-linux-amd64.tar.gz" >> $GITHUB_ENV
46+
echo "STATIC_BINARY_NAME=finch-daemon-${release_version}-linux-amd64-static.tar.gz" >> $GITHUB_ENV
4647
4748
mkdir release
4849
- name: Install Go licenses
4950
run: go install github.com/google/go-licenses@latest
5051
- name: Create Third Party Licences File
5152
run: make licenses
53+
- name: setup static dependecies
54+
run: |
55+
sudo apt-get update
56+
sudo apt-get install libc6-dev -f
5257
- name: Create release binaries
5358
run: make RELEASE_TAG=${{ env.RELEASE_TAG }} release
5459
- name: Verify Release version
5560
run: |
56-
mkdir output
57-
tar -xzf release/${{ env.DYNAMIC_BINARY_NAME }} -C ./output
58-
BINARY_VERSION=$(./output/finch-daemon --version | grep -oP '\d+\.\d+\.\d+')
61+
mkdir -p output/static output/dynamic
62+
tar -xzf release/${{ env.DYNAMIC_BINARY_NAME }} -C ./output/dynamic
63+
tar -xzf release/${{ env.STATIC_BINARY_NAME }} -C ./output/static
64+
DYNAMIC_BINARY_VERSION=$(./output/dynamic/finch-daemon --version | grep -oP '\d+\.\d+\.\d+')
65+
STATIC_BINARY_VERSION=$(./output/static/finch-daemon --version | grep -oP '\d+\.\d+\.\d+')
5966
export release_tag=${{ env.RELEASE_TAG }}
6067
export release_version=${release_tag/v/}
61-
if ["$BINARY_VERSION" != "$release_version"]; then
68+
if ["$STATIC_BINARY_VERSION" != "$release_version"] || ["$DYNAMIC_BINARY_VERSION" != "$release_version"]; then
6269
echo "Version mismatch"
6370
exit 1
6471
fi
@@ -98,3 +105,5 @@ jobs:
98105
files: |
99106
${{ needs.generate-artifacts.outputs.dynamic_binary_name }}
100107
${{ needs.generate-artifacts.outputs.dynamic_binary_name }}.sha256sum
108+
${{ needs.generate-artifacts.outputs.static_binary_name }}
109+
${{ needs.generate-artifacts.outputs.static_binary_name }}.sha256sum

Makefile

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,30 @@ BINDIR ?= $(PREFIX)/bin
1313

1414
BINARY = $(addprefix bin/,finch-daemon)
1515

16+
PACKAGE := github.com/runfinch/finch-daemon
17+
VERSION := $(shell git describe --match 'v[0-9]*' --dirty='.modified' --always --tags)
18+
GITCOMMIT := $(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi)
19+
1620
ifndef GODEBUG
1721
EXTRA_LDFLAGS += -s -w
1822
endif
1923

24+
LDFLAGS_BASE := -X $(PACKAGE)/version.Version=$(VERSION) -X $(PACKAGE)/version.GitCommit=$(GITCOMMIT) $(EXTRA_LDFLAGS)
25+
2026
.PHONY: build
2127
build:
22-
$(eval PACKAGE := github.com/runfinch/finch-daemon)
23-
$(eval VERSION ?= $(shell git describe --match 'v[0-9]*' --dirty='.modified' --always --tags))
24-
$(eval GITCOMMIT := $(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi))
25-
$(eval LDFLAGS := "-X $(PACKAGE)/version.Version=$(VERSION) -X $(PACKAGE)/version.GitCommit=$(GITCOMMIT) $(EXTRA_LDFLAGS)")
26-
GOOS=linux go build -ldflags $(LDFLAGS) -v -o $(BINARY) $(PACKAGE)/cmd/finch-daemon
28+
ifeq ($(STATIC),)
29+
@echo "Building Dynamic Binary"
30+
CGO_ENABLED=1 GOOS=linux go build \
31+
-ldflags "$(LDFLAGS_BASE)" \
32+
-v -o $(BINARY) $(PACKAGE)/cmd/finch-daemon
33+
else
34+
@echo "Building Static Binary"
35+
CGO_ENABLED=0 GOOS=linux go build \
36+
-tags netgo \
37+
-ldflags "$(LDFLAGS_BASE) -extldflags '-static'" \
38+
-v -o $(BINARY) $(PACKAGE)/cmd/finch-daemon
39+
endif
2740

2841
clean:
2942
@rm -f $(BINARIES)

scripts/create-releases.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fi
5454

5555
release_version=${1/v/} # Remove v from tag name
5656
dynamic_binary_name=finch-daemon-${release_version}-linux-${ARCH}.tar.gz
57+
static_binary_name=finch-daemon-${release_version}-linux-${ARCH}-static.tar.gz
5758

5859
make build
5960
cp "$LICENSE_FILE" "${OUT_DIR}"
@@ -62,6 +63,14 @@ tar -czvf "$RELEASE_DIR"/"$dynamic_binary_name" -- *
6263
popd
6364
rm -rf "{$OUT_DIR:?}"/*
6465

66+
STATIC=1 make build
67+
cp "$LICENSE_FILE" "${OUT_DIR}"
68+
pushd "$OUT_DIR"
69+
tar -czvf "$RELEASE_DIR"/"$static_binary_name" -- *
70+
popd
71+
rm -rf "{$OUT_DIR:?}"/*
72+
6573
pushd "$RELEASE_DIR"
6674
sha256sum "$dynamic_binary_name" > "$RELEASE_DIR"/"$dynamic_binary_name".sha256sum
75+
sha256sum "$static_binary_name" > "$RELEASE_DIR"/"$static_binary_name".sha256sum
6776
popd

scripts/verify-release-artifacts.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ release_tag=$1
4444
release_version=${release_tag/v/}
4545

4646
pushd "$release_dir" || exit 1
47-
tarballs=("finch-daemon-${release_version}-linux-${arch}.tar.gz")
47+
tarballs=("finch-daemon-${release_version}-linux-${arch}.tar.gz" "finch-daemon-${release_version}-linux-${arch}-static.tar.gz")
4848
expected_contents=("finch-daemon" "THIRD_PARTY_LICENSES")
4949
release_is_valid=true
5050

0 commit comments

Comments
 (0)