Add corso version using ldflags (#1104)

* Make corso respect --version

* Add proper version information when building

* Use unified version string

$(git describe --exact-match --tags $(git rev-parse HEAD) 2>/dev/null || echo unreleased)-$(git rev-parse --short HEAD)

With tag:    v0.0.0-ceaf04c9
Without tag: unreleased-ceaf04c9

* Add a simple build command to build using Makefile
This commit is contained in:
Abin Simon 2022-10-14 12:20:30 +05:30 committed by GitHub
parent 2d30f4a999
commit 69a6fd1593
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 12 deletions

View File

@ -234,6 +234,9 @@ jobs:
with: with:
go-version-file: src/go.mod go-version-file: src/go.mod
- id: version
run: echo "::set-output name=version::$(git describe --exact-match --tags $(git rev-parse HEAD) 2>/dev/null || echo unreleased)-$(git rev-parse --short HEAD)"
- name: Run GoReleaser - name: Run GoReleaser
uses: goreleaser/goreleaser-action@v3 # TODO(meain): make sure release builds work uses: goreleaser/goreleaser-action@v3 # TODO(meain): make sure release builds work
with: with:
@ -244,6 +247,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RUDDERSTACK_CORSO_WRITE_KEY: ${{ secrets.RUDDERSTACK_CORSO_WRITE_KEY }} RUDDERSTACK_CORSO_WRITE_KEY: ${{ secrets.RUDDERSTACK_CORSO_WRITE_KEY }}
RUDDERSTACK_CORSO_DATA_PLANE_URL: ${{ secrets.RUDDERSTACK_CORSO_DATA_PLANE_URL }} RUDDERSTACK_CORSO_DATA_PLANE_URL: ${{ secrets.RUDDERSTACK_CORSO_DATA_PLANE_URL }}
CORSO_VERSION: ${{ steps.version.outputs.version }}
- name: Upload assets - name: Upload assets
uses: actions/upload-artifact@v3 uses: actions/upload-artifact@v3
@ -306,10 +310,11 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Build Corso Binaries - name: Build Corso Binaries
run: > run: |
export CORSO_BUILD_LDFLAGS="-X 'github.com/alcionai/corso/src/internal/events.RudderStackWriteKey=${{ secrets.RUDDERSTACK_CORSO_WRITE_KEY }}' -X 'github.com/alcionai/corso/src/internal/events.RudderStackDataPlaneURL=${{ secrets.RUDDERSTACK_CORSO_DATA_PLANE_URL }}'" export CORSO_BUILD_LDFLAGS="-X 'github.com/alcionai/corso/src/internal/events.RudderStackWriteKey=${{ secrets.RUDDERSTACK_CORSO_WRITE_KEY }}' \
./build.sh -X 'github.com/alcionai/corso/src/internal/events.RudderStackDataPlaneURL=${{ secrets.RUDDERSTACK_CORSO_DATA_PLANE_URL }}' \
--platforms ${{ env.PLATFORMS }} -X 'github.com/alcionai/corso/src/cli.version=$(git describe --exact-match --tags $(git rev-parse HEAD) 2>/dev/null || echo unreleased)-$(git rev-parse --short HEAD)'"
./build.sh --platforms ${{ env.PLATFORMS }}
# apparently everyone uses this step # apparently everyone uses this step
- name: Set up QEMU - name: Set up QEMU

1
src/.gitignore vendored
View File

@ -1 +1,2 @@
dist/ dist/
corso

View File

@ -13,6 +13,7 @@ builds:
- goos: windows - goos: windows
goarch: arm64 goarch: arm64
ldflags: ldflags:
- -X 'github.com/alcionai/corso/src/cli.version={{.Env.CORSO_VERSION}}'
- -X 'github.com/alcionai/corso/src/internal/events.RudderStackWriteKey={{.Env.RUDDERSTACK_CORSO_WRITE_KEY}}' - -X 'github.com/alcionai/corso/src/internal/events.RudderStackWriteKey={{.Env.RUDDERSTACK_CORSO_WRITE_KEY}}'
- -X 'github.com/alcionai/corso/src/internal/events.RudderStackDataPlaneURL={{.Env.RUDDERSTACK_CORSO_DATA_PLANE_URL}}' - -X 'github.com/alcionai/corso/src/internal/events.RudderStackDataPlaneURL={{.Env.RUDDERSTACK_CORSO_DATA_PLANE_URL}}'

View File

@ -8,6 +8,10 @@ BAD_LINT_MSG := "Missing golangci-lint version $(WANTED_LINT_VERSION). Visit $(I
.PHONY: check-lint check-lint-version lint .PHONY: check-lint check-lint-version lint
build:
go build -o corso -ldflags \
"-X 'github.com/alcionai/corso/src/cli/version.Version=$(shell git describe --exact-match --tags $(git rev-parse HEAD) 2>/dev/null || echo unreleased)-$(shell git rev-parse --short HEAD)'"
lint: check-lint-version lint: check-lint-version
golangci-lint run golangci-lint run

View File

@ -19,6 +19,8 @@ import (
"github.com/alcionai/corso/src/pkg/logger" "github.com/alcionai/corso/src/pkg/logger"
) )
var version = "dev"
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
// Corso Command // Corso Command
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
@ -33,16 +35,12 @@ var corsoCmd = &cobra.Command{
PersistentPreRunE: config.InitFunc(), PersistentPreRunE: config.InitFunc(),
} }
// the root-level flags
var (
version bool
)
// Handler for flat calls to `corso`. // Handler for flat calls to `corso`.
// Produces the same output as `corso --help`. // Produces the same output as `corso --help`.
func handleCorsoCmd(cmd *cobra.Command, args []string) error { func handleCorsoCmd(cmd *cobra.Command, args []string) error {
if version { v, _ := cmd.Flags().GetBool("version")
print.Infof(cmd.Context(), "Corso\nversion:\tpre-alpha\n") if v {
print.Infof(cmd.Context(), "Corso\nversion: "+version)
return nil return nil
} }
@ -64,7 +62,7 @@ func BuildCommandTree(cmd *cobra.Command) {
// want to order flags explicitly // want to order flags explicitly
cmd.PersistentFlags().SortFlags = false cmd.PersistentFlags().SortFlags = false
cmd.Flags().BoolP("version", "v", version, "current version info") cmd.Flags().BoolP("version", "v", false, "current version info")
cmd.PersistentPostRunE = config.InitFunc() cmd.PersistentPostRunE = config.InitFunc()
config.AddConfigFlags(cmd) config.AddConfigFlags(cmd)
logger.AddLogLevelFlag(cmd) logger.AddLogLevelFlag(cmd)