Skip to content

Commit

Permalink
Create statically linked dist for linux
Browse files Browse the repository at this point in the history
Statically-linked dist is required for portability to other linux distros. This is due to the use of cgo and ensures the necessary C libs are included in the linux binary. The resultant binary is portable to at least ubuntu-18 and alpine distros.

For mac the standard dynamically linked binary is fine as portability is not a concern.
  • Loading branch information
chris-j-h committed Jul 24, 2020
1 parent 8f66ddc commit 9ff0dcb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/release.yml
Expand Up @@ -22,11 +22,16 @@ jobs:
run: |
echo "::set-output name=version::${GITHUB_REF##*/}"
echo "::set-output name=version_short::${GITHUB_REF##*/v}"
- name: Create dist
id: build
- name: Create macos dist
if: ${{ matrix.os == 'macos-latest' }}
run: |
export PATH=$PATH:$(go env GOPATH)/bin
make VERSION=${{ steps.meta.outputs.version_short }}
- name: Create portable linux dist
if: ${{ matrix.os == 'ubuntu-latest' }}
run: |
export PATH=$PATH:$(go env GOPATH)/bin
make VERSION=${{ steps.meta.outputs.version_short }} extraldflags='-linkmode external -extldflags "-static"'
- name: Upload artifacts
uses: actions/upload-artifact@v2
if: success()
Expand Down
13 changes: 7 additions & 6 deletions Makefile
Expand Up @@ -5,9 +5,10 @@ EXECUTABLE := "quorum-account-plugin-hashicorp-vault"
PACKAGE ?= quorum-account-plugin-hashicorp-vault
OUTPUT_DIR := "$(shell pwd)/build"
VERSION := "0.0.1"
LD_FLAGS="-X main.GitCommit=${GIT_COMMIT} -X main.GitBranch=${GIT_BRANCH} -X main.GitRepo=${GIT_REPO} \
GEN_LD_FLAGS="-X main.GitCommit=${GIT_COMMIT} -X main.GitBranch=${GIT_BRANCH} -X main.GitRepo=${GIT_REPO} \
-X main.Executable=${EXECUTABLE} -X main.Version=${VERSION} -X main.OutputDir=${OUTPUT_DIR}"
DOCKER_LD_FLAGS="-X main.GitCommit=${GIT_COMMIT} -X main.GitBranch=${GIT_BRANCH} -X main.GitRepo=${GIT_REPO} \
BUILD_LD_FLAGS=-s -w $(extraldflags)
DOCKER_GEN_LD_FLAGS="-X main.GitCommit=${GIT_COMMIT} -X main.GitBranch=${GIT_BRANCH} -X main.GitRepo=${GIT_REPO} \
-X main.Executable=${EXECUTABLE} -X main.Version=${VERSION} -X main.OutputDir=/shared"

.PHONY: ${OUTPUT_DIR}
Expand Down Expand Up @@ -39,9 +40,9 @@ dist: clean build zip
build: checkfmt
@mkdir -p ${OUTPUT_DIR}/dist
@echo Output to ${OUTPUT_DIR}/dist
@CGO_ENABLED=0 GOFLAGS="-mod=readonly" go run -ldflags=${LD_FLAGS} ./internal/metadata/gen.go
@CGO_ENABLED=0 GOFLAGS="-mod=readonly" go run -ldflags=${GEN_LD_FLAGS} ./internal/metadata/gen.go
@GOFLAGS="-mod=readonly" go build \
-ldflags="-s -w" \
-ldflags='$(BUILD_LD_FLAGS)' \
-o "${OUTPUT_DIR}/dist/${EXECUTABLE}" \
.

Expand All @@ -64,9 +65,9 @@ build-alpine: checkfmt

build-alpine-docker:
go test ./...
CGO_ENABLED=0 go run -ldflags=${DOCKER_LD_FLAGS} ./internal/metadata/gen.go
CGO_ENABLED=0 go run -ldflags=${DOCKER_GEN_LD_FLAGS} ./internal/metadata/gen.go
go build \
-ldflags="-s -w" \
-ldflags='$(BUILD_LD_FLAGS)' \
-o "/shared/linux/${EXECUTABLE}" \
.
zip -j -FS -q /shared/linux/${EXECUTABLE}-${VERSION}.zip /shared/*.json /shared/linux/*
Expand Down

0 comments on commit 9ff0dcb

Please sign in to comment.