Skip to content

Commit

Permalink
Add version at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
obierlaire committed Jun 13, 2023
1 parent bba88cf commit 896e9ad
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ before:
# You may remove this if you don't use go modules.
- go mod tidy
builds:
- env:
- id: carbonifer
ldflags:
- "-s -w -X main.version={{.Version}}"
env:
- CGO_ENABLED=0
goos:
- linux
Expand Down
24 changes: 23 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
APP_NAME = $(shell basename $(shell pwd))
VERSION := $(shell git describe --tags)

.PHONY: all

dependencies:
@echo "Downloading dependencies..."
Expand All @@ -14,7 +17,26 @@ test:

build:
@echo "Building..."
@go build -o bin/$(APP_NAME)
go build -ldflags "-X main.Version=${VERSION}" -o bin/$(APP_NAME)

# Example make tag TAG=v0.0.1
tag:
@echo "Tagging..."
@if [ "$(TAG)" == "" ]; then \
echo "Please set the tag name. For example: make tag TAG=v1.0.0"; \
exit 1;\
fi
git tag -as $(TAG) -m "$(TAG) ($(shell date -u '+%Y-%m-%d %H:%M:%S')))"
git push upstream $(TAG)

untag:
@echo "Untagging..."
@if [ "$(TAG)" == "" ]; then \
echo "Please set the tag name. For example: make tag TAG=v1.0.0"; \
exit 1;\
fi
git tag -d $(TAG)
git push upstream :refs/tags/$(TAG)


all: dependencies lint test build
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package main

import "github.com/carboniferio/carbonifer/cmd"

var version = "dev"

func main() {
cmd.RootCmd.Version = version
cmd.Execute()
}

0 comments on commit 896e9ad

Please sign in to comment.