diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 57e051c..c3eec2f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,34 +5,44 @@ on: - v* jobs: release: - name: Release runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-go@v3 + - name: Checkout + uses: actions/checkout@v4 with: - go-version: 1.22.x - - run: go mod download + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: "1.22" + id: go + + - name: Cache Go Modules + uses: actions/cache@v4 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - run: go test -v ./... - - run: go build -v . - run: ./test.sh - - name: Create release - uses: actions/create-release@v1 - id: release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: ${{ github.ref }} - draft: false + - name: Get the version + id: get_version + run: | + VERSION=${GITHUB_REF/refs\/tags\//} + if ${{github.event_name == 'workflow_dispatch'}}; then + VERSION=$(git describe --abbrev=0 --tags) + fi + echo "VERSION=${VERSION}" >> "$GITHUB_OUTPUT" - - name: Upload artifacts - uses: actions/upload-release-asset@v1.0.1 + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v5 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + VERSION: ${{ steps.get_version.outputs.VERSION }} with: - upload_url: ${{ steps.release.outputs.upload_url }} - asset_path: ./crd-ref-docs - asset_name: crd-ref-docs - asset_content_type: application/octet-stream + version: '~> v1' + args: release --clean diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..521227c --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,29 @@ +before: + hooks: + - go mod tidy +builds: + - main: ./main.go + goos: + - linux + - windows + - darwin + ldflags: -s -w -X main.buildVersion={{.Tag}} -X main.buildCommit={{.ShortCommit}} -X main.buildDate={{.Date}} +archives: + - name_template: >- + {{- .ProjectName }}_ + {{- .Version }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + files: + - LICENSE + - README.md +checksum: + name_template: 'checksums.txt' +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' \ No newline at end of file diff --git a/main.go b/main.go index 8844f1d..9900ea1 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ package main import ( + "fmt" "os" "strings" "time" @@ -36,9 +37,12 @@ func main() { Use: "crd-ref-docs", Short: "Generate CRD reference documentation", SilenceUsage: true, + Version: version(), RunE: doRun, } + cmd.SetVersionTemplate("{{ .Version}}\n") + cmd.Flags().StringVar(&args.LogLevel, "log-level", "INFO", "Log level") cmd.Flags().StringVar(&args.Config, "config", "config.yaml", "Path to config file") cmd.Flags().StringVar(&args.SourcePath, "source-path", "", "Path to source directory containing CRDs") @@ -136,3 +140,13 @@ func initLogging(level string) { zap.ReplaceGlobals(logger.Named("crd-ref-docs")) zap.RedirectStdLog(logger.Named("stdlog")) } + +var ( + buildVersion string + buildDate string + buildCommit string +) + +func version() string { + return fmt.Sprintf("Version: %s, Commit: %s, BuildDate: %s", buildVersion, buildCommit, buildDate) +}