Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 41 additions & 21 deletions build_and_release.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#!/bin/bash

# Exit early if a single command fails
set -e

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"

# Add color utilities.
# TODO: Do we need to check for color support if a self-hosted runner happens to not support color?
source "${SCRIPT_DIR}"/utils.sh

# An array containing all GOOS-es to build the binaries for.
platforms=(
darwin-amd64
darwin-arm64
Expand All @@ -17,50 +26,53 @@ platforms=(
)

if [[ "$RELEASE_ANDROID" == "true" ]]; then
platforms+=("android-amd64")
platforms+=("android-arm64")
fi

# We must know the android sdk version to build for android.
if [[ "$RELEASE_ANDROID" == "true" && -z "$ANDROID_SDK_VERSION" ]]; then
echo "error: Cannot build for android without android_sdk_version." >&2
exit 1
fi
# We must have `ANDROID_SDK_VERSION` and `ANDROID_NDK_HOME` set to build for android.
# The latter is available by default on GitHub hosted runners, but not necessarily the former.
if [[ -z "$ANDROID_SDK_VERSION" ]]; then
fail "Cannot build for Android without ANDROID_SDK_VERSION environment variable!"
elif [[ ! -d "$ANDROID_NDK_HOME" ]]; then
fail "Cannot build for Android without ANDROID_NDK_HOME environment variable!"
fi
Comment on lines +31 to +35

# We must have `ANDROID_NDK_HOME` set to build for android.
# This will be available by default on GitHub hosted runners.
if [[ "$RELEASE_ANDROID" == "true" && ! -d "$ANDROID_NDK_HOME" ]]; then
echo "error: Cannot build for android without android_ndk_home." >&2
exit 1
platforms+=("android-amd64" "android-arm64")
fi

prerelease=""
# TODO: Do we want to allow users to set `--prerelease` via a workflow flag?
if [[ $GH_RELEASE_TAG = *-* ]]; then
info "Marking release as not production-ready..."
prerelease="--prerelease"
fi

draft_release=""
if [[ "$DRAFT_RELEASE" = "true" ]]; then
info "Marking release as draft..."
draft_release="--draft"
fi

if [ -n "$GH_EXT_BUILD_SCRIPT" ]; then
echo "invoking build script override $GH_EXT_BUILD_SCRIPT"
info "Invoking build script override: $GH_EXT_BUILD_SCRIPT"
./"$GH_EXT_BUILD_SCRIPT" "$GH_RELEASE_TAG"
else
# Create build for individual platforms, ensuring they are supported
IFS=$'\n' read -d '' -r -a supported_platforms < <(go tool dist list) || true

for p in "${platforms[@]}"; do
goos="${p%-*}"
goarch="${p#*-}"
if [[ " ${supported_platforms[*]} " != *" ${goos}/${goarch} "* ]]; then
echo "warning: skipping unsupported platform $p" >&2
warn "Skipping unsupported platform: $p"
continue
fi

# Add .exe suffix on windows
ext=""
if [ "$goos" = "windows" ]; then
ext=".exe"
fi

path="dist/${p}${ext}"

cc=""
cgo_enabled="${CGO_ENABLED:-0}"
if [ "$goos" = "android" ]; then
Expand All @@ -72,10 +84,18 @@ else
cgo_enabled="1"
fi
fi
GOOS="$goos" GOARCH="$goarch" CGO_ENABLED="$cgo_enabled" CC="$cc" go build -trimpath -ldflags="-s -w" -o "dist/${p}${ext}" "${GO_BUILD_OPTIONS}"

if GOOS="$goos" GOARCH="$goarch" CGO_ENABLED="$cgo_enabled" CC="$cc" \
go build -trimpath -ldflags="-s -w" -o "${path}" "${GO_BUILD_OPTIONS}"; then
success "Successfully created binary for ${goos}/${goarch} at ${path}!"
Comment thread
Bertie690 marked this conversation as resolved.
else
fail "Error creating binary for ${goos}/${goarch}!" $?
fi

done
fi

# TODO: We can likely rework this to use `readarray` or just compacting the glob directly with `nullglob`
assets=()
for f in dist/*; do
if [ -f "$f" ]; then
Expand All @@ -84,20 +104,20 @@ for f in dist/*; do
done

if [ "${#assets[@]}" -eq 0 ]; then
echo "error: no files found in dist/*" >&2
exit 1
fail "No executable files found in dist/!"
fi

if [ -n "$GPG_FINGERPRINT" ]; then
shasum -a 256 "${assets[@]}" > checksums.txt
gpg --output checksums.txt.sig --detach-sign checksums.txt
assets+=(checksums.txt checksums.txt.sig)
success "Successfully signed binaries!"
fi

if gh release view "$GH_RELEASE_TAG" >/dev/null; then
echo "uploading assets to an existing release..."
gh release upload "$GH_RELEASE_TAG" --clobber -- "${assets[@]}"
success "Uploaded assets to existing release ${GH_RELEASE_TAG}!"
else
echo "creating release and uploading assets..."
gh release create "$GH_RELEASE_TAG" $prerelease $draft_release --title="${GH_RELEASE_TITLE_PREFIX} ${GH_RELEASE_TAG#v}" --generate-notes -- "${assets[@]}"
success "Created release ${GH_RELEASE_TAG} and uploaded assets!"
fi
51 changes: 51 additions & 0 deletions utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#! /bin/bash
# Assorted color utilities for build_and_release.sh.

# Make text orange.
orange () {
printf "\e[33m%s\e[0m" "$1"
}

# Make text red.
red () {
printf "\e[31m%s\e[0m" "$1"
}

# Make text green.
green () {
printf "\e[32m%s\e[0m" "$1"
}

# Make text blue.
blue () {
printf "\e[34m%s\e[0m" "$1"
}

# Annotate text with a blue [...].
info () {
printf "[$(blue '...')] %s\n" "$1"
}

# Annotate text with a green [✔].
success () {
# Extra space used to avoid the emoji clipping into the closing bracket
printf "[$(green '✔ ')] %s\n" "$1"
}

# Annotate text with an orange [!!!] and send it to stderr.
warn () {
printf "[$(orange '!!!')] %s\n" "$1" >&2
}
Comment on lines +24 to +38

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually a really good catch, seen as i copied the utils directly from one of my personal scripts.


# Annotate text with a red [✗] and send it to stderr.
error () {
printf "[$(red '✗')] %s\n" "$1" >&2
}
Comment on lines +40 to +43

# Annotate text with a red [✗] and send it to stderr,
# then exit with the specified non-zero error code.
# Defaults to error code 1 if not provided.
fail () {
error "$1"
exit "${2:-1}"
}
Comment thread
Bertie690 marked this conversation as resolved.