Skip to content

Commit 4a7f726

Browse files
committed
Add version command and inject version at build time
1 parent 84b5dc4 commit 4a7f726

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

.github/workflows/release.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,19 @@ jobs:
4040
with:
4141
go-version: '1.21'
4242

43+
- name: Get version
44+
id: version
45+
run: |
46+
VERSION=${GITHUB_REF_NAME:-dev}
47+
echo "version=$VERSION" >> $GITHUB_OUTPUT
48+
4349
- name: Build binary
4450
env:
4551
GOOS: ${{ matrix.goos }}
4652
GOARCH: ${{ matrix.goarch }}
4753
CGO_ENABLED: '0'
4854
run: |
49-
go build -ldflags="-s -w" -o git-credential-azure-cli-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.suffix }} .
55+
go build -ldflags="-s -w -X main.version=${{ steps.version.outputs.version }}" -o git-credential-azure-cli-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.suffix }} .
5056
5157
- name: Upload artifact
5258
uses: actions/upload-artifact@v4

main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ import (
4141
"github.com/spf13/cobra"
4242
)
4343

44+
// Version information (set via ldflags at build time)
45+
var version = "dev"
46+
4447
var defaultAllowedDomains = []string{"visualstudio.com", "dev.azure.com"}
4548

4649
// Resource overrides for hosts that need a different token resource.
@@ -463,8 +466,18 @@ Usage:
463466
},
464467
}
465468

469+
// Version command
470+
var versionCmd = &cobra.Command{
471+
Use: "version",
472+
Short: "Print the version number",
473+
Run: func(cmd *cobra.Command, args []string) {
474+
fmt.Println(version)
475+
},
476+
}
477+
466478
rootCmd.AddCommand(storeCmd)
467479
rootCmd.AddCommand(eraseCmd)
480+
rootCmd.AddCommand(versionCmd)
468481

469482
if err := rootCmd.Execute(); err != nil {
470483
os.Exit(1)

0 commit comments

Comments
 (0)