ci: embed proper version to bin

This commit is contained in:
Nurul Huda (Apon) 2026-06-11 00:18:21 +06:00
parent 0752b1b487
commit 31c8f9b152
No known key found for this signature in database
GPG Key ID: 5D3F1DE2855A2F79
2 changed files with 26 additions and 9 deletions

View File

@ -157,7 +157,10 @@ jobs:
run: bash tools/synctemplate
- name: Build release binary
run: zig build release-${{ matrix.target.name }}
run: |
BIN_VERSION=$(bash tools/version --resolve --with-sha "${{ needs.resolve-version.outputs.version }}")
echo "Embedding binary version: $BIN_VERSION"
zig build release-${{ matrix.target.name }} -Dversion="$BIN_VERSION"
- name: Prepare artifact
shell: bash

View File

@ -13,6 +13,11 @@
# ./version --resolve [--from=commits|tag]
# Print the resolved full version to stdout and exit
# (does not modify any files). Defaults to --from=commits.
# ./version --resolve --with-sha
# As above, but append "+<short-sha>" build metadata
# to pre-release versions (for embedding in the binary
# via -Dversion). Package versions must stay clean, so
# this is opt-in.
# ./version Equivalent to --from=commits (stamp files)
#
# The base version (everything before the pre-release number) is read from
@ -40,10 +45,12 @@ fi
EXPLICIT_VERSION=""
FROM="commits"
RESOLVE_ONLY=0
WITH_SHA=0
for arg in "$@"; do
case "$arg" in
--resolve) RESOLVE_ONLY=1 ;;
--with-sha) WITH_SHA=1 ;;
--from=commits) FROM="commits" ;;
--from=tag) FROM="tag" ;;
--commit) FROM="commits" ;; # backwards-compatible alias
@ -62,11 +69,13 @@ done
# Compute the base (version without the pre-release number)
# Strip a trailing ".<number>" pre-release counter if present.
# 0.1.0-dev -> 0.1.0-dev
# 0.1.0-dev.123 -> 0.1.0-dev
# 0.1.0 -> 0.1.0 (no -dev, left as-is)
BASE_VERSION=$(echo "$CURRENT_VERSION" | sed -E 's/(-[a-zA-Z]+)\.[0-9]+$/\1/')
# Strip any "+<build metadata>" suffix, then a trailing ".<number>"
# pre-release counter if present.
# 0.1.0-dev -> 0.1.0-dev
# 0.1.0-dev.123 -> 0.1.0-dev
# 0.1.0-dev.123+abc1234 -> 0.1.0-dev
# 0.1.0 -> 0.1.0 (no -dev, left as-is)
BASE_VERSION=$(echo "$CURRENT_VERSION" | sed -E 's/\+.*$//; s/(-[a-zA-Z]+)\.[0-9]+$/\1/')
# Resolve the new full version
@ -100,6 +109,11 @@ else
NEW_VERSION="${BASE_VERSION}.${N}"
fi
if [ "$WITH_SHA" -eq 1 ] && echo "$NEW_VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+-' && ! echo "$NEW_VERSION" | grep -q '+'; then
SHA=$(git -C "$PROJECT_ROOT" rev-parse --short HEAD 2>/dev/null)
[ -n "$SHA" ] && NEW_VERSION="${NEW_VERSION}+${SHA}"
fi
# --resolve: just print and exit, touching nothing.
if [ "$RESOLVE_ONLY" -eq 1 ]; then
echo "$NEW_VERSION"
@ -110,8 +124,8 @@ echo "Version: $CURRENT_VERSION -> $NEW_VERSION"
# Stamp the new version into all version-bearing files
# Match any X.Y.Z[-pre[.N]] version string for in-place replacement.
VERSION_PATTERN='[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z]+(\.[0-9]+)?)?'
# Match any X.Y.Z[-pre[.N]][+meta] version string for in-place replacement.
VERSION_PATTERN='[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z]+(\.[0-9]+)?)?(\+[0-9A-Za-z.]+)?'
# Root build.zig.zon (.version field only)
sed -i.bak -E "/^[[:space:]]*\.version[[:space:]]*=/s/$VERSION_PATTERN/$NEW_VERSION/" "$BUILD_ZON_PATH"
@ -145,7 +159,7 @@ fi
# ide/vscode/package.json (uses 0.1.N format since VSCode rejects prerelease tags)
VSCODE_PACKAGE_JSON="$PROJECT_ROOT/ide/vscode/package.json"
if [ -f "$VSCODE_PACKAGE_JSON" ]; then
VSCODE_DEV_NUMBER=$(echo "$NEW_VERSION" | grep -oE '[a-zA-Z]+\.([0-9]+)$' | grep -oE '[0-9]+$')
VSCODE_DEV_NUMBER=$(echo "${NEW_VERSION%%+*}" | grep -oE '[a-zA-Z]+\.([0-9]+)$' | grep -oE '[0-9]+$')
if [ -n "$VSCODE_DEV_NUMBER" ]; then
VSCODE_VERSION="0.1.$VSCODE_DEV_NUMBER"
sed -i.bak -E "s/\"version\": \"0\\.1\\.[0-9]+\"/\"version\": \"$VSCODE_VERSION\"/" "$VSCODE_PACKAGE_JSON"