diff --git a/.github/publish.yaml b/.github/publish.yaml index 6c35c2e..94724be 100644 --- a/.github/publish.yaml +++ b/.github/publish.yaml @@ -20,3 +20,5 @@ docker: helm: folders: - tests +dispatch: + - {} diff --git a/.github/workflows/repository-dispatch.yaml b/.github/workflows/repository-dispatch.yaml index f6a0dc6..680f3d8 100644 --- a/.github/workflows/repository-dispatch.yaml +++ b/.github/workflows/repository-dispatch.yaml @@ -5,21 +5,8 @@ on: types: - published inputs: - type: - description: The type of the event - required: true - name: - description: The package name - folder: - description: The package folder - version: - description: The package version - tag: - description: The package tag - repository: - description: The repository name or URL - version_type: - description: The version type + content: + description: Published content required: true jobs: @@ -31,10 +18,4 @@ jobs: steps: - name: Print the event run: | - echo "Event type: ${{ github.event.client_payload.type }}" - echo "Package name: ${{ github.event.client_payload.name }}" - 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 }}" - echo "Version type: ${{ github.event.client_payload.version_type }}" + echo "Publish: ${{ toJson(github.event.client_payload.content) }}" diff --git a/README.md b/README.md index 8da6ff9..bdcfb92 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ permissions: packages: write # To publish Python packages using OIDC id-token: write - # To publish Helm charts + # To publish Helm charts and send repository dispatch notifications contents: write ``` @@ -27,7 +27,7 @@ Install the package in the worklow: ```yaml - name: Install tag-publish - run: pip install c2cciutils-publish + run: pip install tag-publish ``` Do the publishing: @@ -77,7 +77,7 @@ The configuration file is `.github/publish.yaml`, the schema is `https://raw.git ### Dry run -Dry run publish: `GITHUB_REF=... c2cciutils-publish --dry-run ...` +Dry run publish: `GITHUB_REF=... tag-publish --dry-run ...` ### To pypi @@ -247,6 +247,43 @@ git commit --allow-empty -m "Initialize gh-pages branch" git push origin gh-pages ``` +## Dispatch + +The minimal config is like this: + +```yaml +dispatch: + - {} +``` + +The required permission is `contents: write`. + +This will create a repository dispatch of type `published` on own repository with the `content` e.g.: + +```json +{ + "version": "1.2.3", + "version_type": "version_tag", + "repository": "camptocamp/tag-publish", + "items": [ + { + "type": "docker", + "image": "camptocamp/tag-publish", + "repository": "ghcr.io", + "tag": "1.2.3" + }, + { + "type": "pypi", + "path": "." + }, + { + "type": "helm", + "path": "." + } + ] +} +``` + ## Contributing Install the pre-commit hooks: diff --git a/config.md b/config.md index dccd6bb..2175369 100644 --- a/config.md +++ b/config.md @@ -10,7 +10,7 @@ _Tag Publish configuration file_ - **`docker`**: Refer to _[#/definitions/docker](#definitions/docker)_. - **`pypi`**: Refer to _[#/definitions/pypi](#definitions/pypi)_. - **`helm`**: Refer to _[#/definitions/helm](#definitions/helm)_. -- **`dispatch`** _(array)_: Default: `[{}]`. +- **`dispatch`** _(array)_: Default: `[]`. - **Items** _(object)_: Send a dispatch event to an other repository. Default: `{}`. - **`repository`** _(string)_: The repository name to be triggered. Default: `"camptocamp/argocd-gs-gmf-apps"`. - **`event-type`** _(string)_: The event type to be triggered. Default: `"published"`. diff --git a/tag_publish/__init__.py b/tag_publish/__init__.py index b5f7592..6eaee18 100644 --- a/tag_publish/__init__.py +++ b/tag_publish/__init__.py @@ -302,10 +302,7 @@ class PublishedPayload(TypedDict, total=False): """ type: str - name: str + image: str folder: str - version: str tag: str repository: str - version_type: str - id: int diff --git a/tag_publish/cli.py b/tag_publish/cli.py index 61afb66..8ec2967 100644 --- a/tag_publish/cli.py +++ b/tag_publish/cli.py @@ -8,7 +8,6 @@ import json import os import os.path -import random import re import subprocess # nosec import sys @@ -197,7 +196,7 @@ def main() -> None: local, ) success &= _handle_helm_publish(args.dry_run, config, version, version_type, github, published_payload) - _trigger_dispatch_events(config, published_payload, github) + _trigger_dispatch_events(config, version, version_type, published_payload, github) if not success: sys.exit(1) @@ -228,14 +227,7 @@ def _handle_pypi_publish( 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", - "folder": folder, - "version": version, - "version_type": version_type, - } - ) + published_payload.append({"type": "pypi", "folder": folder}) return success @@ -363,7 +355,6 @@ def _handle_docker_publish( tag_src, tags, images_full, - version_type, published_payload, ) @@ -507,39 +498,35 @@ def _handle_helm_publish( 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, - } - ) + published_payload.append({"type": "helm", "folder": folder}) return success def _trigger_dispatch_events( config: tag_publish.configuration.Configuration, + version: str, + version_type: str, 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") - event_type = dispatch_config.get( - "event-type", tag_publish.configuration.DISPATCH_EVENT_TYPE_DEFAULT - ) - - id_ = random.randint(1, 100000) # nosec # noqa: S311 - published["id"] = id_ - - if repository: - print(f"Triggering {event_type}:{id_} on {repository} with {json.dumps(published)}") - github_repo = github.github.get_repo(repository) - else: - print(f"Triggering {event_type}:{id_} with {json.dumps(published)}") - github_repo = github.repo - github_repo.create_repository_dispatch(event_type, published) # type: ignore[arg-type] + for dispatch_config in config.get("dispatch", []): + repository = dispatch_config.get("repository") + event_type = dispatch_config.get("event-type", tag_publish.configuration.DISPATCH_EVENT_TYPE_DEFAULT) + + published = { + "version": version, + "version_type": version_type, + "repository": github.repo.full_name, + "items": published_payload, + } + + if repository: + print(f"Triggering {event_type} on {repository} with {json.dumps(published)}") + github_repo = github.github.get_repo(repository) + else: + print(f"Triggering {event_type} with {json.dumps(published)}") + github_repo = github.repo + github_repo.create_repository_dispatch(event_type, {"content": published}) if __name__ == "__main__": diff --git a/tag_publish/configuration.py b/tag_publish/configuration.py index 0891cc7..f7318a1 100644 --- a/tag_publish/configuration.py +++ b/tag_publish/configuration.py @@ -45,7 +45,7 @@ class Configuration(TypedDict, total=False): Dispatch. default: - - {} + [] """ @@ -53,7 +53,7 @@ class Configuration(TypedDict, total=False): """ Default value of the field path 'Dispatch item' """ -DISPATCH_DEFAULT = [{}] +DISPATCH_DEFAULT: List[Any] = [] """ Default value of the field path 'configuration dispatch' """ diff --git a/tag_publish/publish.py b/tag_publish/publish.py index c5e434f..35d1dd1 100644 --- a/tag_publish/publish.py +++ b/tag_publish/publish.py @@ -110,7 +110,6 @@ def docker( tag_src: str, dst_tags: list[str], images_full: list[str], - version_type: str, published: Optional[list[tag_publish.PublishedPayload]] = None, ) -> bool: """ @@ -160,9 +159,8 @@ def docker( { "type": "docker", "repository": config["server"], - "name": image_config["name"], + "image": image_config["name"], "tag": tag, - "version_type": version_type, } ) else: @@ -182,9 +180,8 @@ def docker( { "type": "docker", "repository": "docker.io", - "name": image_config["name"], + "image": image_config["name"], "tag": tag, - "version_type": version_type, } ) new_images_full.append(f"{image_config['name']}:{tag}") diff --git a/tag_publish/schema.json b/tag_publish/schema.json index 52cbad7..2f22059 100644 --- a/tag_publish/schema.json +++ b/tag_publish/schema.json @@ -223,7 +223,7 @@ "dispatch": { "title": "Dispatch", "type": "array", - "default": [{}], + "default": [], "items": { "title": "dispatch config",