Skip to content

Commit

Permalink
chore: Use goreleaser for build
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko committed Dec 1, 2023
1 parent 13bdc4e commit 37ab81b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
24 changes: 12 additions & 12 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ release:
**Full Changelog**: https://github.com/obalunenko/advent-of-code/compare/{{ .PreviousTag }}...{{ .Tag }}
gomod:
proxy: true
proxy: false
# If proxy is true, use these environment variables when running `go mod` commands (namely, `go mod tidy`).
# Defaults to `os.Environ()`.
env:
Expand All @@ -16,24 +16,24 @@ gomod:
- CGO_ENABLED=0
# Which Go binary to use.
# Defaults to `go`.
mod: vendor
gobinary: go

before:
hooks:
- go mod verify

builds:
- id: cli
binary: aoc-cli
goos:
- linux
- darwin
- windows
- 'linux'
- 'darwin'
- 'windows'
goarch:
- 386
- amd64
- arm
- arm64
- 'amd64'
- 'arm'
- 'arm64'
ignore:
- goos: darwin
goarch: 'arm'

mod_timestamp: '{{ .CommitTimestamp }}'
env:
- CGO_ENABLED=0
Expand Down
29 changes: 21 additions & 8 deletions scripts/build/app.sh
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
#!/bin/sh
#!/bin/bash

set -eu

SCRIPT_NAME="$(basename "$0")"
SCRIPT_DIR="$(dirname "$0")"
REPO_ROOT="$(cd "${SCRIPT_DIR}" && git rev-parse --show-toplevel)"
SCRIPTS_DIR="${REPO_ROOT}/scripts"
source "${SCRIPTS_DIR}/helpers-source.sh"

BIN_DIR=${REPO_ROOT}/bin
mkdir -p "${BIN_DIR}"

echo "${SCRIPT_NAME} is running... "

checkInstalled 'goreleaser'

goreleaser healthcheck

APP=${APP_NAME}

echo "Building ${APP}..."

COMMIT="$(git rev-parse HEAD)"
SHORTCOMMIT="$(git rev-parse --short HEAD)"
DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
VERSION="$(git tag --sort=committerdate | tail -1)"
VERSION="$(git tag | sort -V | tail -1)"
GOVERSION="$(go version | awk '{print $3;}')"

if [ -z "${VERSION}" ] || [ "${VERSION}" = "${SHORTCOMMIT}" ]
then
if [ -z "${VERSION}" ] || [ "${VERSION}" = "${SHORTCOMMIT}" ]; then
VERSION="v0.0.0"
fi

if [[ $(git diff --stat) != '' ]]; then
echo 'dirty'

COMMIT="${COMMIT}-dirty"
SHORTCOMMIT="${SHORTCOMMIT}-dirty"
VERSION="${VERSION}-dirty"
fi

BIN_OUT="${BIN_DIR}/${APP}"

BUILDINFO_VARS_PKG=github.com/obalunenko/version
GO_BUILD_LDFLAGS="-s -w \
export GO_BUILD_LDFLAGS="-s -w \
-X ${BUILDINFO_VARS_PKG}.version=${VERSION} \
-X ${BUILDINFO_VARS_PKG}.commit=${COMMIT} \
-X ${BUILDINFO_VARS_PKG}.shortcommit=${SHORTCOMMIT} \
-X ${BUILDINFO_VARS_PKG}.builddate=${DATE} \
-X ${BUILDINFO_VARS_PKG}.appname=${APP} \
-X ${BUILDINFO_VARS_PKG}.goversion=${GOVERSION}"

GO_BUILD_PACKAGE="${REPO_ROOT}/cmd/${APP}"

rm -rf "${BIN_OUT}"

go build -trimpath -o "${BIN_OUT}" -a -ldflags "${GO_BUILD_LDFLAGS}" "${GO_BUILD_PACKAGE}"
goreleaser build --skip=validate --clean --single-target --output "${BIN_OUT}"

echo "Binary compiled at ${BIN_OUT}"

0 comments on commit 37ab81b

Please sign in to comment.