From 890e098d2133bc02069663a2cd327137d68a422b Mon Sep 17 00:00:00 2001 From: joyceliu Date: Thu, 12 Dec 2024 18:15:06 +0800 Subject: [PATCH] feat: add version in image Signed-off-by: joyceliu --- build/Dockerfile | 3 ++ hack/docker_build_multiarch.sh | 3 ++ hack/update-licenses.sh | 0 hack/version.sh | 84 ++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) mode change 100644 => 100755 hack/update-licenses.sh create mode 100755 hack/version.sh diff --git a/build/Dockerfile b/build/Dockerfile index f77209c1f25..4b2e008a1d3 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -19,6 +19,9 @@ RUN mv /kubesphere/server/locales \ #RUN ["/bin/bash", "-c", "mv /kubesphere/server/{locales,public,sample,views,config.yaml} /out/server/"] RUN mv /kubesphere/package.json /out/ +# generate version file to out +RUN hack/version.sh > /out/version.txt + ############## # Final Image ############## diff --git a/hack/docker_build_multiarch.sh b/hack/docker_build_multiarch.sh index c0d3cdc7350..a5eff3bf7a0 100755 --- a/hack/docker_build_multiarch.sh +++ b/hack/docker_build_multiarch.sh @@ -27,6 +27,9 @@ sudo chown $(id -u):$(id -g) -R $PROJECT_DIR/server # build out dir rm -rf "$PROJECT_DIR"/out/ mkdir -p "$PROJECT_DIR"/out/server +# generate version file to out +"$PROJECT_DIR"/hack/version.sh > "$PROJECT_DIR"/out/version.txt + mv "$PROJECT_DIR"/dist/ "$PROJECT_DIR"/package.json "$PROJECT_DIR"/out/ mv "$PROJECT_DIR"/server/locales \ "$PROJECT_DIR"/server/public \ diff --git a/hack/update-licenses.sh b/hack/update-licenses.sh old mode 100644 new mode 100755 diff --git a/hack/version.sh b/hack/version.sh new file mode 100755 index 00000000000..b90c89bd061 --- /dev/null +++ b/hack/version.sh @@ -0,0 +1,84 @@ +#!/usr/bin/env bash + +# ----------------------------------------------------------------------------- +# Version management helpers. These functions help to set, save and load the +# following variables: +# +# KUBE_GIT_COMMIT - The git commit id corresponding to this +# source code. +# KUBE_GIT_TREE_STATE - "clean" indicates no changes since the git commit id +# "dirty" indicates source code changes after the git commit id +# "archive" indicates the tree was produced by 'git archive' +# KUBE_GIT_VERSION - "vX.Y" used to indicate the last release version. +# KUBE_GIT_MAJOR - The major part of the version +# KUBE_GIT_MINOR - The minor component of the version + +KUBE_GIT_COMMIT="" +KUBE_GIT_TREE_STATE="" +KUBE_GIT_VERSION="" +KUBE_GIT_MAJOR="" +KUBE_GIT_MINOR="" +KUBE_GIT_RELEASE_COMMIT="" +SOURCE_DATE_EPOCH="" + +version::get_version_vars() { + local git=(git --work-tree "${KUBE_ROOT:-.}") + + if [[ -n ${KUBE_GIT_COMMIT-} ]] || KUBE_GIT_COMMIT=$("${git[@]}" rev-parse "HEAD^{commit}" 2>/dev/null); then + if [[ -z ${KUBE_GIT_TREE_STATE-} ]]; then + if git_status=$("${git[@]}" status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then + KUBE_GIT_TREE_STATE="clean" + else + KUBE_GIT_TREE_STATE="dirty" + fi + fi + + if [[ -n ${KUBE_GIT_VERSION-} ]] || KUBE_GIT_VERSION=$("${git[@]}" describe --tags --match='v*' --abbrev=14 "${KUBE_GIT_COMMIT}^{commit}" 2>/dev/null); then + DASHES_IN_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/[^-]//g") + if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then + KUBE_GIT_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1+\2/") + elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then + KUBE_GIT_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/") + fi + if [[ "${KUBE_GIT_TREE_STATE}" == "dirty" ]]; then + KUBE_GIT_VERSION+="-dirty" + fi + + if [[ "${KUBE_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then + KUBE_GIT_MAJOR=${BASH_REMATCH[1]} + KUBE_GIT_MINOR=${BASH_REMATCH[2]} + if [[ -n "${BASH_REMATCH[4]}" ]]; then + KUBE_GIT_MINOR+="+" + fi + fi + + if ! [[ "${KUBE_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then + echo "KUBE_GIT_VERSION should be a valid Semantic Version. Current value: ${KUBE_GIT_VERSION}" + echo "Please see more details here: https://semver.org" + exit 1 + fi + fi + fi +} + +version() { + version::get_version_vars + + local buildDate + buildDate=$(date ${SOURCE_DATE_EPOCH:+"--date=@${SOURCE_DATE_EPOCH}"} -u +'%Y-%m-%dT%H:%M:%SZ') + + printf '{ + "buildDate": "%s", + "gitCommit": "%s", + "gitTreeState": "%s", + "gitMajor": "%s", + "gitMinor": "%s", + "gitVersion": "%s", + "gitReleaseCommit": "%s" +}\n' \ + "$buildDate" "$KUBE_GIT_COMMIT" "$KUBE_GIT_TREE_STATE" "$KUBE_GIT_MAJOR" "$KUBE_GIT_MINOR" "$KUBE_GIT_VERSION" "$KUBE_GIT_RELEASE_COMMIT" +} + +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + version +fi \ No newline at end of file