Skip to content

Commit

Permalink
Merge pull request #723 from camptocamp/fix-version-increment
Browse files Browse the repository at this point in the history
Have a minor version increment for master branch, add documentation
  • Loading branch information
sbrunner authored Sep 23, 2022
2 parents ba14b0a + c8e9a41 commit 1d5ad90
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 44 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ RUN --mount=type=cache,target=/root/.cache \

# Do the conversion
COPY poetry.lock pyproject.toml ./
ENV POETRY_DYNAMIC_VERSIONING_BYPASS=dev
RUN poetry export --extras=checks --extras=publish --extras=audit --output=requirements.txt \
&& poetry export --dev --output=requirements-dev.txt

Expand Down Expand Up @@ -53,9 +54,9 @@ RUN --mount=type=cache,target=/var/lib/apt/lists --mount=type=cache,target=/var/
RUN python3 -m compileall -q -- *

COPY . ./
ARG VERSION=dev
RUN --mount=type=cache,target=/root/.cache \
cd c2cciutils && npm install && cd - \
&& sed --in-place 's/enable = true # disable on Docker/enable = false/g' pyproject.toml \
&& python3 -m pip install --disable-pip-version-check --no-deps --editable=. \
&& POETRY_DYNAMIC_VERSIONING_BYPASS=${VERSION} python3 -m pip install --disable-pip-version-check --no-deps --editable=. \
&& python3 -m pip freeze > /requirements.txt \
&& python3 -m compileall -q /app/c2cciutils
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
GITHUB_REPOSITORY ?= camptocamp/c2cciutils
export DOCKER_BUILDKIT = 1
VERSION = $(strip $(shell poetry version --short))

.PHONY: help
help: ## Display this help message
Expand All @@ -11,7 +12,7 @@ help: ## Display this help message

.PHONY: build
build: ## Build the Docker images
docker build --tag=$(GITHUB_REPOSITORY) .
docker build --build-arg=VERSION=$(VERSION) --tag=$(GITHUB_REPOSITORY) .

.PHONY: build-checker
build-checker: ## Build the checker Docker images
Expand Down
69 changes: 63 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,6 @@ Select a formatter:

### To pypi

When publishing, the version computed from arguments or `GITHUB_REF` is put in environment variable `VERSION`, thus you should use it in `setup.py`, example:

```python
VERSION = os.environ.get("VERSION", "1.0.0")
```

The config is like this:

```yaml
Expand All @@ -175,6 +169,69 @@ versions:
# version_tag, version_branch, feature_branch, feature_tag (for pull request)
```

It we have a `setup.py` file, we will be in legacy mode:
When publishing, the version computed from arguments or `GITHUB_REF` is put in environment variable `VERSION`, thus you should use it in `setup.py`, example:

```python
VERSION = os.environ.get("VERSION", "1.0.0")
```

Also we consider that we use `poetry` with [poetry-dynamic-versioning](https://pypi.org/project/poetry-dynamic-versioning/) to manage the version, and [poetry-plugin-tweak-dependencies-version](https://pypi.org/project/poetry-plugin-tweak-dependencies-version/) to manage the dependencies versions.

Example of configuration:

```toml
[tool.poetry-dynamic-versioning]
enable = true
vcs = "git"
pattern = "^(?P<base>\\d+(\\.\\d+)*)"
format-jinja = """
{%- if env.get("VERSION_TYPE") == "version_branch" -%}
{{serialize_pep440(bump_version(base, 1 if env.get("IS_MASTER") == "TRUE" else 2), dev=distance)}}
{%- elif distance == 0 -%}
{{serialize_pep440(base)}}
{%- else -%}
{{serialize_pep440(bump_version(base), dev=distance)}}
{%- endif -%}
"""
```

Note that we can access to the environment variables `VERSION`,`VERSION_TYPE` and `IS_MASTER`.

Then by default:

- Tag with `1.2.3` => release `1.2.3`
- Commit on feature branch just do a validation
- Commit on `master` branch after the tag 1.3.0 => release `1.4.0.dev1`
- Commit on `1.3` branch after the tag 1.3.0 => release `1.3.1.dev1`

To make it working in the `Dockerfile` you should have in the `poetry` stage:

```Dockerfile
ENV POETRY_DYNAMIC_VERSIONING_BYPASS=dev
RUN poetry export --extras=checks --extras=publish --extras=audit --output=requirements.txt \
&& poetry export --dev --output=requirements-dev.txt
```

And in the `run` stage

```Dockerfile
ARG VERSION=dev
RUN --mount=type=cache,target=/root/.cache \
POETRY_DYNAMIC_VERSIONING_BYPASS=${VERSION} python3 -m pip install --disable-pip-version-check --no-deps --editable=.
```

And in the `Makefile`:

```Makefile
VERSION = $(strip $(shell poetry version --short))
.PHONY: build
build: ## Build the Docker images
docker build --build-arg=VERSION=$(VERSION) --tag=$(GITHUB_REPOSITORY) .
```

### To Docker registry

The config is like this:
Expand Down
31 changes: 9 additions & 22 deletions c2cciutils/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import pickle # nosec
import subprocess # nosec
import sys
import tempfile
import uuid
from typing import List, Optional

Expand Down Expand Up @@ -265,6 +264,12 @@ def pip(
try:
env = dict(os.environ)
env["VERSION"] = version
env["VERSION_TYPE"] = version_type
full_repo = c2cciutils.get_repository()
full_repo_split = full_repo.split("/")
master_branch, _ = c2cciutils.get_master_branch(full_repo_split)
is_master = master_branch == version
env["IS_MASTER"] = "TRUE" if is_master else "FALSE"

cwd = os.path.abspath(package.get("path", "."))

Expand Down Expand Up @@ -292,27 +297,9 @@ def pip(
if requirement_split[0] == "poetry-core":
use_poetry = True
if use_poetry:
if version_type == "version_tag":
pyproject.get("tool", {})["poetry"]["version"] = version
pyproject.get("tool", {})["poetry-dynamic-versioning"]["enable"] = False
elif version_type in ("version_branch", "feature_branch"):
pyproject.get("tool", {})["poetry-dynamic-versioning"][
"format-jinja"
] = pyproject.get("tool", {})["poetry-dynamic-versioning"].get(
"format-jinja-dev", "{{bump_version(base)}}.dev{{timestamp}}"
)
with open(os.path.join(cwd, "pyproject.toml"), "w", encoding="utf-8") as project_file:
project_file.write(tomlkit.dumps(pyproject))
with tempfile.TemporaryDirectory(prefix="c2cciutils-publish-venv") as venv:
subprocess.run(["python3", "-m", "venv", venv], check=True)
subprocess.run([f"{venv}/bin/pip", "install", "poetry"], check=True)
for requirement in pyproject.get("build-system", {}).get("requires", []):
print(f"Install requirement {requirement}")
sys.stdout.flush()
subprocess.run([f"{venv}/bin/pip", "install", requirement], check=True)
subprocess.run(["poetry", "--version"], check=True)
subprocess.run([f"{venv}/bin/poetry", "build"], cwd=cwd, env=env, check=True)
cmd = []
subprocess.run(["poetry", "--version"], check=True)
subprocess.run(["poetry", "build"], cwd=cwd, env=env, check=True)
cmd = []
if cmd:
cmd = package.get("build_command", cmd)
subprocess.check_call(cmd, cwd=cwd, env=env)
Expand Down
2 changes: 1 addition & 1 deletion c2cciutils/scripts/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def main() -> None:
parser.add_argument("--dry-run", action="store_true", help="Don't do the publish")
parser.add_argument(
"--type",
help="The type of version, if no argument provided autodeterminated, can be: "
help="The type of version, if no argument provided auto-determinate, can be: "
"rebuild (in case of rebuild), version_tag, version_branch, feature_branch, feature_tag "
"(for pull request)",
)
Expand Down
4 changes: 3 additions & 1 deletion example-project/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RUN --mount=type=cache,target=/root/.cache \

# Do the conversion
COPY poetry.lock pyproject.toml ./
ENV POETRY_DYNAMIC_VERSIONING_BYPASS=dev
RUN poetry export --output=requirements.txt \
&& poetry export --dev --output=requirements-dev.txt

Expand All @@ -43,7 +44,8 @@ FROM base AS run
RUN python3 -m compileall -q /usr/local/lib/python3.*

COPY . ./
ARG VERSION=dev
RUN --mount=type=cache,target=/root/.cache \
python3 -m pip install --disable-pip-version-check --no-deps --editable=. \
POETRY_DYNAMIC_VERSIONING_BYPASS=${VERSION} python3 -m pip install --disable-pip-version-check --no-deps --editable=. \
&& python3 -m pip freeze > /requirements.txt \
&& python3 -m compileall -q /app/
3 changes: 2 additions & 1 deletion example-project/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
GITHUB_REPOSITORY ?= camptocamp/project
export DOCKER_BUILDKIT = 1
VERSION = $(strip $(shell poetry version --short))

.PHONY: help
help: ## Display this help message
Expand All @@ -11,7 +12,7 @@ help: ## Display this help message

.PHONY: build
build: checks ## Build the Docker images
docker build --tag=$(GITHUB_REPOSITORY) .
docker build --build-arg=VERSION=$(VERSION) --tag=$(GITHUB_REPOSITORY) .

.PHONY: build-checker
build-checker: ## Build the checker Docker images
Expand Down
17 changes: 15 additions & 2 deletions example-project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,26 @@ python = ">=3.8,<4.0"
prospector = { version = "1.7.7", extras = ["with_bandit", "with_mypy", "with_pyorama"] }

[build-system]
requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning", "poetry-plugin-tweak-dependencies-version"]
requires = [
"poetry-core>=1.0.0",
"poetry-dynamic-versioning[plugin]>=0.19.0",
"poetry-plugin-tweak-dependencies-version>=1.0.0"
]
build-backend = "poetry.core.masonry.api"

[tool.poetry-dynamic-versioning]
enable = true
vcs = "git"
pattern = "^((?P<epoch>\\d+)!)?(?P<base>\\d+(\\.\\d+)*)"
pattern = "^(?P<base>\\d+(\\.\\d+)*)"
format-jinja = """
{%- if env.get("VERSION_TYPE") == "version_branch" -%}
{{serialize_pep440(bump_version(base, 1 if env.get("IS_MASTER") == "TRUE" else 2), dev=distance)}}
{%- elif distance == 0 -%}
{{serialize_pep440(base)}}
{%- else -%}
{{serialize_pep440(bump_version(base), dev=distance)}}
{%- endif -%}
"""

[tool.poetry-plugin-tweak-dependencies-version]
default = "major"
Loading

0 comments on commit 1d5ad90

Please sign in to comment.