diff --git a/.github/build_new_tag.py b/.github/build_new_tag.py index 7525eaf..d1df387 100644 --- a/.github/build_new_tag.py +++ b/.github/build_new_tag.py @@ -30,6 +30,17 @@ def cmd_run(cmd: str) -> str: return get_str(output) +def cmd_run_iter(cmd: str): + _cmd = str(cmd).split() + popen = subprocess.Popen(_cmd, stdout=subprocess.PIPE, universal_newlines=True) + for stdout_line in iter(popen.stdout.readline, ""): + yield get_str(stdout_line) + popen.stdout.close() + return_code = popen.wait() + if return_code: + raise subprocess.CalledProcessError(return_code, _cmd) + + def is_empty(obj): if obj is None: return True @@ -57,19 +68,25 @@ def is_not_empty(obj): return not is_empty(obj) +def next_revision_num(version_pre: str) -> int: + _max = -1 + for line in cmd_run_iter("git ls-remote --tags -q"): + if is_empty(line): + continue + if version_pre not in line: + continue + code = int(line.rsplit('.', 1)[-1]) + print(f"code:{code}") + if _max < code: + _max = code + return _max + 1 + + v_prefix = os.getenv('VERSION_PREFIX') v_major_minor = os.getenv('VERSION_MAJOR_MINOR') print(f"v_prefix:{v_prefix}") print(f"v_major_minor:{v_major_minor}") -# Get the path of the runner file -v_revision_old = cmd_run( - f"git tag --list '{v_prefix}{v_major_minor}.*' --sort=-version:refname | head -n 1 | grep -oE '[0-9]+$'") -v_revision = 0 -if is_empty(v_revision_old): - print("v_revision_old empty!!!") - v_revision = 0 -else: - v_revision = int(v_revision_old.strip()) + 1 +v_revision = next_revision_num(f"{v_prefix}{v_major_minor}.") new_tag = f"{v_prefix}{v_major_minor}.{v_revision}" print(f"newTag:{new_tag}") add_env("NEW_TAG", new_tag) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 84cfb73..4a24e32 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -40,27 +40,7 @@ jobs: echo "VERSION_MAJOR_MINOR:${VERSION_MAJOR_MINOR}" echo "VERSION_PREFIX=$VERSION_PREFIX" >> $GITHUB_ENV echo "VERSION_MAJOR_MINOR=$VERSION_MAJOR_MINOR" >> $GITHUB_ENV - # - name: Generate Git Tag - # id: generate_tag - # uses: actions/setup-python@v5 - # with: - # python-version: '3.9' - # run: | - # - # - name: Generate Git Tag - # id: generate_tag - # run: | - # echo "- :white_check_mark: checkout done." >> $GITHUB_STEP_SUMMARY - # VERSION_PATCH=$(git tag --list "${VERSION_PREFIX}${VERSION_MAJOR_MINOR}.*" --sort=-version:refname | head -n 1 | grep -oE '[0-9]+$') - # if [ -z "$VERSION_PATCH" ]; then - # VERSION_PATCH=0 - # else - # VERSION_PATCH=$((VERSION_PATCH + 1)) - # fi - # NEW_TAG="${VERSION_PREFIX}${VERSION_MAJOR_MINOR}.${VERSION_PATCH}" - # echo "Generated new tag: $NEW_TAG" - # echo "NEW_TAG=$NEW_TAG" >> $GITHUB_ENV - # echo "- :white_check_mark: create new tag: ${NEW_TAG}" >> $GITHUB_STEP_SUMMARY + - name: generate git tag id: gennerate_tag run: python .github/build_new_tag.py