diff --git a/.github/workflows/heph.yml b/.github/workflows/heph.yml index 0d5ea74f..82f9d806 100644 --- a/.github/workflows/heph.yml +++ b/.github/workflows/heph.yml @@ -14,7 +14,17 @@ jobs: os: [linux, darwin] arch: [arm64, amd64] steps: - - uses: actions/checkout@v4 + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Compute version + id: version + run: | + VERSION=$(.github/workflows/version.sh) + echo "$VERSION" + echo "version=$VERSION\n" >> $GITHUB_OUTPUT - name: Set up Go uses: actions/setup-go@v4 @@ -45,4 +55,6 @@ jobs: CGO_ENABLED: 0 GOOS: ${{ matrix.os }} GOARCH: ${{ matrix.arch }} - run: go build -ldflags "-s -w" -o heph_${{ matrix.os }}_${{ matrix.arch }} + HEPH_VERSION: ${{steps.version.outputs.version}} + run: | + go build -ldflags "-s -w -X internal/hversion.Version=$HEPH_VERSION" -o heph_$HEPH_VERSION_${{ matrix.os }}_${{ matrix.arch }} diff --git a/.github/workflows/version.sh b/.github/workflows/version.sh new file mode 100755 index 00000000..d15b97b3 --- /dev/null +++ b/.github/workflows/version.sh @@ -0,0 +1,31 @@ +#!/bin/bash +set -e + +# Get the most recent tag +latest_tag=$(git describe --tags --abbrev=0 2>/dev/null) + +if [ -z "$latest_tag" ]; then + echo "No tags found" + exit 1 +fi + +# Get the full description of current commit relative to last tag +# --long ensures we always get the commit count and hash +# --always returns hash even if no tags exist +describe=$(git describe --tags --long --always) + +# If we're exactly on a tag, just output the tag +if git describe --tags --exact-match 2>/dev/null >/dev/null; then + echo "$latest_tag" +else + # Parse the describe output + # Format is like: 1.2.3-5-g36e65 + # We want to transform it to: 1.2.3-build.5+g36e65 + + # Extract components using sed + version=$(echo "$describe" | sed -E 's/^(.*)-([0-9]+)-g([0-9a-f]+)$/\1/') + commits=$(echo "$describe" | sed -E 's/^(.*)-([0-9]+)-g([0-9a-f]+)$/\2/') + hash=$(echo "$describe" | sed -E 's/^(.*)-([0-9]+)-g([0-9a-f]+)$/\3/') + + echo "$version-build.$commits+g$hash" +fi \ No newline at end of file diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 67ffbcab..d3fcccfb 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -9,6 +9,8 @@ import ( "runtime/pprof" "sync" + "github.com/hephbuild/heph/internal/hversion" + "github.com/hephbuild/heph/internal/hcore/hlog" "github.com/mattn/go-isatty" "github.com/spf13/cobra" @@ -35,6 +37,7 @@ var rootCmd = &cobra.Command{ TraverseChildren: true, SilenceUsage: true, SilenceErrors: true, + Version: hversion.Version, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if !debug { levelVar.Set(slog.LevelInfo) diff --git a/internal/hversion/version.go b/internal/hversion/version.go new file mode 100644 index 00000000..cf3e24bc --- /dev/null +++ b/internal/hversion/version.go @@ -0,0 +1,3 @@ +package hversion + +const Version = "dev"