driftctl/scripts/build.sh

87 lines
2.0 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
#
# This script builds the application from source for multiple platforms.
# Inspired from hashicorp/terraform build script
2020-12-14 15:13:47 +00:00
set -eo pipefail
echo "Bash: ${BASH_VERSION}"
# By default build for dev
ENV=${ENV:-"dev"}
# Get the git commit
GIT_COMMIT=$(git rev-parse HEAD)
GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "-dev" || true)
# Retrieve
2021-01-27 14:19:15 +00:00
VERSION=$(git describe --exact-match 2>/dev/null || git rev-parse --short HEAD)
# Inject version number
LD_FLAGS="-X github.com/cloudskiff/driftctl/pkg/version.version=${VERSION}"
# Reference:
# https://github.com/golang/go/blob/master/src/go/build/syslist.go
os_archs=(
darwin/amd64
2021-02-17 08:59:51 +00:00
darwin/arm64
linux/386
linux/amd64
linux/arm
linux/arm64
windows/386
windows/amd64
)
if [ -n "$OS_ARCH" ]; then
os_archs=("$OS_ARCH")
fi
if [ $ENV != "release" ]; then
echo "+ Building env: dev"
# If its dev mode, only build for ourself
os_archs=("$(go env GOOS)/$(go env GOARCH)")
# And set version to git commit
VERSION="${GIT_COMMIT}${GIT_DIRTY}"
fi
2021-04-08 14:00:30 +00:00
echo "ARCH: ${os_archs[*]}"
2021-01-04 15:54:43 +00:00
# In release mode we don't want debug information in the binary
# We also set the build env to release
if [ $ENV = "release" ]; then
echo "+ Building env: release"
LD_FLAGS="-s -w -X github.com/cloudskiff/driftctl/build.env=release ${LD_FLAGS}"
fi
if ! which gox > /dev/null; then
echo "+ Installing gox..."
2021-04-06 14:48:47 +00:00
go install github.com/mitchellh/gox
fi
# Delete old binaries
echo "+ Removing old binaries ..."
rm -f bin/*
# Instruct gox to build statically linked binaries
export CGO_ENABLED=0
# Ensure all remote modules are downloaded and cached before build so that
# the concurrent builds launched by gox won't race to redundantly download them.
go mod download
# Build!
echo "+ Building with flags: ${LD_FLAGS}"
osarch="${os_archs[@]}"
gox \
-osarch="$osarch" \
-parallel=2 \
-ldflags "${LD_FLAGS}" \
-output "bin/driftctl_{{.OS}}_{{.Arch}}" \
./
2021-02-03 11:19:12 +00:00
if [ $ENV = "release" ]; then
echo "+ Computing checksums"
cd bin
sha256sum * > driftctl_SHA256SUMS
fi