Skip to content

Commit 32208cb

Browse files
authored
Merge pull request #19 from coderbirju/make-binaries-static
feat: Make binaries static
2 parents 5d6878c + 5887d3b commit 32208cb

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-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-22.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: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,23 @@ 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+
20+
LDFLAGS := -X $(PACKAGE)/version.Version=$(VERSION) -X $(PACKAGE)/version.GitCommit=$(GITCOMMIT)
21+
22+
ifeq ($(STATIC),)
23+
GO_BUILDTAGS := osusergo netgo
24+
LDFLAGS += -extldflags '-static'
25+
$(info Building Static Binary)
26+
else
27+
$(info Building Dynamic Binary)
28+
endif
29+
1630
.PHONY: build
1731
build:
18-
$(eval PACKAGE := github.com/runfinch/finch-daemon)
19-
$(eval VERSION ?= $(shell git describe --match 'v[0-9]*' --dirty='.modified' --always --tags))
20-
$(eval GITCOMMIT := $(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi))
21-
$(eval LDFLAGS := "-X $(PACKAGE)/version.Version=$(VERSION) -X $(PACKAGE)/version.GitCommit=$(GITCOMMIT)")
22-
GOOS=linux go build -ldflags $(LDFLAGS) -v -o $(BINARY) $(PACKAGE)/cmd/finch-daemon
32+
GOOS=linux go build $(if $(GO_BUILDTAGS), -tags "$(GO_BUILDTAGS)") -ldflags "$(LDFLAGS)" -v -o $(BINARY) $(PACKAGE)/cmd/finch-daemon
2333

2434
.PHONY: linux
2535
linux:

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)