version: Use linker flag

Embedding the version file is slightly annoying for packaging, because
the release tarball is not a git repository and so "go generate" would
fail. Instead a packager would have to manually create the version.txt
file.
With using a linker flag, the version string has a graceful fallback.
This commit is contained in:
Thorben Günther 2023-02-16 13:06:21 +01:00
parent 8a387ff834
commit 84c4c45d5b
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
4 changed files with 2 additions and 6 deletions

View file

@ -11,7 +11,6 @@ sources:
tasks: tasks:
- test: | - test: |
cd ntfy-alertmanager cd ntfy-alertmanager
go generate ./...
go test -v ./... go test -v ./...
- lint: | - lint: |
cd ntfy-alertmanager cd ntfy-alertmanager

1
.gitignore vendored
View file

@ -1,2 +1 @@
/ntfy-alertmanager /ntfy-alertmanager
version.txt

View file

@ -3,7 +3,7 @@ WORKDIR /app
COPY . . COPY . .
RUN apk add --no-cache git RUN apk add --no-cache git
RUN go generate ./... && go build -o /app/ntfy-alertmanager RUN go build -ldflags="-X main.version=$(git describe --long)" -o /app/ntfy-alertmanager
FROM alpine:latest FROM alpine:latest

View file

@ -19,9 +19,7 @@ import (
"golang.org/x/text/language" "golang.org/x/text/language"
) )
//go:generate sh -c "git describe --long > version.txt" var version = "dev"
//go:embed version.txt
var version string
type receiver struct { type receiver struct {
cfg *config cfg *config