Skip to content

Commit 164823c

Browse files
committed
ci(ci): ci updated, added commit date and date for version cmd
1 parent eb317ad commit 164823c

3 files changed

Lines changed: 65 additions & 26 deletions

File tree

.github/workflows/ci.yml

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test, Release
1+
name: Test, Build and Release
22

33
on:
44
push:
@@ -17,6 +17,8 @@ jobs:
1717
steps:
1818
- name: Checkout code
1919
uses: actions/checkout@v3
20+
with:
21+
fetch-depth: 0
2022

2123
- name: Set up Go
2224
uses: actions/setup-go@v4
@@ -26,7 +28,7 @@ jobs:
2628
- name: Ensure go mod tidy has zero output
2729
run: go mod tidy -v && git diff --exit-code
2830

29-
- name: Esure gofumpt has zero output
31+
- name: Ensure gofumpt has zero output
3032
run: |
3133
go install mvdan.cc/gofumpt@latest
3234
gofumpt -l -w .
@@ -39,6 +41,28 @@ jobs:
3941
skip-pkg-cache: true
4042
args: --issues-exit-code=0
4143

44+
- name: Build with version info
45+
run: |
46+
TAG=$(git describe --tags --always)
47+
COMMIT=$(git rev-parse --short HEAD)
48+
DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
49+
50+
go build -ldflags="\
51+
-X github.com/apiqube/cli/cmd/qube.version=$TAG \
52+
-X github.com/apiqube/cli/cmd/qube.commit=$COMMIT \
53+
-X github.com/apiqube/cli/cmd/qube.date=$DATE" \
54+
-o qube ./cmd/qube
55+
56+
./qube version # Проверка версии
57+
mkdir -p bin
58+
mv qube bin/
59+
60+
- name: Upload artifact
61+
uses: actions/upload-artifact@v3
62+
with:
63+
name: qube-binaries
64+
path: bin/qube
65+
4266
release:
4367
needs: build-and-test
4468
runs-on: ubuntu-latest
@@ -51,6 +75,8 @@ jobs:
5175
steps:
5276
- name: Checkout code
5377
uses: actions/checkout@v3
78+
with:
79+
fetch-depth: 0
5480

5581
- name: Set up Go
5682
uses: actions/setup-go@v4
@@ -74,17 +100,19 @@ jobs:
74100
echo "tag=$TAG"
75101
echo "tag=$TAG" >> $GITHUB_OUTPUT
76102
77-
- name: Create GitHub Release (if not exists)
78-
env:
79-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
80-
VERSION: ${{ steps.get_tag.outputs.tag }}
103+
- name: Build release binaries
81104
run: |
82-
if gh release view "$VERSION" >/dev/null 2>&1; then
83-
echo "Release $VERSION already exists, skipping creation."
84-
else
85-
gh release create "$VERSION" --generate-notes
86-
fi
87-
105+
TAG=$(git describe --tags --always)
106+
COMMIT=$(git rev-parse --short HEAD)
107+
DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
108+
109+
go build -ldflags="\
110+
-X github.com/apiqube/cli/cmd/qube.version=$TAG \
111+
-X github.com/apiqube/cli/cmd/qube.commit=$COMMIT \
112+
-X github.com/apiqube/cli/cmd/qube.date=$DATE" \
113+
-o qube ./cmd/qube
114+
115+
./qube version
88116
89117
- name: Create GitHub Release (if not exists)
90118
env:
@@ -96,6 +124,4 @@ jobs:
96124
gh release create "$VERSION" --generate-notes
97125
else
98126
echo "No new tag created, skipping release step."
99-
fi
100-
101-
127+
fi

.goreleaser.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
builds:
2-
- id: "cli"
2+
- id: "qube"
33
dir: ./cmd/qube
44
main: .
55
binary: "qube"
6-
goos:
7-
- linux
8-
- darwin
9-
- windows
10-
goarch:
11-
- amd64
12-
- arm64
6+
ldflags:
7+
- -s -w
8+
- -X github.com/apiqube/cli/cmd/cli.version={{.Version}}
9+
- -X github.com/apiqube/cli/cmd/cli.commit={{.Commit}}
10+
- -X github.com/apiqube/cli/cmd/cli.date={{.Date}}
11+
goos: [linux, darwin, windows]
12+
goarch: [amd64, arm64]

cmd/cli/version.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,32 @@ package cli
22

33
import (
44
"fmt"
5-
65
"github.com/spf13/cobra"
76
)
87

9-
var version = "dev"
8+
var (
9+
version = "dev"
10+
commit = ""
11+
date = ""
12+
)
1013

1114
var versionCmd = &cobra.Command{
1215
Use: "version",
1316
Short: "Print the version number",
1417
SilenceUsage: true,
1518
SilenceErrors: true,
1619
Run: func(cmd *cobra.Command, args []string) {
17-
fmt.Println("Qube CLI", version)
20+
var data = fmt.Sprintf("Qube CLI\nVersion: %s", version)
21+
22+
if commit != "" {
23+
data += fmt.Sprintf("Commit: %s\n", commit)
24+
}
25+
26+
if date != "" {
27+
data += fmt.Sprintf("Date: %s\n", date)
28+
}
29+
30+
fmt.Println(data)
1831
},
1932
}
2033

0 commit comments

Comments
 (0)