Commit 20c75519 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch 'feat/issue_112' into 'master'

feat(hatch): switch to Hatch for bumping and version management and for (all)...

Closes #112

See merge request to-be-continuous/python!161
parents 43cdd442 2ab8588b
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -463,6 +463,7 @@ The release job is bound to the `publish` stage, appears only on production and
| :lock: `GIT_PASSWORD`   | Git password for Git push operations (see below)                        | _none_            |
| :lock: `GIT_PRIVATE_KEY`| SSH key for Git push operations (see below)                             | _none_            |
| `release-commit-message` / `PYTHON_RELEASE_COMMIT_MESSAGE`| The Git commit message to use on the release commit. This is templated using the [Python Format String Syntax](http://docs.python.org/2/library/string.html#format-string-syntax). Available in the template context are current_version and new_version. | `chore(python-release): {current_version} → {new_version}  [skip ci on prod]` |
| `release-commit-additional-patterns` / `PYTHON_RELEASE_COMMIT_ADDITIONAL_PATTERNS`| Add additional patterns to git commit when release after bump (with git convention). | `` |

When `py-release` job is enabled, `py-publish` job is automatically enabled too.

@@ -474,7 +475,7 @@ The Python template supports three packaging systems:

* [Poetry](https://python-poetry.org/): uses Poetry-specific [version](https://python-poetry.org/docs/cli/#version), [build](https://python-poetry.org/docs/cli/#build) and [publish](https://python-poetry.org/docs/cli/#publish) commands.
* [uv](https://docs.astral.sh/uv/): uses [bump-my-version](https://github.com/callowayproject/bump-my-version) as version management, [build](https://docs.astral.sh/uv/guides/publish/#building-your-package) as package builder and [publish](https://docs.astral.sh/uv/guides/publish/) to publish.
* [hatch](https://hatch.pypa.io/latest//): uses [bump-my-version](https://github.com/callowayproject/bump-my-version) as version management, [build](https://hatch.pypa.io/latest/build/) as package builder and [publish](https://hatch.pypa.io/latest/publish/) to publish.
* [hatch](https://hatch.pypa.io/latest//): uses [version](https://hatch.pypa.io/latest/version/#versioning) as version management, [build](https://hatch.pypa.io/latest/build/) as package builder and [publish](https://hatch.pypa.io/latest/publish/) to publish.
* [Setuptools](https://setuptools.pypa.io/): uses [bump-my-version](https://github.com/callowayproject/bump-my-version) as version management, [build](https://pypa-build.readthedocs.io/) as package builder and [Twine](https://twine.readthedocs.io/) to publish.

The publish job is bound to the `publish` stage, is executed on a Git tag matching [semantic versioning pattern](https://semver.org/) and uses the following variables:
+5 −0
Original line number Diff line number Diff line
@@ -233,6 +233,11 @@
          "default": "chore(python-release): {current_version} → {new_version} [skip ci on prod]",
          "advanced": true
        },
        {
          "name": "PYTHON_RELEASE_COMMIT_ADDITIONAL_PATTERNS",
          "description": "Define additional patterns to git commit when release after bump (with git convention).",
          "advanced": true
        },
        {
          "name": "GIT_USERNAME",
          "description": "Git username for Git push operations",
+49 −62
Original line number Diff line number Diff line
@@ -164,6 +164,9 @@ spec:
    release-commit-message:
      description: The Git commit message to use on the release commit. This is templated using the [Python Format String Syntax](http://docs.python.org/2/library/string.html#format-string-syntax). Available in the template context are current_version and new_version.
      default: "chore(python-release): {current_version} \u2192 {new_version} [skip ci on prod]"
    release-commit-additional-patterns:
      description: Define additional patterns to git commit when release after bump (with git convention).
      default: ""
    repository-url:
      description: |-
        Target PyPI repository to publish packages.
@@ -286,6 +289,7 @@ variables:

  PYTHON_RELEASE_NEXT: $[[ inputs.release-next ]]
  PYTHON_RELEASE_COMMIT_MESSAGE: $[[ inputs.release-commit-message ]]
  PYTHON_RELEASE_COMMIT_ADDITIONAL_PATTERNS: $[[ inputs.release-commit-additional-patterns ]]

  # By default, publish on the Packages registry of the project
  # https://docs.gitlab.com/user/packages/pypi_repository/#authenticate-with-a-ci-job-token
@@ -786,21 +790,15 @@ variables:
  }

  function _run() {
    if [[ "$PYTHON_BUILD_SYSTEM" =~ ^poetry ]]
    then
      maybe_install_build_system
      poetry run "$@"
    elif [[ "$PYTHON_BUILD_SYSTEM" =~ ^uv ]]
    then
      maybe_install_build_system
      uv run "$@"
    elif [[ "$PYTHON_BUILD_SYSTEM" =~ ^hatch ]]
    then
    case "$PYTHON_BUILD_SYSTEM" in
      poetry*|uv*|hatch*)
        maybe_install_build_system
        $PYTHON_BUILD_SYSTEM_CMD run "$@"
    else
        ;;
      *)
        "$@"
    fi
        ;;
    esac
  }

  function _python() {
@@ -825,30 +823,27 @@ variables:
  function py_package() {
    _start_time=$(get_current_ts_ms)

    # clean reports for this job but there not impact on other jobs
    # clean reports for this job but no impact on other jobs
    rm -fr "$PYTHON_PROJECT_DIR/reports"

    if [[ "$PYTHON_BUILD_SYSTEM" =~ ^poetry ]]
    then
    case "$PYTHON_BUILD_SYSTEM" in
      poetry*|uv*)
        maybe_install_build_system
      log_info "--- build packages (poetry)..."
      poetry build ${TRACE+--verbose}
    elif [[ "$PYTHON_BUILD_SYSTEM" =~ ^uv ]]
    then
        log_info "--- build packages ($PYTHON_BUILD_SYSTEM_CMD)..."
        $PYTHON_BUILD_SYSTEM_CMD build ${TRACE+--verbose}
        ;;
      hatch*)
        maybe_install_build_system
      log_info "--- build packages (uv)..."
      uv build ${TRACE+--verbose}
    elif [[ "$PYTHON_BUILD_SYSTEM" =~ ^hatch ]]
    then
        log_info "--- build packages (hatch)..."
      maybe_install_build_system
        $PYTHON_BUILD_SYSTEM_CMD build
    else
        ;;
      *)
        log_info "--- build packages ..."
        # shellcheck disable=SC2086
        pip install ${PIP_OPTS} build
        python -m build
    fi
        ;;
    esac

    log_elapsed_time "py_package" "$_start_time"
  }
@@ -888,15 +883,18 @@ variables:
    rm -fr tbc_tmp/version.txt >&2
  }

  function py_commit_pyproject() {
  function py_git_tag_commit_release_change() {
    py_cur_version="$1"
    py_next_version="$2"  
    # Git commit and tag
    git add pyproject.toml
    log_info "[git] will commit pyproject.toml ${PYTHON_RELEASE_COMMIT_ADDITIONAL_PATTERNS}"
    # shellcheck disable=SC2086
    git add pyproject.toml ${PYTHON_RELEASE_COMMIT_ADDITIONAL_PATTERNS}
    git status -sb
    # emulate bump-my-version to generate commit message
    py_commit_message=$(python -c "print('$PYTHON_RELEASE_COMMIT_MESSAGE'.format(current_version='$py_cur_version', new_version='$py_next_version'))")
    git commit -m "$py_commit_message"
    git tag "$py_next_version"  
    git tag --force "$py_next_version"  
  }

  function py_release() {
@@ -929,12 +927,8 @@ variables:
      poetry version ${TRACE+--verbose} "$py_next_version"
      # eval exact next version
      py_next_version=$(poetry version --short)
      # Git commit and tag
      git add pyproject.toml
      # emulate bump-my-version to generate commit message
      py_commit_message=$(python -c "print('$PYTHON_RELEASE_COMMIT_MESSAGE'.format(current_version='$py_cur_version', new_version='$py_next_version'))")
      git commit -m "$py_commit_message"
      git tag "$py_next_version"

      py_git_tag_commit_release_change "$py_cur_version" "$py_next_version"
    elif [[ "$PYTHON_BUILD_SYSTEM" =~ ^uv ]]
    then
      maybe_install_build_system
@@ -946,7 +940,6 @@ variables:
          log_info "[uv version] increase \\e[1;94m${PYTHON_RELEASE_NEXT}\\e[0m (from current \\e[1;94m${py_cur_version}\\e[0m)"
          uv version ${TRACE+--verbose} --bump "$PYTHON_RELEASE_NEXT"
          py_next_version=$(uv version --short)

        else
          log_info "[uv version] change version \\e[1;94m${py_cur_version}\\e[0m → \\e[1;94m${py_next_version}\\e[0m"
          uv version ${TRACE+--verbose} "$py_next_version"
@@ -959,35 +952,29 @@ variables:
          uvx --from toml-cli toml get --toml-path pyproject.toml project.version > tbc_tmp/version.txt
          py_cur_version=$(cat tbc_tmp/version.txt)

          log_info "[bump-my-version] increase \\e[1;94m${PYTHON_RELEASE_NEXT}\\e[0m (from current \\e[1;94m${py_cur_version}\\e[0m)"
          uvx bump-my-version bump ${TRACE+--verbose} --current-version "$py_cur_version" "$PYTHON_RELEASE_NEXT" tbc_tmp/version.txt
          py_next_version=$(cat tbc_tmp/version.txt)
          rm -fr tbc_tmp/version.txt
          py_next_version=$(py_bump_my_version "$py_cur_version" "$PYTHON_RELEASE_NEXT")
        fi

        log_info "[toml-cli] change version \\e[1;94m${py_cur_version}\\e[0m → \\e[1;94m${py_next_version}\\e[0m"
        uvx --from toml-cli toml set --toml-path pyproject.toml project.version "$py_next_version"
      fi

      # Git commit and tag
      git add pyproject.toml
      # emulate bump-my-version to generate commit message
      py_commit_message=$(python -c "print('$PYTHON_RELEASE_COMMIT_MESSAGE'.format(current_version='$py_cur_version', new_version='$py_next_version'))")
      git commit -m "$py_commit_message"
      git tag --force "$py_next_version"
      py_git_tag_commit_release_change "$py_cur_version" "$py_next_version"
    elif [[ "$PYTHON_BUILD_SYSTEM" =~ ^hatch ]]
    then
      maybe_install_build_system
      if [[ -z "$py_next_version" ]]
      then
        py_cur_version=$(hatch version)
        py_next_version=$(py_bump_my_version "$py_cur_version" "$PYTHON_RELEASE_NEXT")
        log_info "[hatch version] increase \\e[1;94m${PYTHON_RELEASE_NEXT}\\e[0m (from current \\e[1;94m${py_cur_version}\\e[0m)"
        hatch version "$PYTHON_RELEASE_NEXT"
        py_next_version=$(hatch version)
      else
        log_info "[hatch] change version \\e[1;94m${py_cur_version}\\e[0m → \\e[1;94m${py_next_version}\\e[0m"
        hatch version "$py_next_version"
      fi

      log_info "[hatch] change version \\e[1;94m${py_cur_version}\\e[0m → \\e[1;94m${py_next_version}\\e[0m"
      _pip install toml-cli
      _run toml set --toml-path pyproject.toml project.version "$py_next_version"
      py_commit_pyproject "$py_cur_version" "$py_next_version"
      py_git_tag_commit_release_change "$py_cur_version" "$py_next_version"
    else
      # Setuptools / bump-my-version
      # shellcheck disable=SC2086