diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 15400aa..0ae5857 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -18,16 +18,46 @@ on: push: tags: - "*" + pull_request: + workflow_dispatch: jobs: + build: + runs-on: ubuntu-latest + if: (!startsWith(github.ref, 'refs/tags/')) + strategy: + matrix: + version: ['1.18'] + steps: + - name: Checkout repo + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Setup Golang + uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.version }} + check-latest: true + cache: true + - name: Build and package with GoReleaser + uses: goreleaser/goreleaser-action@v3 + with: + distribution: goreleaser + version: latest + args: release --clean + env: + GITHUB_TOKEN: ${{ github.token }} + - uses: actions/upload-artifact@v4 + with: + name: dist + path: dist/ + release: runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/tags/') strategy: matrix: version: ['1.18'] - env: - GORELEASER_CURRENT_TAG: ${{ github.ref_name }} steps: - name: Checkout repo uses: actions/checkout@v3 diff --git a/.goreleaser.yaml b/.goreleaser.yaml index a3e59b3..365bfd8 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -25,6 +25,8 @@ builds: - linux - windows - darwin + ldflags: + - '-s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.builtBy=goreleaser' archives: - name_template: >- {{ .ProjectName }}_ diff --git a/Makefile b/Makefile index 69d08ff..314a11e 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,13 @@ .EXPORT_ALL_VARIABLES: +VERSION := $(shell git describe --abbrev=0 --tags | tr -d v) +COMMIT := $(shell git rev-parse --short HEAD) +LDFLAGS := -X 'main.version=$(VERSION)' \ + -X 'main.commit=$(COMMIT)' + .PHONY: build build: - go build -o dist/yamlfmt ./cmd/yamlfmt + go build -ldflags "$(LDFLAGS)" -o dist/yamlfmt ./cmd/yamlfmt .PHONY: test test: @@ -43,7 +48,7 @@ endif .PHONY: install install: - go install ./cmd/yamlfmt + go install -ldflags "$(LDFLAGS)" ./cmd/yamlfmt .PHONY: install_tools install_tools: diff --git a/cmd/yamlfmt/main.go b/cmd/yamlfmt/main.go index e5d8f53..097c7bf 100644 --- a/cmd/yamlfmt/main.go +++ b/cmd/yamlfmt/main.go @@ -26,7 +26,10 @@ import ( "github.com/google/yamlfmt/internal/logger" ) -var version string = "0.12.0" +var ( + version = "dev" + commit = "none" +) func main() { if err := run(); err != nil { @@ -41,7 +44,7 @@ func run() error { flag.Parse() if *flagVersion { - fmt.Printf("%s\n", version) + fmt.Printf("yamlfmt %s (%s)\n", version, commit) return nil }