Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Add corso version using ldflags (#1104)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
meain authored Oct 14, 2022
1 parent 2d30f4a commit 69a6fd1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ jobs:
with:
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
uses: goreleaser/goreleaser-action@v3 # TODO(meain): make sure release builds work
with:
Expand All @@ -244,6 +247,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RUDDERSTACK_CORSO_WRITE_KEY: ${{ secrets.RUDDERSTACK_CORSO_WRITE_KEY }}
RUDDERSTACK_CORSO_DATA_PLANE_URL: ${{ secrets.RUDDERSTACK_CORSO_DATA_PLANE_URL }}
CORSO_VERSION: ${{ steps.version.outputs.version }}

- name: Upload assets
uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -306,10 +310,11 @@ jobs:
- uses: actions/checkout@v3

- name: Build Corso Binaries
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 }}'"
./build.sh
--platforms ${{ env.PLATFORMS }}
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 }}' \
-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
- name: Set up QEMU
Expand Down
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist/
corso
1 change: 1 addition & 0 deletions src/.goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ builds:
- goos: windows
goarch: arm64
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.RudderStackDataPlaneURL={{.Env.RUDDERSTACK_CORSO_DATA_PLANE_URL}}'

Expand Down
4 changes: 4 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ BAD_LINT_MSG := "Missing golangci-lint version $(WANTED_LINT_VERSION). Visit $(I

.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
golangci-lint run

Expand Down
14 changes: 6 additions & 8 deletions src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/alcionai/corso/src/pkg/logger"
)

var version = "dev"

// ------------------------------------------------------------------------------------------
// Corso Command
// ------------------------------------------------------------------------------------------
Expand All @@ -33,16 +35,12 @@ var corsoCmd = &cobra.Command{
PersistentPreRunE: config.InitFunc(),
}

// the root-level flags
var (
version bool
)

// Handler for flat calls to `corso`.
// Produces the same output as `corso --help`.
func handleCorsoCmd(cmd *cobra.Command, args []string) error {
if version {
print.Infof(cmd.Context(), "Corso\nversion:\tpre-alpha\n")
v, _ := cmd.Flags().GetBool("version")
if v {
print.Infof(cmd.Context(), "Corso\nversion: "+version)
return nil
}

Expand All @@ -64,7 +62,7 @@ func BuildCommandTree(cmd *cobra.Command) {
// want to order flags explicitly
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()
config.AddConfigFlags(cmd)
logger.AddLogLevelFlag(cmd)
Expand Down

0 comments on commit 69a6fd1

Please sign in to comment.