Skip to content

Commit

Permalink
Move publish to tag-publish
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 6, 2024
1 parent 21c41bc commit 3becc2e
Show file tree
Hide file tree
Showing 20 changed files with 10 additions and 4,376 deletions.
1 change: 0 additions & 1 deletion .nvmrc

This file was deleted.

19 changes: 3 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN --mount=type=cache,target=/var/lib/apt/lists \
&& apt-get upgrade --yes \
&& apt-get install --yes --no-install-recommends apt-utils \
&& DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get install --yes tzdata \
&& apt-get install --yes --no-install-recommends binutils python3-pip python3-venv \
&& apt-get install --yes --no-install-recommends binutils git python3-pip python3-venv \
&& python3 -m venv /venv

ENV PATH=/venv/bin:$PATH
Expand All @@ -27,7 +27,7 @@ RUN --mount=type=cache,target=/root/.cache \
# Do the conversion
COPY poetry.lock pyproject.toml ./
ENV POETRY_DYNAMIC_VERSIONING_BYPASS=0.0.0
RUN poetry export --extras=checks --extras=publish --extras=audit --extras=version --output=requirements.txt \
RUN poetry export --output=requirements.txt \
&& poetry export --with=dev --output=requirements-dev.txt

# Base, the biggest thing is to install the Python packages
Expand All @@ -49,25 +49,12 @@ FROM base AS run

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

COPY .nvmrc /tmp
RUN --mount=type=cache,target=/var/lib/apt/lists --mount=type=cache,target=/var/cache \
apt-get update \
&& apt-get --assume-yes upgrade \
&& apt-get install --assume-yes --no-install-recommends apt-transport-https gnupg curl \
&& NODE_MAJOR="$(cat /tmp/.nvmrc)" \
&& echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_MAJOR}.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \
&& curl --silent https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor --output=/etc/apt/keyrings/nodesource.gpg \
&& apt-get update \
&& apt-get install --assume-yes --no-install-recommends "nodejs=${NODE_MAJOR}.*" libmagic1 git python3-dev libpq-dev gcc python-is-python3

RUN python3 -m compileall -q -- *

COPY . ./
ARG VERSION=dev
RUN --mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/root/.npm \
cd c2cciutils && npm install && cd - \
&& POETRY_DYNAMIC_VERSIONING_BYPASS=${VERSION} 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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ 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 --output=requirements.txt \
RUN poetry export --output=requirements.txt \
&& poetry export --with=dev --output=requirements-dev.txt
```

Expand Down
189 changes: 1 addition & 188 deletions c2cciutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
c2cciutils shared utils function.
"""

import glob
import json
import os.path
import re
import subprocess # nosec
import sys
from re import Match, Pattern
from typing import Any, Optional, TypedDict, cast
from typing import Any, Optional, cast

import requests
import ruamel.yaml
Expand Down Expand Up @@ -37,25 +35,6 @@ def get_repository() -> str:
return "camptocamp/project"


def merge(default_config: Any, config: Any) -> Any:
"""
Deep merge the dictionaries (on dictionaries only, not on arrays).
Arguments:
default_config: The default config that will be applied
config: The base config, will be modified
"""
if not isinstance(default_config, dict) or not isinstance(config, dict):
return config

for key in default_config.keys():
if key not in config:
config[key] = default_config[key]
else:
merge(default_config[key], config[key])
return config


def get_master_branch(repo: list[str]) -> tuple[str, bool]:
"""Get the name of the master branch."""
master_branch = "master"
Expand All @@ -82,52 +61,6 @@ def get_config() -> c2cciutils.configuration.Configuration:
yaml_ = ruamel.yaml.YAML()
config = yaml_.load(open_file)

repository = get_repository()
repo = repository.split("/")
master_branch, _ = get_master_branch(repo)

merge(
{
"version": {
"tag_to_version_re": [
{"from": r"([0-9]+.[0-9]+.[0-9]+)", "to": r"\1"},
],
"branch_to_version_re": [
{"from": r"([0-9]+.[0-9]+)", "to": r"\1"},
{"from": master_branch, "to": master_branch},
],
}
},
config,
)

has_docker_files = bool(
subprocess.run(
["git", "ls-files", "*/Dockerfile*", "Dockerfile*"], stdout=subprocess.PIPE, check=True
).stdout
)
has_python_package = bool(
subprocess.run(
["git", "ls-files", "setup.py", "*/setup.py"], stdout=subprocess.PIPE, check=True
).stdout
) or bool(
subprocess.run(
["git", "ls-files", "pyproject.toml", "*/pyproject.toml"], stdout=subprocess.PIPE, check=True
).stdout
)

publish_config = merge(c2cciutils.configuration.PUBLISH_DEFAULT, {})
publish_config["pypi"]["packages"] = [{"path": "."}] if has_python_package else []
publish_config["docker"]["images"] = [{"name": get_repository()}] if has_docker_files else []
publish_config["helm"]["folders"] = [
os.path.dirname(f) for f in glob.glob("./**/Chart.yaml", recursive=True)
]

default_config = {
"publish": publish_config,
}
merge(default_config, config)

return config


Expand Down Expand Up @@ -174,98 +107,6 @@ def error(
print(f"[{error_type}] {result}")


VersionTransform = TypedDict(
"VersionTransform",
{
# The from regular expression
"from": Pattern[str],
# The expand regular expression: https://docs.python.org/3/library/re.html#re.Match.expand
"to": str,
},
total=False,
)


def compile_re(config: c2cciutils.configuration.VersionTransform, prefix: str = "") -> list[VersionTransform]:
"""
Compile the from as a regular expression of a dictionary of the config list.
to be used with convert and match
Arguments:
config: The transform config
prefix: The version prefix
Return the compiled transform config.
"""
result = []
for conf in config:
new_conf = cast(VersionTransform, dict(conf))

from_re = conf.get("from", r"(.*)")
if from_re[0] == "^":
from_re = from_re[1:]
if from_re[-1] != "$":
from_re += "$"
from_re = f"^{re.escape(prefix)}{from_re}"

new_conf["from"] = re.compile(from_re)
result.append(new_conf)
return result


def match(
value: str, config: list[VersionTransform]
) -> tuple[Optional[Match[str]], Optional[VersionTransform], str]:
"""
Get the matched version.
Arguments:
value: That we want to match with
config: The result of `compile`
Returns the re match object, the matched config and the value as a tuple
On no match it returns None, value
"""
for conf in config:
matched = conf["from"].match(value)
if matched is not None:
return matched, conf, value
return None, None, value


def does_match(value: str, config: list[VersionTransform]) -> bool:
"""
Check if the version match with the config patterns.
Arguments:
value: That we want to match with
config: The result of `compile`
Returns True it it does match else False
"""
matched, _, _ = match(value, config)
return matched is not None


def get_value(matched: Optional[Match[str]], config: Optional[VersionTransform], value: str) -> str:
"""
Get the final value.
`match`, `config` and `value` are the result of `match`.
The `config` should have a `to` key with an expand template.
Arguments:
matched: The matched object to a regular expression
config: The result of `compile`
value: The default value on returned no match
Return the value
"""
return matched.expand(config.get("to", r"\1")) if matched is not None and config is not None else value


def print_versions(config: c2cciutils.configuration.PrintVersions) -> bool:
"""
Print some tools version.
Expand Down Expand Up @@ -319,17 +160,6 @@ def gopass(key: str, default: Optional[str] = None) -> Optional[str]:
raise


def gopass_put(secret: str, key: str) -> None:
"""
Put an entry in gopass.
Arguments:
secret: The secret value
key: The key
"""
subprocess.check_output(["gopass", "insert", "--force", key], input=secret.encode())


def add_authorization_header(headers: dict[str, str]) -> dict[str, str]:
"""
Add the Authorization header needed to be authenticated on GitHub.
Expand Down Expand Up @@ -406,20 +236,3 @@ def graphql(query_file: str, variables: dict[str, Any], default: Any = None) ->
if "data" not in json_response:
raise RuntimeError(f"GraphQL no data: {json.dumps(json_response, indent=2)}")
return cast(dict[str, Any], json_response["data"])


def snyk_exec() -> tuple[str, dict[str, str]]:
"""Get the Snyk cli executable path."""
if not os.path.exists(os.path.join(os.path.dirname(__file__), "node_modules")):
subprocess.run(["npm", "install"], cwd=os.path.dirname(__file__), check=True) # nosec

env = {**os.environ}
env["FORCE_COLOR"] = "true"
if "SNYK_TOKEN" not in env:
token = gopass("gs/ci/snyk/token")
if token is not None:
env["SNYK_TOKEN"] = token
if "SNYK_ORG" in env:
subprocess.run(["snyk", "config", "set", f"org={env['SNYK_ORG']}"], check=True, env=env)

return os.path.join(os.path.dirname(os.path.abspath(__file__)), "node_modules/snyk/bin/snyk"), env
1 change: 0 additions & 1 deletion c2cciutils/applications-versions.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# https://docs.renovatebot.com/modules/datasource/#github-releases-datasource
k3d-io/k3d: v5.7.4 # github-releases
postgresql: 16.1.0 # helm - https://charts.bitnami.com/bitnami
helm/chart-releaser: v1.6.1 # github-releases
Loading

0 comments on commit 3becc2e

Please sign in to comment.