From 22d3f1a4f9828b55fac3fcaa4e7659afdcb5b1d5 Mon Sep 17 00:00:00 2001 From: Benson Wong Date: Sat, 14 Dec 2024 09:53:13 -0800 Subject: [PATCH] Change versioning to use git commits counts instead of semver - less work for me - more frequent releases --- .github/workflows/release.yml | 16 ++++++++++------ .goreleaser.yaml | 3 +++ Makefile | 14 ++++++++++++-- llama-swap.go | 10 ++++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ffaef11..271de4f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,8 +2,8 @@ name: goreleaser on: push: - tags: - - '*' + branches: + - main permissions: contents: write @@ -20,14 +20,18 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 + + - name: Get commit count + id: get_commit_count + run: echo "COMMIT_COUNT=$(git rev-list --count HEAD)" >> $GITHUB_OUTPUT + - name: Run GoReleaser uses: goreleaser/goreleaser-action@v6 with: - # either 'goreleaser' (default) or 'goreleaser-pro' distribution: goreleaser - # 'latest', 'nightly', or a semver - version: '~> v2' - args: release --clean + version: latest + args: release --clean --snapshot env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GORELEASER_CURRENT_TAG: v${{ steps.get_commit_count.outputs.COMMIT_COUNT }} diff --git a/.goreleaser.yaml b/.goreleaser.yaml index cdb1c67..9c6c680 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,5 +1,8 @@ version: 2 +release: + name_template: "v{{.Env.GORELEASER_CURRENT_TAG}}" + builds: - env: - CGO_ENABLED=0 diff --git a/Makefile b/Makefile index 47c8999..f3c5216 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,16 @@ APP_NAME = llama-swap BUILD_DIR = build +# Get the current Git hash +GIT_HASH := $(shell git rev-parse --short HEAD) +ifneq ($(shell git status --porcelain),) + # There are untracked changes + GIT_HASH := $(GIT_HASH)+ +endif + +# Get the build number from the commit count on the main branch +COMMIT_COUNT := $(shell git rev-list --count HEAD) + # Default target: Builds binaries for both OSX and Linux all: mac linux simple-responder @@ -18,12 +28,12 @@ test-all: # Build OSX binary mac: @echo "Building Mac binary..." - GOOS=darwin GOARCH=arm64 go build -o $(BUILD_DIR)/$(APP_NAME)-darwin-arm64 + GOOS=darwin GOARCH=arm64 go build -ldflags="-X main.GIT_HASH=${GIT_HASH} -X main.COMMIT_COUNT=${COMMIT_COUNT}" -o $(BUILD_DIR)/$(APP_NAME)-darwin-arm64 # Build Linux binary linux: @echo "Building Linux binary..." - GOOS=linux GOARCH=amd64 go build -o $(BUILD_DIR)/$(APP_NAME)-linux-amd64 + GOOS=linux GOARCH=amd64 go build -ldflags="-X main.GIT_HASH=${GIT_HASH} -X main.COMMIT_COUNT=${COMMIT_COUNT}" -o $(BUILD_DIR)/$(APP_NAME)-linux-amd64 # for testing proxy.Process simple-responder: diff --git a/llama-swap.go b/llama-swap.go index b7444d8..88e10f3 100644 --- a/llama-swap.go +++ b/llama-swap.go @@ -9,13 +9,23 @@ import ( "github.com/mostlygeek/llama-swap/proxy" ) +// see Makefile which injects new values at build time +var GIT_HASH string = "abcd1234" +var COMMIT_COUNT string = "0-dev" + func main() { // Define a command-line flag for the port configPath := flag.String("config", "config.yaml", "config file name") listenStr := flag.String("listen", ":8080", "listen ip/port") + showVersion := flag.Bool("version", false, "show version of build") flag.Parse() // Parse the command-line flags + if *showVersion { + fmt.Printf("version: v%s (%s)\n", COMMIT_COUNT, GIT_HASH) + os.Exit(0) + } + config, err := proxy.LoadConfig(*configPath) if err != nil { fmt.Printf("Error loading config: %v\n", err)