Skip to content

Commit 397287f

Browse files
committed
Added github ci
1 parent bf6196a commit 397287f

11 files changed

Lines changed: 387 additions & 39 deletions

File tree

.github/workflows/ci.yml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main, "release/*"]
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
outputs:
13+
version: ${{ steps.git_version.outputs.BUILD_VERSION }}
14+
version_major: ${{ steps.git_version.outputs.BUILD_VERSION_MAJOR }}
15+
version_minor: ${{ steps.git_version.outputs.BUILD_VERSION_MINOR }}
16+
version_patch: ${{ steps.git_version.outputs.BUILD_VERSION_PATCH }}
17+
version_build: ${{ steps.git_version.outputs.BUILD_VERSION_BUILD }}
18+
tag: ${{ steps.git_version.outputs.BUILD_VERSION_TAG }}
19+
branch: ${{ steps.git_version.outputs.BUILD_VERSION_BRANCH }}
20+
commit: ${{ steps.git_version.outputs.BUILD_VERSION_COMMIT }}
21+
short: ${{ steps.git_version.outputs.BUILD_VERSION_SHORT }}
22+
full: ${{ steps.git_version.outputs.BUILD_VERSION_FULL }}
23+
extended: ${{ steps.git_version.outputs.BUILD_VERSION_EXTENDED }}
24+
default_branch: ${{ steps.git_version.outputs.BUILD_VERSION_DEFAULT_BRANCH }}
25+
release_branches: ${{ steps.git_version.outputs.BUILD_VERSION_RELEASE_BRANCHES }}
26+
27+
steps:
28+
- uses: actions/checkout@v4
29+
with:
30+
fetch-depth: 0
31+
32+
- name: Set up Python
33+
uses: actions/setup-python@v5
34+
with:
35+
python-version: "3.13"
36+
37+
- name: Install build tools
38+
run: |
39+
python -m pip install --upgrade pip
40+
pip install build
41+
42+
- name: Generate _version.py from git tags
43+
run: python tools/write_version.py
44+
45+
- name: Build package
46+
run: python -m build
47+
48+
- name: Install built package
49+
run: pip install dist/*.whl
50+
51+
- name: Extract version info from built package
52+
id: git_version
53+
run: |
54+
while IFS='=' read -r key value; do
55+
echo "$key=$value" >> "$GITHUB_OUTPUT"
56+
done < <(git-version --property env)
57+
58+
- name: Show version info
59+
run: |
60+
echo "═══════════════════════════════════════"
61+
echo " GIT VERSION INFORMATION"
62+
echo "═══════════════════════════════════════"
63+
echo " Version: ${{ steps.git_version.outputs.BUILD_VERSION }}"
64+
echo " Major: ${{ steps.git_version.outputs.BUILD_VERSION_MAJOR }}"
65+
echo " Minor: ${{ steps.git_version.outputs.BUILD_VERSION_MINOR }}"
66+
echo " Patch: ${{ steps.git_version.outputs.BUILD_VERSION_PATCH }}"
67+
echo " Build: ${{ steps.git_version.outputs.BUILD_VERSION_BUILD }}"
68+
echo " Tag: ${{ steps.git_version.outputs.BUILD_VERSION_TAG }}"
69+
echo " Branch: ${{ steps.git_version.outputs.BUILD_VERSION_BRANCH }}"
70+
echo " Commit: ${{ steps.git_version.outputs.BUILD_VERSION_COMMIT }}"
71+
echo " Short: ${{ steps.git_version.outputs.BUILD_VERSION_SHORT }}"
72+
echo " Full: ${{ steps.git_version.outputs.BUILD_VERSION_FULL }}"
73+
echo " Extended: ${{ steps.git_version.outputs.BUILD_VERSION_EXTENDED }}"
74+
echo " DefaultBranch: ${{ steps.git_version.outputs.BUILD_VERSION_DEFAULT_BRANCH }}"
75+
echo " ReleaseBranches: ${{ steps.git_version.outputs.BUILD_VERSION_RELEASE_BRANCHES }}"
76+
echo "═══════════════════════════════════════"
77+
78+
- name: Upload build artifacts
79+
uses: actions/upload-artifact@v4
80+
with:
81+
name: dist
82+
path: dist/
83+
84+
test:
85+
runs-on: ubuntu-latest
86+
needs: build
87+
88+
strategy:
89+
matrix:
90+
python-version: ["3.10", "3.11", "3.12", "3.13"]
91+
92+
steps:
93+
- uses: actions/checkout@v4
94+
with:
95+
fetch-depth: 0
96+
97+
- name: Set up Python ${{ matrix.python-version }}
98+
uses: actions/setup-python@v5
99+
with:
100+
python-version: ${{ matrix.python-version }}
101+
102+
- name: Install build tools
103+
run: |
104+
python -m pip install --upgrade pip
105+
pip install build
106+
107+
- name: Generate _version.py from git tags
108+
run: python tools/write_version.py
109+
110+
- name: Build package
111+
run: python -m build
112+
113+
- name: Install built package
114+
run: pip install dist/*.whl
115+
116+
- name: Install test dependencies
117+
run: pip install pytest
118+
119+
- name: Run tests from repo
120+
run: python -m pytest tests/ -v
121+
122+
lint:
123+
runs-on: ubuntu-latest
124+
125+
steps:
126+
- uses: actions/checkout@v4
127+
128+
- name: Set up Python
129+
uses: actions/setup-python@v5
130+
with:
131+
python-version: "3.13"
132+
133+
- name: Install lint tools
134+
run: |
135+
python -m pip install --upgrade pip
136+
pip install ruff
137+
138+
- name: Lint with ruff
139+
run: ruff check src/ tests/

.github/workflows/publish.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Publish to PyPI
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version:
7+
description: "Version to publish (e.g., 0.1.0)"
8+
required: true
9+
type: string
10+
11+
jobs:
12+
publish:
13+
runs-on: ubuntu-latest
14+
if: github.ref == 'refs/heads/master'
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Set up Python
22+
uses: actions/setup-python@v5
23+
with:
24+
python-version: "3.13"
25+
26+
- name: Install build tools
27+
run: |
28+
python -m pip install --upgrade pip
29+
pip install build
30+
31+
- name: Generate _version.py from git tags
32+
run: python tools/write_version.py
33+
34+
- name: Build package
35+
run: python -m build
36+
37+
- name: Publish to PyPI
38+
uses: pypa/gh-action-pypi-publish@release/v1
39+
with:
40+
password: ${{ secrets.PYPI_API_TOKEN }}

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include tools/write_version.py

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "git-version-utils"
7-
version = "0.1.0"
7+
version = "0.0.0"
88
description = "Extract version information from a Git repository and output as environment variables"
99
readme = "README.md"
1010
authors = [

setup.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Setup script for git-version-utils.
2+
3+
The package version is determined dynamically from git tags.
4+
Run `python tools/write_version.py` before building to generate
5+
src/git_version/_version.py with the correct version.
6+
7+
The version in pyproject.toml is a fallback placeholder (0.0.0).
8+
"""
9+
10+
import os
11+
import tomllib
12+
13+
from setuptools import setup
14+
15+
16+
def get_version() -> str:
17+
"""Get the package version from pyproject.toml."""
18+
pyproject = os.path.join(os.path.dirname(__file__), "pyproject.toml")
19+
if os.path.exists(pyproject):
20+
with open(pyproject, "rb") as f:
21+
data = tomllib.load(f)
22+
return data.get("project", {}).get("version", "0.0.0")
23+
return "0.0.0"
24+
25+
26+
if __name__ == "__main__":
27+
setup(version=get_version())

src/git_version/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
try:
2+
from ._version import __version__, __version_tuple__
3+
except ImportError:
4+
__version__ = "0.0.0"
5+
__version_tuple__ = (0, 0, 0)
6+
17
from .core import GitVersion
28

3-
__all__ = ["GitVersion"]
9+
__all__ = ["GitVersion", "__version__", "__version_tuple__"]

src/git_version/_version.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# file generated by tools/write_version.py
2+
# don't change, don't track in version control
3+
__version__ = "0.0.0.3"
4+
__version_tuple__ = (0, 0, 0, 3)

src/git_version/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse
2-
import sys
32

43
from .core import GitVersion
54

@@ -28,7 +27,7 @@ def main() -> None:
2827
choices=[
2928
"tag", "version", "version_major", "version_minor", "version_patch",
3029
"build", "branch", "short", "full", "extended", "commit",
31-
"default_branch", "env", "all",
30+
"default_branch", "release_branches", "env", "all",
3231
],
3332
default="all",
3433
help="Which property to output (default: all)",

src/git_version/core.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import subprocess
4+
import fnmatch
45
from functools import cached_property
56

67

@@ -10,15 +11,19 @@ class GitVersion:
1011
Args:
1112
repo_path: Path to the Git repository (default: current working directory).
1213
tag_pattern: Glob pattern to match version tags (default: "v[0-9]*").
14+
release_branches: List of branch patterns considered as release branches.
15+
Defaults to [default_branch, "release/*"].
1316
"""
1417

1518
def __init__(
1619
self,
1720
repo_path: str | None = None,
1821
tag_pattern: str = "v[0-9]*",
22+
release_branches: list[str] | None = None,
1923
):
2024
self.repo_path = os.path.abspath(repo_path or os.getcwd())
2125
self.tag_pattern = tag_pattern
26+
self._release_branches = release_branches
2227

2328
def _git(self, *args: str) -> str:
2429
"""Execute a git command safely and return stripped stdout."""
@@ -74,6 +79,16 @@ def default_branch(self) -> str:
7479
result = self._git("config", "--get", "init.defaultBranch")
7580
return result or "master"
7681

82+
@cached_property
83+
def release_branches(self) -> list[str]:
84+
"""List of release branch patterns.
85+
86+
Defaults to [default_branch, "release/*"] if not explicitly set.
87+
"""
88+
if self._release_branches is not None:
89+
return self._release_branches
90+
return [self.default_branch, "release/*"]
91+
7792
@cached_property
7893
def build(self) -> str:
7994
"""Number of commits since the last version tag."""
@@ -94,16 +109,31 @@ def short(self) -> str:
94109

95110
@cached_property
96111
def full(self) -> str:
97-
"""Full version string with commit hash: '<version>-<short>'."""
98-
return f"{self.version}-{self.short}"
112+
"""Full version string.
113+
114+
If the current branch matches a release branch pattern, returns just the
115+
version (e.g. '1.0.0.1'). Otherwise returns the extended version with
116+
commit hash (e.g. '1.0.0.1-a1b2c3').
117+
"""
118+
if self._is_release_branch():
119+
return self.version
120+
return self.extended
99121

100122
@cached_property
101123
def extended(self) -> str:
102-
"""Extended version: same as 'version' if build==0, else 'full'."""
103-
if self.build == "0":
104-
return self.version
124+
"""Extended version: '<version>-<short>' (e.g. '1.0.0.1-a1b2c3')."""
105125
return f"{self.version}-{self.short}"
106126

127+
def _is_release_branch(self) -> bool:
128+
"""Check if the current branch matches any release branch pattern."""
129+
current = self.branch
130+
if not current:
131+
return False
132+
for pattern in self.release_branches:
133+
if fnmatch.fnmatch(current, pattern):
134+
return True
135+
return False
136+
107137
@cached_property
108138
def commit(self) -> str:
109139
"""Full 40-character commit hash."""
@@ -132,6 +162,7 @@ def env(self, prefix: str = "BUILD_VERSION") -> dict[str, str]:
132162
f"{prefix}_COMMIT": self.commit,
133163
f"{prefix}_BRANCH": self.branch,
134164
f"{prefix}_DEFAULT_BRANCH": self.default_branch,
165+
f"{prefix}_RELEASE_BRANCHES": " ".join(self.release_branches),
135166
}
136167

137168
def __str__(self) -> str:
@@ -143,4 +174,5 @@ def __str__(self) -> str:
143174
Build: {self.build}
144175
Extended: {self.extended}
145176
Commit: {self.commit}
177+
Release branches: {", ".join(self.release_branches)}
146178
"""

0 commit comments

Comments
 (0)