From a178c7be423ad8e34df189bf7b4c05f393f3395b Mon Sep 17 00:00:00 2001 From: Yukun Wang Date: Thu, 25 Jul 2024 00:27:17 +0800 Subject: [PATCH] Support multi-arch release (#78) This changes the release process to build and upload binaries for multiple architectures. This also adds a `version` command to the cli. --------- Co-authored-by: Thibault Richard --- .github/workflows/release.yml | 43 ++++++++++++++++++----------------- .goreleaser.yaml | 29 +++++++++++++++++++++++ main.go | 14 ++++++++++++ 3 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 .goreleaser.yaml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 57e051c..30b8bfa 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,31 +8,32 @@ jobs: 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 - - run: go test -v ./... - - run: go build -v . - - run: ./test.sh + fetch-depth: 0 - - name: Create release - uses: actions/create-release@v1 - id: release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Set up Go + uses: actions/setup-go@v5 with: - tag_name: ${{ github.ref }} - release_name: ${{ github.ref }} - draft: false + 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: ./test.sh - - 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 }} 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..b4aa28e --- /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:' diff --git a/main.go b/main.go index 8844f1d..b63410a 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\nGitCommit: %s\nBuildDate: %s\n", buildVersion, buildCommit, buildDate) +}