Skip to content

Commit

Permalink
add version
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed Dec 29, 2024
1 parent 3e39526 commit 028cb11
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
19 changes: 17 additions & 2 deletions .github/workflows/heph.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@ jobs:
os: [linux, darwin]
arch: [arm64, amd64]
steps:
- uses: actions/checkout@v4
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup tmate session
uses: mxschmitt/action-tmate@v3

- name: Compute version
id: version
run: |
VERSION=$(.github/workflows/version.sh)
echo "$VERSION"
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Set up Go
uses: actions/setup-go@v4
Expand Down Expand Up @@ -45,4 +58,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 }}
31 changes: 31 additions & 0 deletions .github/workflows/version.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions internal/hversion/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package hversion

const Version = "dev"

0 comments on commit 028cb11

Please sign in to comment.