From 253e87d186fb7d9edd26264825987876fd3f8924 Mon Sep 17 00:00:00 2001 From: wang yan Date: Tue, 29 Oct 2019 16:09:54 +0800 Subject: [PATCH] inject ldflags for harbor compiler and linker 1, replace the UIVERSION file with ldflags, which is generarted by make to inject into the UI core. 2, inject additional ldflags for harbor compiler Signed-off-by: wang yan --- Makefile | 35 +++++++++++++------------------ make/photon/core/Dockerfile | 2 +- src/core/api/systeminfo.go | 10 ++------- src/core/main.go | 3 +++ src/pkg/version/version.go | 21 +++++++++++++++++++ tests/integration.sh | 6 ------ tests/resources/Harbor-Util.robot | 6 +++--- 7 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 src/pkg/version/version.go diff --git a/Makefile b/Makefile index 7966618cc..11d68a348 100644 --- a/Makefile +++ b/Makefile @@ -71,6 +71,9 @@ TOOLSPATH=$(BUILDPATH)/tools CORE_PATH=$(BUILDPATH)/src/core PORTAL_PATH=$(BUILDPATH)/src/portal CHECKENVCMD=checkenv.sh +GITCOMMIT := $(shell git rev-parse --short=8 HEAD) +RELEASEVERSION := $(shell cat VERSION) +PKG_PATH=github.com/goharbor/harbor/src/pkg # parameters REGISTRYSERVER= @@ -90,10 +93,6 @@ CHARTFLAG=false VERSIONTAG=dev # for harbor package name PKGVERSIONTAG=dev -# for harbor about dialog -UIVERSIONTAG=dev -VERSIONFILEPATH=$(CURDIR) -VERSIONFILENAME=UIVERSION PREPARE_VERSION_NAME=versions @@ -143,8 +142,10 @@ GODEP=$(GOTEST) -i GOFMT=gofmt -w GOBUILDIMAGE=golang:1.12.12 GOBUILDPATH=/harbor -GOIMAGEBUILDCMD=/usr/local/go/bin/go -GOIMAGEBUILD=$(GOIMAGEBUILDCMD) build -mod vendor +GOIMAGEBUILDCMD=/usr/local/go/bin/go build -mod vendor +GOIMAGEBUILD_COMMON=$(GOIMAGEBUILDCMD) --ldflags "$(ADDITIONAL_LDFLAGS)" +CORE_BASE_LDFLAGS=-X $(PKG_PATH)/version.GitCommit=$(GITCOMMIT) -X $(PKG_PATH)/version.ReleaseVersion=$(RELEASEVERSION) +GOIMAGEBUILD_CORE=$(GOIMAGEBUILDCMD) --ldflags "$(CORE_BASE_LDFLAGS) $(ADDITIONAL_LDFLAGS)" GOBUILDPATH_CORE=$(GOBUILDPATH)/src/core GOBUILDPATH_JOBSERVICE=$(GOBUILDPATH)/src/jobservice GOBUILDPATH_REGISTRYCTL=$(GOBUILDPATH)/src/registryctl @@ -267,9 +268,6 @@ ifeq ($(CHARTFLAG), true) endif export VERSIONS_FOR_PREPARE -ui_version: - @printf $(UIVERSIONTAG) > $(VERSIONFILEPATH)/$(VERSIONFILENAME); - versions_prepare: @echo "$$VERSIONS_FOR_PREPARE" > $(MAKE_PREPARE_PATH)/$(PREPARE_VERSION_NAME) @@ -279,22 +277,22 @@ check_environment: compile_core: @echo "compiling binary for core (golang image)..." @echo $(GOBUILDPATH) - @$(DOCKERCMD) run --rm -v $(BUILDPATH):$(GOBUILDPATH) -w $(GOBUILDPATH_CORE) $(GOBUILDIMAGE) $(GOIMAGEBUILD) -o $(GOBUILDMAKEPATH_CORE)/$(CORE_BINARYNAME) + @$(DOCKERCMD) run --rm -v $(BUILDPATH):$(GOBUILDPATH) -w $(GOBUILDPATH_CORE) $(GOBUILDIMAGE) $(GOIMAGEBUILD_CORE) -o $(GOBUILDMAKEPATH_CORE)/$(CORE_BINARYNAME) @echo "Done." compile_jobservice: @echo "compiling binary for jobservice (golang image)..." - @$(DOCKERCMD) run --rm -v $(BUILDPATH):$(GOBUILDPATH) -w $(GOBUILDPATH_JOBSERVICE) $(GOBUILDIMAGE) $(GOIMAGEBUILD) -o $(GOBUILDMAKEPATH_JOBSERVICE)/$(JOBSERVICEBINARYNAME) + @$(DOCKERCMD) run --rm -v $(BUILDPATH):$(GOBUILDPATH) -w $(GOBUILDPATH_JOBSERVICE) $(GOBUILDIMAGE) $(GOIMAGEBUILD_COMMON) -o $(GOBUILDMAKEPATH_JOBSERVICE)/$(JOBSERVICEBINARYNAME) @echo "Done." compile_registryctl: @echo "compiling binary for harbor registry controller (golang image)..." - @$(DOCKERCMD) run --rm -v $(BUILDPATH):$(GOBUILDPATH) -w $(GOBUILDPATH_REGISTRYCTL) $(GOBUILDIMAGE) $(GOIMAGEBUILD) -o $(GOBUILDMAKEPATH_REGISTRYCTL)/$(REGISTRYCTLBINARYNAME) + @$(DOCKERCMD) run --rm -v $(BUILDPATH):$(GOBUILDPATH) -w $(GOBUILDPATH_REGISTRYCTL) $(GOBUILDIMAGE) $(GOIMAGEBUILD_COMMON) -o $(GOBUILDMAKEPATH_REGISTRYCTL)/$(REGISTRYCTLBINARYNAME) @echo "Done." compile_notary_migrate_patch: @echo "compiling binary for migrate patch (golang image)..." - @$(DOCKERCMD) run --rm -v $(BUILDPATH):$(GOBUILDPATH) -w $(GOBUILDPATH_MIGRATEPATCH) $(GOBUILDIMAGE) $(GOIMAGEBUILD) -o $(GOBUILDMAKEPATH_NOTARY)/$(MIGRATEPATCHBINARYNAME) + @$(DOCKERCMD) run --rm -v $(BUILDPATH):$(GOBUILDPATH) -w $(GOBUILDPATH_MIGRATEPATCH) $(GOBUILDIMAGE) $(GOIMAGEBUILD_COMMON) -o $(GOBUILDMAKEPATH_NOTARY)/$(MIGRATEPATCHBINARYNAME) @echo "Done." compile: check_environment versions_prepare compile_core compile_jobservice compile_registryctl compile_notary_migrate_patch @@ -315,7 +313,7 @@ build: -e CHARTMUSEUMVERSION=$(CHARTMUSEUMVERSION) -e DOCKERIMAGENAME_CHART_SERVER=$(DOCKERIMAGENAME_CHART_SERVER) \ -e NPM_REGISTRY=$(NPM_REGISTRY) -install: compile ui_version build prepare start +install: compile build prepare start package_online: update_prepare_version @echo "packing online package ..." @@ -330,7 +328,7 @@ package_online: update_prepare_version @rm -rf $(HARBORPKG) @echo "Done." -package_offline: update_prepare_version compile ui_version build +package_offline: update_prepare_version compile build @echo "packing offline package ..." @cp -r make $(HARBORPKG) @@ -460,10 +458,6 @@ cleandockercomposefile: @find $(DOCKERCOMPOSEFILEPATH) -maxdepth 1 -name "docker-compose*.yml" -exec rm -f {} \; @find $(DOCKERCOMPOSEFILEPATH) -maxdepth 1 -name "docker-compose*.yml-e" -exec rm -f {} \; -cleanversiontag: - @echo "cleaning version TAG" - @rm -rf $(VERSIONFILEPATH)/$(VERSIONFILENAME) - cleanpackage: @echo "cleaning harbor install package" @if [ -d $(BUILDPATH)/harbor ] ; then rm -rf $(BUILDPATH)/harbor ; fi @@ -473,7 +467,7 @@ cleanpackage: then rm $(BUILDPATH)/harbor-offline-installer-$(VERSIONTAG).tgz ; fi .PHONY: cleanall -cleanall: cleanbinary cleanimage cleandockercomposefile cleanversiontag cleanpackage +cleanall: cleanbinary cleanimage cleandockercomposefile cleanpackage clean: @echo " make cleanall: remove binary, Harbor images, specific version docker-compose" @@ -481,7 +475,6 @@ clean: @echo " make cleanbinary: remove core and jobservice binary" @echo " make cleanimage: remove Harbor images" @echo " make cleandockercomposefile: remove specific version docker-compose" - @echo " make cleanversiontag: cleanpackageremove specific version tag" @echo " make cleanpackage: remove online and offline install package" all: install diff --git a/make/photon/core/Dockerfile b/make/photon/core/Dockerfile index d585a98ee..80add1701 100644 --- a/make/photon/core/Dockerfile +++ b/make/photon/core/Dockerfile @@ -6,7 +6,7 @@ RUN tdnf install sudo tzdata -y >> /dev/null \ && mkdir /harbor/ HEALTHCHECK CMD curl --fail -s http://127.0.0.1:8080/api/ping || exit 1 -COPY ./make/photon/core/harbor_core ./UIVERSION /harbor/ +COPY ./make/photon/core/harbor_core /harbor/ COPY ./src/core/views /harbor/views COPY ./make/migrations /harbor/migrations diff --git a/src/core/api/systeminfo.go b/src/core/api/systeminfo.go index 2f813ede1..f1f7f311b 100644 --- a/src/core/api/systeminfo.go +++ b/src/core/api/systeminfo.go @@ -17,7 +17,6 @@ package api import ( "errors" "fmt" - "io/ioutil" "net/http" "os" "strings" @@ -29,6 +28,7 @@ import ( "github.com/goharbor/harbor/src/core/config" "github.com/goharbor/harbor/src/core/systeminfo" "github.com/goharbor/harbor/src/core/systeminfo/imagestorage" + "github.com/goharbor/harbor/src/pkg/version" ) // SystemInfoAPI handle requests for getting system info /api/systeminfo @@ -37,7 +37,6 @@ type SystemInfoAPI struct { } const defaultRootCert = "/etc/core/ca/ca.crt" -const harborVersionFile = "/harbor/UIVERSION" // SystemInfo models for system info. type SystemInfo struct { @@ -165,12 +164,7 @@ func (sia *SystemInfoAPI) GetGeneralInfo() { // getVersion gets harbor version. func (sia *SystemInfoAPI) getVersion() string { - version, err := ioutil.ReadFile(harborVersionFile) - if err != nil { - log.Errorf("Error occurred getting harbor version: %v", err) - return "" - } - return string(version[:]) + return fmt.Sprintf("%s-%s", version.ReleaseVersion, version.GitCommit) } // Ping ping the harbor core service. diff --git a/src/core/main.go b/src/core/main.go index 2d32c2d57..dfe2f6a66 100755 --- a/src/core/main.go +++ b/src/core/main.go @@ -51,6 +51,7 @@ import ( "github.com/goharbor/harbor/src/pkg/scan/event" "github.com/goharbor/harbor/src/pkg/scheduler" "github.com/goharbor/harbor/src/pkg/types" + "github.com/goharbor/harbor/src/pkg/version" "github.com/goharbor/harbor/src/replication" ) @@ -275,5 +276,7 @@ func main() { log.Fatalf("quota migration error, %v", err) } + log.Infof("Version: %s, Git commit: %s", version.ReleaseVersion, version.GitCommit) beego.Run() + } diff --git a/src/pkg/version/version.go b/src/pkg/version/version.go new file mode 100644 index 000000000..26fee258d --- /dev/null +++ b/src/pkg/version/version.go @@ -0,0 +1,21 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package version + +// ReleaseVersion is the content of VERSION, which contains the target release version, like v1.10.0 +var ReleaseVersion string + +// GitCommit is the git commit id with length of 8 characters +var GitCommit string diff --git a/tests/integration.sh b/tests/integration.sh index 387c664d6..9041c2391 100755 --- a/tests/integration.sh +++ b/tests/integration.sh @@ -74,12 +74,8 @@ echo $container_ip ## --------------------------------------------- Init Version ----------------------------------------------- buildinfo=$(drone build info goharbor/harbor $DRONE_BUILD_NUMBER) echo $buildinfo -git_commit=$(git rev-parse --short=8 HEAD) - # the target release version is the version of next release(RC or GA). It needs to be updated on creating new release branch. target_release_version=$(cat ./VERSION) -# the harbor ui version will be shown in the about dialog. -Harbor_UI_Version=$target_release_version-$git_commit # the harbor package version is for both online and offline installer. # harbor-offline-installer-v1.5.2-build.8.tgz Harbor_Package_Version=$target_release_version-'build.'$DRONE_BUILD_NUMBER @@ -91,14 +87,12 @@ if [[ $DRONE_BRANCH == "master" ]]; then else Harbor_Assets_Version=$target_release_version fi -export Harbor_UI_Version=$Harbor_UI_Version export Harbor_Assets_Version=$Harbor_Assets_Version # the env is for online and offline package. export Harbor_Package_Version=$Harbor_Package_Version export NPM_REGISTRY=$NPM_REGISTRY echo "--------------------------------------------------" -echo "Harbor UI version: $Harbor_UI_Version" echo "Harbor Package version: $Harbor_Package_Version" echo "Harbor Assets version: $Harbor_Assets_Version" echo "--------------------------------------------------" diff --git a/tests/resources/Harbor-Util.robot b/tests/resources/Harbor-Util.robot index 2307a243d..d400d29fd 100644 --- a/tests/resources/Harbor-Util.robot +++ b/tests/resources/Harbor-Util.robot @@ -55,8 +55,8 @@ Package Harbor Offline [Arguments] ${with_notary}=true ${with_clair}=true ${with_migrator}=false ${with_chartmuseum}=true Log To Console \nStart Docker Daemon Start Docker Daemon Locally - Log To Console \n\nmake package_offline NPM_REGISTRY=%{NPM_REGISTRY} VERSIONTAG=%{Harbor_Assets_Version} PKGVERSIONTAG=%{Harbor_Package_Version} UIVERSIONTAG=%{Harbor_UI_Version} NOTARYFLAG=${with_notary} CLAIRFLAG=${with_clair} MIGRATORFLAG=${with_migrator} CHARTFLAG=${with_chartmuseum} HTTPPROXY= - ${rc} ${output}= Run And Return Rc And Output make package_offline VERSIONTAG=%{Harbor_Assets_Version} PKGVERSIONTAG=%{Harbor_Package_Version} UIVERSIONTAG=%{Harbor_UI_Version} NOTARYFLAG=${with_notary} CLAIRFLAG=${with_clair} MIGRATORFLAG=${with_migrator} CHARTFLAG=${with_chartmuseum} HTTPPROXY= + Log To Console \n\nmake package_offline NPM_REGISTRY=%{NPM_REGISTRY} VERSIONTAG=%{Harbor_Assets_Version} PKGVERSIONTAG=%{Harbor_Package_Version} NOTARYFLAG=${with_notary} CLAIRFLAG=${with_clair} MIGRATORFLAG=${with_migrator} CHARTFLAG=${with_chartmuseum} HTTPPROXY= + ${rc} ${output}= Run And Return Rc And Output make package_offline VERSIONTAG=%{Harbor_Assets_Version} PKGVERSIONTAG=%{Harbor_Package_Version} NOTARYFLAG=${with_notary} CLAIRFLAG=${with_clair} MIGRATORFLAG=${with_migrator} CHARTFLAG=${with_chartmuseum} HTTPPROXY= Log To Console ${rc} Log To Console ${output} Should Be Equal As Integers ${rc} 0 @@ -66,7 +66,7 @@ Package Harbor Online Log To Console \nStart Docker Daemon Start Docker Daemon Locally Log To Console \nmake package_online VERSIONTAG=%{Harbor_Assets_Version} PKGVERSIONTAG=%{Harbor_Package_Version} UIVERSIONTAG=%{Harbor_UI_Version} NOTARYFLAG=${with_notary} CLAIRFLAG=${with_clair} MIGRATORFLAG=${with_migrator} CHARTFLAG=${with_chartmuseum} HTTPPROXY= - ${rc} ${output}= Run And Return Rc And Output make package_online VERSIONTAG=%{Harbor_Assets_Version} PKGVERSIONTAG=%{Harbor_Package_Version} UIVERSIONTAG=%{Harbor_UI_Version} NOTARYFLAG=${with_notary} CLAIRFLAG=${with_clair} MIGRATORFLAG=${with_migrator} CHARTFLAG=${with_chartmuseum} HTTPPROXY= + ${rc} ${output}= Run And Return Rc And Output make package_online VERSIONTAG=%{Harbor_Assets_Version} PKGVERSIONTAG=%{Harbor_Package_Version} NOTARYFLAG=${with_notary} CLAIRFLAG=${with_clair} MIGRATORFLAG=${with_migrator} CHARTFLAG=${with_chartmuseum} HTTPPROXY= Log ${rc} Log ${output} Should Be Equal As Integers ${rc} 0