Skip to content

Commit

Permalink
fix: correct Version and GitHash output for manager-api command (#1162)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
imjoey authored Jan 2, 2021
1 parent fa12db8 commit 62d1c43
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ output
default.etcd
api/build-tools/apisix
/*.zip
.githash
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ endif
.PHONY: release-src
release-src:
git clean -Xdf
rm -f ./.githash && git log --pretty=format:"%h" -1 > ./.githash
tar -zcvf $(RELEASE_SRC).tgz \
--exclude .github \
--exclude .git \
Expand Down
2 changes: 1 addition & 1 deletion api/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2
master
3 changes: 2 additions & 1 deletion api/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export ENV=local
pwd=`pwd`

VERSION=$(cat ./api/VERSION)
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})

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

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

# build
cd ./api && go build -o ../output/manager-api -ldflags "-X github.com/apisix/manager-api/cmd.Version=${VERSION}" ./cmd/manager && cd ..
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 ..

cp ./api/conf/schema.json ./output/conf/schema.json
cp ./api/conf/conf.yaml ./output/conf/conf.yaml
Expand Down
6 changes: 5 additions & 1 deletion api/cmd/managerapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ import (
"github.com/spf13/cobra"
)

var Version string
var (
Version string
GitHash string
)

func printInfo() {
fmt.Fprint(os.Stdout, "The manager-api is running successfully!\n\n")
fmt.Fprintf(os.Stdout, "%-8s: %s\n", "Version", Version)
fmt.Fprintf(os.Stdout, "%-8s: %s\n", "GitHash", GitHash)
fmt.Fprintf(os.Stdout, "%-8s: %s:%d\n", "Listen", conf.ServerHost, conf.ServerPort)
fmt.Fprintf(os.Stdout, "%-8s: %s\n", "Loglevel", conf.ErrorLogLevel)
fmt.Fprintf(os.Stdout, "%-8s: %s\n\n", "Logfile", conf.ErrorLogPath)
Expand Down
18 changes: 17 additions & 1 deletion api/test/shell/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
set -ex
VERSION=$(cat ./VERSION)

# test content in .githash
if [[ -f ../.githash ]]; then
GITHASH=$(cat ../.githash)
if [[ ! $GITHASH =~ ^[a-z0-9]{7}$ ]]; then
echo "failed: verify .githash content failed"
exit 1
fi
else
GITHASH=$(HASH="ref: HEAD"; while [[ $HASH == ref\:* ]]; do HASH="$(cat "../.git/$(echo $HASH | cut -d \ -f 2)")"; done; echo ${HASH:0:7})
fi

clean_up() {
git checkout conf/conf.yaml
}
Expand All @@ -40,7 +51,7 @@ clean_logfile() {
trap clean_up EXIT

export GO111MODULE=on
go build -o ./manager-api -ldflags "-X github.com/apisix/manager-api/cmd.Version=${VERSION}" ./cmd/manager
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

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

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

if [[ `grep -c "${GITHASH}" ${STDOUT}` -ne '1' ]]; then
echo "failed: the manager server didn't show started info"
exit 1
fi

if [[ `grep -c "${LOGLEVEL}" ${STDOUT}` -ne '1' ]]; then
echo "failed: the manager server didn't show started info"
exit 1
Expand Down

0 comments on commit 62d1c43

Please sign in to comment.