Skip to content

Commit eb87c95

Browse files
authored
feat(cli): Show GitHash for manager-api in branch v2.3 (backport #1162) (#1181)
* fix: correct Version and GitHash output for manager-api command (#1162) * bug: fix Version and add GitHash for manager-api command Signed-off-by: imjoey <majunjiev@gmail.com> * feat: git hash support generating .githash for apache release Signed-off-by: imjoey <majunjiev@gmail.com> * feat: Add testcase for the new githash info Signed-off-by: imjoey <majunjiev@gmail.com> * feat: add test case for .githash content validation Signed-off-by: imjoey <majunjiev@gmail.com> * feat: Remove git command dependency for getting git hash Signed-off-by: imjoey <majunjiev@gmail.com> * feat: set VERSION to 2.3 in branch v2.3 Signed-off-by: imjoey <majunjiev@gmail.com>
1 parent 3ba3f7b commit eb87c95

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ output
4545
default.etcd
4646
api/build-tools/apisix
4747
/*.zip
48+
.githash

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ endif
9595
.PHONY: release-src
9696
release-src:
9797
git clean -Xdf
98+
rm -f ./.githash && git log --pretty=format:"%h" -1 > ./.githash
9899
tar -zcvf $(RELEASE_SRC).tgz \
99100
--exclude .github \
100101
--exclude .git \

api/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.2
1+
2.3

api/build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export ENV=local
2020
pwd=`pwd`
2121

2222
VERSION=$(cat ./api/VERSION)
23+
GITHASH=$(cat ./.githash 2> /dev/null || HASH="ref: HEAD"; while [[ $HASH == ref\:* ]]; do HASH="$(cat ".git/$(echo $HASH | cut -d \ -f 2)")"; done; echo ${HASH:0:7})
2324

2425
rm -rf output && mkdir -p output/conf && mkdir -p output/dag-to-lua
2526

@@ -31,7 +32,7 @@ if [[ ! -f "dag-to-lua-1.1/lib/dag-to-lua.lua" ]]; then
3132
fi
3233

3334
# build
34-
cd ./api && go build -o ../output/manager-api -ldflags "-X github.com/apisix/manager-api/cmd.Version=${VERSION}" ./cmd/manager && cd ..
35+
cd ./api && go build -o ../output/manager-api -ldflags "-X github.com/apisix/manager-api/cmd.Version=${VERSION} -X github.com/apisix/manager-api/cmd.GitHash=${GITHASH}" ./cmd/manager && cd ..
3536

3637
cp ./api/conf/schema.json ./output/conf/schema.json
3738
cp ./api/conf/conf.yaml ./output/conf/conf.yaml

api/cmd/managerapi.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ import (
3636
"github.com/spf13/cobra"
3737
)
3838

39-
var Version string
39+
var (
40+
Version string
41+
GitHash string
42+
)
4043

4144
func printInfo() {
4245
fmt.Fprint(os.Stdout, "The manager-api is running successfully!\n\n")
4346
fmt.Fprintf(os.Stdout, "%-8s: %s\n", "Version", Version)
47+
fmt.Fprintf(os.Stdout, "%-8s: %s\n", "GitHash", GitHash)
4448
fmt.Fprintf(os.Stdout, "%-8s: %s:%d\n", "Listen", conf.ServerHost, conf.ServerPort)
4549
fmt.Fprintf(os.Stdout, "%-8s: %s\n", "Loglevel", conf.ErrorLogLevel)
4650
fmt.Fprintf(os.Stdout, "%-8s: %s\n\n", "Logfile", conf.ErrorLogPath)

api/test/shell/cli_test.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
set -ex
2121
VERSION=$(cat ./VERSION)
2222

23+
# test content in .githash
24+
if [[ -f ../.githash ]]; then
25+
GITHASH=$(cat ../.githash)
26+
if [[ ! $GITHASH =~ ^[a-z0-9]{7}$ ]]; then
27+
echo "failed: verify .githash content failed"
28+
exit 1
29+
fi
30+
else
31+
GITHASH=$(HASH="ref: HEAD"; while [[ $HASH == ref\:* ]]; do HASH="$(cat "../.git/$(echo $HASH | cut -d \ -f 2)")"; done; echo ${HASH:0:7})
32+
fi
33+
2334
clean_up() {
2435
git checkout conf/conf.yaml
2536
}
@@ -40,7 +51,7 @@ clean_logfile() {
4051
trap clean_up EXIT
4152

4253
export GO111MODULE=on
43-
go build -o ./manager-api -ldflags "-X github.com/apisix/manager-api/cmd.Version=${VERSION}" ./cmd/manager
54+
go build -o ./manager-api -ldflags "-X github.com/apisix/manager-api/cmd.Version=${VERSION} -X github.com/apisix/manager-api/cmd.GitHash=${GITHASH}" ./cmd/manager
4455

4556
# default level: warn, path: logs/error.log
4657

@@ -134,6 +145,11 @@ if [[ `grep -c "${VERSION}" ${STDOUT}` -ne '1' ]]; then
134145
exit 1
135146
fi
136147

148+
if [[ `grep -c "${GITHASH}" ${STDOUT}` -ne '1' ]]; then
149+
echo "failed: the manager server didn't show started info"
150+
exit 1
151+
fi
152+
137153
if [[ `grep -c "${LOGLEVEL}" ${STDOUT}` -ne '1' ]]; then
138154
echo "failed: the manager server didn't show started info"
139155
exit 1

0 commit comments

Comments
 (0)