sshtunnel/bin/scripts/build-binary.sh
Menno van Leeuwen a806b97b9b
Some checks failed
goreleaser / goreleaser (push) Failing after 5m46s
Add GoReleaser configuration and version command
2025-05-23 15:41:54 +02:00

53 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
BINARY_NAME=$1
BINARY_PATH=$2
COMPLETION_SCRIPT=$3
BINARY_PATH_VERSION=$BINARY_PATH.version
source bin/helpers/func.sh
# Check if HEAD is clean, if not abort
if [ -n "$(git status --porcelain)" ]; then
printfe "%s\n" "yellow" "You have uncomitted and/or untracked changes in your working directory."
fi
# Get the current tag checked out to HEAD and hash
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null)
LATEST_COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null)
LATEST_TAG_HASH=$(git rev-list -n 1 --abbrev-commit $LATEST_TAG 2>/dev/null)
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
# If BRANCH is HEAD and latest commit hash equals latest tag hash, we are on a tag and up to date
if [ "$BRANCH" == "HEAD" ] && [ "$LATEST_COMMIT_HASH" == "$LATEST_TAG_HASH" ]; then
BRANCH=$LATEST_TAG
fi
# In case the current head has uncomitted and/or untracked changes, append a postfix to the version saying (dirty)
if [ -n "$(git status --porcelain)" ]; then
POSTFIX=" (dirty)"
fi
# Format a clean version string for the build
VERSION=$(echo "$BRANCH" | sed 's/^v//')
BUILD_DATE=$(date -u '+%Y-%m-%d %H:%M:%S')
printfe "%s\n" "cyan" "Building $BINARY_NAME binary for $BRANCH ($LATEST_COMMIT_HASH)$POSTFIX..."
go build -ldflags="-X 'git.mvl.sh/vleeuwenmenno/sshtunnel/cmd.Version=$BRANCH' -X 'git.mvl.sh/vleeuwenmenno/sshtunnel/cmd.BuildDate=$BUILD_DATE'" -o $BINARY_PATH
if [ $? -ne 0 ]; then
printf "\033[0;31m"
echo "Build failed."
printf "\033[0m"
exit 1
fi
# Put tag and hash in .sshtunnel_version file
echo "$BRANCH" > $BINARY_PATH_VERSION
printfe "%s\n" "cyan" "Generating completion scripts..."
$BINARY_PATH completion bash > $COMPLETION_SCRIPT
printfe "%s\n" "green" "Bash completion script installed to $COMPLETION_SCRIPT."
printfe "%s\n" "green" "Restart or 'source ~/.bashrc' to update your shell."