Skip to content

Commit

Permalink
Fix first run (dry run)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 7, 2024
1 parent ce3501b commit 34cb052
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 119 deletions.
2 changes: 1 addition & 1 deletion .github/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pypi:
- version_tag
- version_branch
packages:
- path: .
- {}
docker:
images:
- name: camptocamp/tag-publish
6 changes: 3 additions & 3 deletions .github/workflows/repository-dispatch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ on:
required: true
name:
description: The package name
path:
description: The package path
folder:
description: The package folder
version:
description: The package version
tag:
Expand All @@ -33,7 +33,7 @@ jobs:
run: |
echo "Event type: ${{ github.event.client_payload.type }}"
echo "Package name: ${{ github.event.client_payload.name }}"
echo "Package path: ${{ github.event.client_payload.path }}"
echo "Package folder: ${{ github.event.client_payload.folder }}"
echo "Package version: ${{ github.event.client_payload.version }}"
echo "Package tag: ${{ github.event.client_payload.tag }}"
echo "Repository: ${{ github.event.client_payload.repository }}"
Expand Down
15 changes: 6 additions & 9 deletions config.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,16 @@ _Tag Publish configuration file_
- **`packages`** _(array)_: The configuration of packages that will be published.
- **Items** _(object)_: The configuration of package that will be published.
- **`group`** _(string)_: The image is in the group, should be used with the --group option of tag-publish script. Default: `"default"`.
- **`path`** _(string)_: The path of the pypi package. Default: `"."`.
- **`folder`** _(string)_: The folder of the pypi package. Default: `"."`.
- **`build_command`** _(array)_: The command used to do the build.
- **Items** _(string)_
- **`versions`** _(array)_: The kind or version that should be published, tag, branch or value of the --version argument of the tag-publish script. Default: `["version_tag"]`.
- **Items** _(string)_
- <a id="definitions/helm"></a>**`helm`**: Configuration to publish Helm charts on GitHub release.
- **One of**
- _object_: Configuration to publish on Helm charts on GitHub release.
- **`folders`** _(array)_: The folders that will be published.
- **Items** _(string)_
- **`versions`** _(array)_: The kind or version that should be published, tag, branch or value of the --version argument of the tag-publish script. Default: `["version_tag"]`.
- **Items** _(string)_
- : Must be: `false`.
- <a id="definitions/helm"></a>**`helm`** _(object)_: Configuration to publish Helm charts on GitHub release.
- **`folders`** _(array)_: The folders that will be published.
- **Items** _(string)_
- **`versions`** _(array)_: The kind or version that should be published, tag, branch or value of the --version argument of the tag-publish script. Default: `["version_tag"]`.
- **Items** _(string)_
- <a id="definitions/version_transform"></a>**`version_transform`** _(array)_: A version transformer definition.
- **Items** _(object)_
- **`from`** _(string)_: The from regular expression.
Expand Down
25 changes: 20 additions & 5 deletions tag_publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,25 @@ class GH:

def __init__(self) -> None:
"""Initialize the GitHub helper class."""
token = os.environ["GITHUB_TOKEN"]
token = (
os.environ["GITHUB_TOKEN"]
if "GITHUB_TOKEN" in os.environ
else subprocess.run(
["gh", "auth", "token"], check=True, stdout=subprocess.PIPE, encoding="utf-8"
).stdout.strip()
)
self.auth = github.Auth.Token(token)
self.github = github.Github(auth=self.auth)
self.repo = self.github.get_repo(os.environ["GITHUB_REPOSITORY"])
self.repo = self.github.get_repo(
os.environ["GITHUB_REPOSITORY"]
if "GITHUB_REPOSITORY" in os.environ
else subprocess.run(
["gh", "repo", "view", "--json", "name,owner", "--jq", '(.owner.login + "/" + .name)'],
check=True,
stdout=subprocess.PIPE,
encoding="utf-8",
).stdout.strip()
)
self.default_branch = self.repo.default_branch


Expand Down Expand Up @@ -77,8 +92,8 @@ def get_config(gh: GH) -> tag_publish.configuration.Configuration:
Get the configuration, with project and auto detections.
"""
config: tag_publish.configuration.Configuration = {}
if os.path.exists("ci/config.yaml"):
with open("ci/config.yaml", encoding="utf-8") as open_file:
if os.path.exists(".github/publish.yaml"):
with open(".github/publish.yaml", encoding="utf-8") as open_file:
yaml_ = ruamel.yaml.YAML()
config = yaml_.load(open_file)

Expand Down Expand Up @@ -237,7 +252,7 @@ class PublishedPayload(TypedDict, total=False):

type: str
name: str
path: str
folder: str
version: str
tag: str
repository: str
Expand Down
148 changes: 104 additions & 44 deletions tag_publish/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def main() -> None:
parser.add_argument("--branch", help="The branch from which to compute the version")
parser.add_argument("--tag", help="The tag from which to compute the version")
parser.add_argument("--dry-run", action="store_true", help="Don't do the publish")
parser.add_argument("--dry-run-tag", help="Don't do the publish, on a tag")
parser.add_argument("--dry-run-branch", help="Don't do the publish, on a branch")
parser.add_argument(
"--type",
help="The type of version, if no argument provided auto-determinate, can be: "
Expand All @@ -87,6 +89,13 @@ def main() -> None:
)
args = parser.parse_args()

if args.dry_run_tag is not None:
args.dry_run = True
os.environ["GITHUB_REF"] = f"refs/tags/{args.dry_run_tag}"
if args.dry_run_branch is not None:
args.dry_run = True
os.environ["GITHUB_REF"] = f"refs/heads/{args.dry_run_branch}"

github = tag_publish.GH()
config = tag_publish.get_config(github)

Expand Down Expand Up @@ -173,37 +182,78 @@ def main() -> None:
success = True
published_payload: list[tag_publish.PublishedPayload] = []

pypi_config = cast(
tag_publish.configuration.Pypi,
config.get("pypi", {}) if config.get("pypi", False) else {},
success &= _handle_pypi_publish(
args.group, args.dry_run, config, version, version_type, github, published_payload
)
success &= _handle_docker_publish(
args.group,
args.dry_run,
args.docker_versions,
args.snyk_version,
config,
version,
version_type,
github,
published_payload,
local,
)
success &= _handle_helm_publish(args.dry_run, config, version, version_type, github, published_payload)
_trigger_dispatch_events(config, published_payload, github)

if not success:
sys.exit(1)


def _handle_pypi_publish(
group: str,
dry_run: bool,
config: tag_publish.configuration.Configuration,
version: str,
version_type: str,
github: tag_publish.GH,
published_payload: list[tag_publish.PublishedPayload],
) -> bool:
success = True
pypi_config = config.get("pypi", {})
if pypi_config:
if pypi_config["packages"]:
if "packages" in pypi_config:
tag_publish.lib.oidc.pypi_login()

for package in pypi_config["packages"]:
if package.get("group", tag_publish.configuration.PIP_PACKAGE_GROUP_DEFAULT) == args.group:
if package.get("group", tag_publish.configuration.PIP_PACKAGE_GROUP_DEFAULT) == group:
publish = version_type in pypi_config.get(
"versions", tag_publish.configuration.PYPI_VERSIONS_DEFAULT
)
path = package.get("path", tag_publish.configuration.PYPI_PACKAGE_PATH_DEFAULT)
if args.dry_run:
print(f"{'Publishing' if publish else 'Checking'} '{path}' to pypi, skipping (dry run)")
folder = package.get("folder", tag_publish.configuration.PYPI_PACKAGE_FOLDER_DEFAULT)
if dry_run:
print(f"{'Publishing' if publish else 'Checking'} '{folder}' to pypi, skipping (dry run)")
else:
success &= tag_publish.publish.pip(package, version, version_type, publish, github)
published_payload.append(
{
"type": "pypi",
"path": "path",
"folder": folder,
"version": version,
"version_type": version_type,
}
)

docker_config = cast(
tag_publish.configuration.Docker,
config.get("docker", {}) if config.get("docker", False) else {},
)
return success


def _handle_docker_publish(
group: str,
dry_run: bool,
docker_versions: str,
snyk_version: str,
config: tag_publish.configuration.Configuration,
version: str,
version_type: str,
github: tag_publish.GH,
published_payload: list[tag_publish.PublishedPayload],
local: bool,
) -> bool:
success = True
docker_config = config.get("docker", {})
if docker_config:
security_text = ""
if local:
Expand Down Expand Up @@ -232,7 +282,6 @@ def main() -> None:
add_latest = True
for data in security.data:
row_tags = {t.strip() for t in data[alternate_tag_index].split(",") if t.strip()}
print(row_tags)
if "latest" in row_tags:
print("latest found in ", row_tags)
add_latest = False
Expand All @@ -243,23 +292,23 @@ def main() -> None:
images_src: set[str] = set()
images_full: list[str] = []
images_snyk: set[str] = set()
versions = args.docker_versions.split(",") if args.docker_versions else [version]
versions = docker_versions.split(",") if docker_versions else [version]
for image_conf in docker_config.get("images", []):
if image_conf.get("group", tag_publish.configuration.DOCKER_IMAGE_GROUP_DEFAULT) == args.group:
if image_conf.get("group", tag_publish.configuration.DOCKER_IMAGE_GROUP_DEFAULT) == group:
for tag_config in image_conf.get("tags", tag_publish.configuration.DOCKER_IMAGE_TAGS_DEFAULT):
tag_src = tag_config.format(version="latest")
image_source = f"{image_conf['name']}:{tag_src}"
images_src.add(image_source)
tag_snyk = tag_config.format(version=args.snyk_version or version).lower()
tag_snyk = tag_config.format(version=snyk_version or version).lower()
image_snyk = f"{image_conf['name']}:{tag_snyk}"

# Workaround sine we have the business plan
image_snyk = f"{image_conf['name']}_{tag_snyk}"

if not args.dry_run:
if not dry_run:
subprocess.run(["docker", "tag", image_source, image_snyk], check=True)
images_snyk.add(image_snyk)
if tag_snyk != tag_src and not args.dry_run:
if tag_snyk != tag_src and not dry_run:
subprocess.run(
[
"docker",
Expand Down Expand Up @@ -287,7 +336,7 @@ def main() -> None:
for alt_tag in [docker_version, *alt_tags]
]

if args.dry_run:
if dry_run:
for tag in tags:
print(
f"Publishing {image_conf['name']}:{tag} to {name}, skipping "
Expand All @@ -305,7 +354,7 @@ def main() -> None:
published_payload,
)

if args.dry_run:
if dry_run:
sys.exit(0)

snyk_exec, env = tag_publish.snyk_exec()
Expand Down Expand Up @@ -388,15 +437,21 @@ def main() -> None:

if dpkg_config_found:
success = False
return success

helm_config = cast(
tag_publish.configuration.HelmConfig,
config.get("helm", {}) if config.get("helm", False) else {},
)
if (
helm_config
and helm_config["folders"]
and version_type in helm_config.get("versions", tag_publish.configuration.HELM_VERSIONS_DEFAULT)

def _handle_helm_publish(
dry_run: bool,
config: tag_publish.configuration.Configuration,
version: str,
version_type: str,
github: tag_publish.GH,
published_payload: list[tag_publish.PublishedPayload],
) -> bool:
success = True
helm_config = config.get("helm", {})
if helm_config.get("folders") and version_type in helm_config.get(
"versions", tag_publish.configuration.HELM_VERSIONS_DEFAULT
):
application_download.cli.download_application("helm/chart-releaser")

Expand Down Expand Up @@ -432,19 +487,27 @@ def main() -> None:
version = ".".join(versions)

for folder in helm_config["folders"]:
token = os.environ["GITHUB_TOKEN"]
success &= tag_publish.publish.helm(folder, version, owner, repo, commit_sha, token)
published_payload.append(
{
"type": "helm",
"path": folder,
"version": version,
"version_type": version_type,
}
)
if dry_run:
print(f"Publishing '{folder}' to helm, skipping (dry run)")
else:
token = os.environ["GITHUB_TOKEN"]
success &= tag_publish.publish.helm(folder, version, owner, repo, commit_sha, token)
published_payload.append(
{
"type": "helm",
"folder": folder,
"version": version,
"version_type": version_type,
}
)
return success

config = tag_publish.get_config(tag_publish.GH())

def _trigger_dispatch_events(
config: tag_publish.configuration.Configuration,
published_payload: list[tag_publish.PublishedPayload],
github: tag_publish.GH,
) -> None:
for published in published_payload:
for dispatch_config in config.get("dispatch", []):
repository = dispatch_config.get("repository")
Expand All @@ -463,9 +526,6 @@ def main() -> None:
github_repo = github.repo
github_repo.create_repository_dispatch(event_type, published) # type: ignore[arg-type]

if not success:
sys.exit(1)


if __name__ == "__main__":
main()
Loading

0 comments on commit 34cb052

Please sign in to comment.